class

Cadmium::BayesClassifier

Inherits 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.bayes_classifier.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.categorize("You shit head!")
# => "angry"

puts classifier.categorize("You're the best :)")
# => "happy"

classifier.categorize("idk, my bff jill?")
# => "indifferent"

Constants

DEFAULT_TOKENIZER = Cadmium::WordTokenizer.new

Constructors

new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
Source
new(pull : JSON::PullParser)
Source
new(tokenizer = nil)
Source
new(*, __pull_for_json_serializable pull : JSON::PullParser)
Source
new(*, __context_for_yaml_serializable ctx : YAML::ParseContext, __node_for_yaml_serializable node : YAML::Nodes::Node)
Source

Instance methods

categories

Category names

Source
categorize(text)

Determines what category the text belongs to.

Source
doc_count

Document frequency table for each of our categories.

Source
frequency_table(tokens)

Build a frequency hash map where

  • the keys are the entries in tokens
  • the values are the frequency of each entry in tokens
Source
initialize_category(name)

Intializes each of our data structure entities for this new category and returns self.

Source
token_probability(token, category)

Calculate the probaility that a token belongs to a category.

Source
tokenizer
Source
tokenizer=(tokenizer : Cadmium::Tokenizer)
Source
total_documents

Number of documents we have learned from.

Source
train(text, category)

Train our native-bayes classifier by telling it what category the train text corresponds to.

Source
vocabulary

The words to learn from.

Source
word_count

For each category, how many total words were mapped to it.

Source
word_frequency_count

Word frequency table for each category.

Source