Module: Cabalist::ModelAdditions

Defined in:
lib/cabalist/model_additions.rb

Overview

Core functionality of Cabalist.

This module is a placeholder for methods that are mixed in with ActiveRecord::Base as well as those that are created by calling acts_as_cabalist method from within the class definition of an inheriting class.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.acts_as_cabalist(opts = {}) ⇒ void

This method returns an undefined value.

Class helper to add Cabalist functionality to a Rails model.

Parameters:

  • opts (Hash) (defaults to: {})

    An options hash.

Options Hash (opts):

  • :features (Array)

    Array of symbols representing name of the model attributes used as features (predictors) to train the classifier.

  • :class_variable (Symbol)

    Symbol representing the name of the model attribute to be predicted by the classifier.

  • :collection (Symbol) — default: :manually_classified

    Symbol representing the class method used to pull data to be used to train the classifier. Anything returning either an ActiveRecord::Relation or an Array of objects of this given class will do.

  • :algorithm (Symbol) — default: :id3

    Symbol representing the algorithm to be used by the classifier. The library currently supports a number of algorithms provided by the ai4r gem. Accepted values are as following:

    • :hyperpipes for Hyperpipes

    • :ib1 for Simple Instance Based Learning

    • :id3 for Iterative Dichotomiser 3

    • :one_r for One Attribute Rule

    • :prism for PRISM

    • :zero_r for ZeroR



42
43
44
45
46
47
# File 'lib/cabalist/model_additions.rb', line 42

