Class: Rrbayes::Classifier
- Inherits:
-
Object
- Object
- Rrbayes::Classifier
- Defined in:
- lib/rrbayes/rrbayes.rb
Instance Attribute Summary collapse
-
#categories ⇒ Object
readonly
Returns the value of attribute categories.
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
-
#category(name) ⇒ Object
Returns the category objects with the name provided in the argument.
-
#classify(frequency_map) ⇒ Object
given a frequency hash tries to guess to which category the hash is most likely to belong to.
-
#initialize(options = {}, redis_options = {}) ⇒ Classifier
constructor
creates a new classifier, takes 2 hashes the first hash contains Rrbayes specific options the second hash is passed to the backend Redis#new constructor.
-
#learn(frequency_map, options) ⇒ Object
given a frequency hash and a category, stores teh frequency data for the given catogory.
Constructor Details
#initialize(options = {}, redis_options = {}) ⇒ Classifier
creates a new classifier, takes 2 hashes the first hash contains Rrbayes specific options the second hash is passed to the backend Redis#new constructor
Rrbayes.new(:categories => %w(spam ham), {:host => '127.0.0.1'})
16 17 18 19 |
# File 'lib/rrbayes/rrbayes.rb', line 16 def initialize( = {}, = {}) @db = Redis.new() @categories = find_categories().map { |c| Category.new(c, self) } end |
Instance Attribute Details
#categories ⇒ Object (readonly)
Returns the value of attribute categories.
8 9 10 |
# File 'lib/rrbayes/rrbayes.rb', line 8 def categories @categories end |
#db ⇒ Object (readonly)
Returns the value of attribute db.
8 9 10 |
# File 'lib/rrbayes/rrbayes.rb', line 8 def db @db end |
Instance Method Details
#category(name) ⇒ Object
Returns the category objects with the name provided in the argument
43 44 45 |
# File 'lib/rrbayes/rrbayes.rb', line 43 def category(name) @categories.find { |c| c.name == name} end |
#classify(frequency_map) ⇒ Object
given a frequency hash tries to guess to which category the hash is most likely to belong to
unknown_data = {'viagra' => 1, 'buy' => 1}
classifier.classify(unknown_data)
=> 'spam'
38 39 40 |
# File 'lib/rrbayes/rrbayes.rb', line 38 def classify(frequency_map) @categories.sort_by { |c| -c.attributes_score(frequency_map) }.first.name end |
#learn(frequency_map, options) ⇒ Object
given a frequency hash and a category, stores teh frequency data for the given catogory
classifier = Rrbayes.new(:categories => %w(spam ham))
classifier.learn {'viagra' => 1, 'buy' => 1}, :as => 'spam'
27 28 29 |
# File 'lib/rrbayes/rrbayes.rb', line 27 def learn(frequency_map, ) category([:as]).learn(frequency_map) end |