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
-
.acts_as_cabalist(opts = {}) ⇒ void
Class helper to add Cabalist functionality to a Rails model.
-
.auto_classified ⇒ ActiveRecord::Relation
Automatically classified records.
-
.build_model ⇒ Ai4r::Classifiers::Object
Build the classification model from scratch.
-
.class_variable_domain ⇒ Array
Show possible values for the classification.
-
.classifier ⇒ Ai4r::Classifiers::Object
Get the classification model for a given class.
-
.cohens_kappa ⇒ Object
Calculate the Cohen’s kappa coefficient for the classifier.
-
.get_class_variable_name ⇒ Symbol
Get name of the class variable.
-
.get_feature_names ⇒ Array
Get names of feature attributes.
-
.manually_classified ⇒ ActiveRecord::Relation
Manually classified records.
-
.not_classified ⇒ ActiveRecord::Relation
Records that have not yet been classified.
-
.percentage_ageement ⇒ Object
Percantage agreement between classifier and human.
-
.percentage_random_agreement ⇒ Object
Percentage of random agreement between classifier and human labeling and the random classifier.
-
.train_model ⇒ Ai4r::Classifiers::Object
Build the classification model from scratch and store it in cache.
Instance Method Summary collapse
-
#classify ⇒ Object
Get predicted value of the class variable.
-
#classify! ⇒ self
Set the class variable to the value suggested by the classifier.
-
#get_class_variable ⇒ Object
Value of the class variable.
-
#get_features ⇒ Array
Get an array of features.
-
#set_class_variable(new_class_variable) ⇒ Object
Set the value of the class variable.
-
#teach(new_class_variable) ⇒ DateTime
Set the value of the class variable.
Class Method Details
.acts_as_cabalist(opts = {}) ⇒ void
This method returns an undefined value.
Class helper to add Cabalist functionality to a Rails model.
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_classified ⇒ ActiveRecord::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.
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_model ⇒ Ai4r::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.
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_domain ⇒ Array
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.
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 } ) |
.classifier ⇒ Ai4r::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.
105 106 107 108 109 110 |
# File 'lib/cabalist/model_additions.rb', line 105 define_singleton_method( :classifier, lambda { raise NoMethodError, _METHOD_UNAVAILABLE } ) |
.cohens_kappa ⇒ Object
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_name ⇒ Symbol
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.
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_names ⇒ Array
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.
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_classified ⇒ ActiveRecord::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.
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_classified ⇒ ActiveRecord::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.
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_ageement ⇒ Object
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_agreement ⇒ Object
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_model ⇒ Ai4r::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.
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
#classify ⇒ Object
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.
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.
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_variable ⇒ Object
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.
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_features ⇒ Array
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.
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.
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.
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 } ) |