define_singleton_method(
  :acts_as_cabalist,
  lambda { |opts|
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

.auto_classifiedActiveRecord::Relation

Automatically classified records.

Named scope which returns objects that were classified automatically. That means they have their class variable value set as well as non-nil value of autoclassified_at attribute.

Returns:

  • (ActiveRecord::Relation)

    automatically classified records.



57
58
59
60
61
62
# File 'lib/cabalist/model_additions.rb', line 57

define_singleton_method(
  :auto_classified,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

.build_modelAi4r::Classifiers::Object

Build the classification model from scratch.

You should probably not use this method directly as it will go ahead and compute the classifier anew each and every time instead of looking at any cached versions.

Returns:

  • (Ai4r::Classifiers::Object)

    one of classifiers from the Ai4r::Classifiers module, depending on the value (and presence) of the :algorithm option in the acts_as_cabalist method call.



74
75
76
77
78
79
# File 'lib/cabalist/model_additions.rb', line 74

define_singleton_method(
  :build_model,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

.class_variable_domainArray

Show possible values for the classification.

This method will return all unique values of the class variable that the classifier knows about. If any new values have been added after the classifier has last been retrained, they will not find their way here.

Returns:

  • (Array)

    an Array containing all unique class variable values.



89
90
91
92
93
94
# File 'lib/cabalist/model_additions.rb', line 89

define_singleton_method(
  :class_variable_domain,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

.classifierAi4r::Classifiers::Object

Get the classification model for a given class.

This method will try to find a ready model in the local cache but having failed that will resort to the ‘train_model’ to create one.

Returns:

  • (Ai4r::Classifiers::Object)

    one of classifiers from the Ai4r::Classifiers module, depending on the value (and presence) of the :algorithm option in the acts_as_cabalist method call.



105
106
107
108
109
110
# File 'lib/cabalist/model_additions.rb', line 105

define_singleton_method(
  :classifier,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

.cohens_kappaObject

Calculate the Cohen’s kappa coefficient for the classifier

Cohen’s kappa is a measure of classifier quality. For more about this method of measurement, see the relevant article on Wikipedia.



118
119
120
121
122
123
# File 'lib/cabalist/model_additions.rb', line 118

define_singleton_method(
  :cohens_kappa,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

.get_class_variable_nameSymbol

Get name of the class variable.

This method returns the class variable name (as symbol) as it has been passed to the acts_as_cabalist method call as :class_variable option.

Returns:

  • (Symbol)

    name of the class variable.



131
132
133
134
135
136
# File 'lib/cabalist/model_additions.rb', line 131

define_singleton_method(
  :get_class_variable_name,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

.get_feature_namesArray

Get names of feature attributes.

This method returns the Array of attribute names (as symbols) as they have been passed to the acts_as_cabalist method call as :features option.

Returns:

  • (Array)

    an Array of symbols representing feature names



145
146
147
148
149
150
# File 'lib/cabalist/model_additions.rb', line 145

define_singleton_method(
  :get_feature_names,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

.manually_classifiedActiveRecord::Relation

Manually classified records.

Named scope which returns objects that were classified but not automatically, that is they have their class variable value set but autoclassified_at nil.

Returns:

  • (ActiveRecord::Relation)

    manually classified records.



160
161
162
163
164
165
# File 'lib/cabalist/model_additions.rb', line 160

define_singleton_method(
  :manually_classified,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

.not_classifiedActiveRecord::Relation

Records that have not yet been classified.

Named scope which returns objects that were classified automatically. That means they have their class variable value set as well as non-nil value of autoclassified_at attribute.

Returns:

  • (ActiveRecord::Relation)

    records that have not yet been classified.



176
177
178
179
180
181
# File 'lib/cabalist/model_additions.rb', line 176

define_singleton_method(
  :not_classified,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

.percentage_ageementObject

Percantage agreement between classifier and human

This is the number between 0 and 1 which represents how often the classifier and human decision-maker agree. It is one of quality measures of the classifier, albeit a naive one.



189
190
191
192
193
194
# File 'lib/cabalist/model_additions.rb', line 189

define_singleton_method(
  :percentage_ageement,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

.percentage_random_agreementObject

Percentage of random agreement between classifier and human labeling and the random classifier

This is the number between 0 and 1 which represents how often the classifier and human decisions may accidentally be the same, due to sheer randomness rather than actual intelligence reperesented by the classifier.



204
205
206
207
208
209
# File 'lib/cabalist/model_additions.rb', line 204

define_singleton_method(
  :percentage_random_agreement,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

.train_modelAi4r::Classifiers::Object

Build the classification model from scratch and store it in cache.

This method will use a ‘build_model’ call to create a model from scratch but once it has been created, it will also be immediately stored in the cache for further retrieval using the ‘classifier’ method.

Returns:

  • (Ai4r::Classifiers::Object)

    one of classifiers from the Ai4r::Classifiers module, depending on the value (and presence) of the :algorithm option in the acts_as_cabalist method call.



221
222
223
224
225
226
# File 'lib/cabalist/model_additions.rb', line 221

define_singleton_method(
  :train_model,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

Instance Method Details

#classifyObject

Get predicted value of the class variable

This method will query the classifier of the instance’s corresponding class for the predicted classification of this instance, given the value of its features.

Returns:

  • (Object)

    predicted value of the class variable



238
239
240
241
242
243
244
# File 'lib/cabalist/model_additions.rb', line 238

send(
  :define_method,
  :classify,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

#classify!self

Set the class variable to the value suggested by the classifier

This method will query the classifier of the instance’s corresponding class for the predicted classification of this instance, given the value of its features and then set the class variable to that value.

Returns:

  • (self)

    the current object instance



255
256
257
258
259
260
261
# File 'lib/cabalist/model_additions.rb', line 255

send(
  :define_method,
  :classify!,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

#get_class_variableObject

Value of the class variable.

This method returns the value of an attribute passed as the :class_variable option of the acts_as_cabalist method call.

Returns:

  • (Object)

    the value of the class variable



271
272
273
274
275
276
277
# File 'lib/cabalist/model_additions.rb', line 271

send(
  :define_method,
  :get_class_variable,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

#get_featuresArray

Get an array of features.

Returns an Array of values which result from methods passed as the :feature option of the acts_as_cabalist method call. Each of this methods is called upon current instance and results are returned.

Returns:

  • (Array)

    array of values corresponding to attributes selected as features



289
290
291
292
293
294
295
# File 'lib/cabalist/model_additions.rb', line 289

send(
  :define_method,
  :get_features,
  lambda {
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

#set_class_variable(new_class_variable) ⇒ Object

Set the value of the class variable.

This method sets the value of an attribute passed as the :class_variable option of the acts_as_cabalist method call to the new value.

Parameters:

  • new_class_variable (Object)

    the new value of the class variable

Returns:

  • (Object)

    the new value of the class variable



307
308
309
310
311
312
313
# File 'lib/cabalist/model_additions.rb', line 307

send(
  :define_method,
  :set_class_variable,
  lambda { |i|
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)

#teach(new_class_variable) ⇒ DateTime

Set the value of the class variable.

This method sets the value of an attribute passed as the :class_variable option of the acts_as_cabalist method call to the new value and sets the autoclassified_at to nil so that current object is not treated as automatically classified.

Parameters:

  • new_class_variable (Object)

    the new value of the class variable

Returns:

  • (DateTime)

    timestamp of the classification



326
327
328
329
330
331
332
# File 'lib/cabalist/model_additions.rb', line 326

send(
  :define_method,
  :teach,
  lambda { |new_class|
    raise NoMethodError, _METHOD_UNAVAILABLE
  }
)