Cadmium::Classifier::Bayes
Inherits MessagePack::Serializable < YAML::Serializable < JSON::Serializable < Reference < Object
This is a native-bayes classifier which used Laplace Smoothing. It can be trained to categorize sentences based on the words in that sentence.
Example:
classifier = Cadmium::Classifier::Bayes.new
# Train some angry examples
classifier.train("omg I can't believe you would do that to me", "angry")
classifier.train("I hate you so much!", "angry")
classifier.train("Just go. I don't need this.", "angry")
classifier.train("You're so full of shit!", "angry")
# Some happy ones
classifier.train("omg you're the best!", "happy")
classifier.train("I can't believe how happy you make me", "happy")
classifier.train("I love you so damn much!", "happy")
classifier.train("You're the best!", "happy")
# And some indifferent ones
classifier.train("Idk, what do you think?", "indifferent")
classifier.train("yeah that's ok", "indifferent")
classifier.train("cool", "indifferent")
classifier.train("I guess we could do that", "indifferent")
# Now let's test it on a sentence
classifier.classify("You shit head!")
# => {"angry" => 85.5, "happy" => 10.2, "indifferent" => 4.3}
# Or just get the top category
classifier.classify_category("You're the best :)")
# => "happy"
Constants
Constructors
Instance methods
Determines what category the text belongs to.
Returns a Hash with all categories and their probabilities.
Convenience method that returns just the top category name instead of all probabilities. Use this when you only need the most likely category.
Example:
classifier = Cadmium::Classifier::Bayes.new
classifier.train("I love this!", "positive")
classifier.train("This is terrible", "negative")
classifier.classify_category("This is amazing!") # => "positive"
Build a frequency hash map where
- the keys are the entries in
tokens - the values are the frequency of each entry in
tokens
Intializes each of our data structure entities for this
new category and returns self.
Calculate the probaility that a token belongs to
a category.
Train our native-bayes classifier by telling it what
category the train text corresponds to.