Class: Rrbayes::Classifier

Inherits:
Object
  • Object
show all
Defined in:
lib/rrbayes/rrbayes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {}, redis_options = {})
  @db = Redis.new(redis_options)
  @categories = find_categories(options).map { |c| Category.new(c, self) }
end

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories.



8
9
10
# File 'lib/rrbayes/rrbayes.rb', line 8

def categories
  @categories
end

#dbObject (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, options)
  category(options[:as]).learn(frequency_map)
end