Class: Rrbayes::Category

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

Constant Summary collapse

DOCUMENTS_SCOPE =
'documents'
EVIDENCES_SCOPE =
'evidences'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, classifier) ⇒ Category

Returns a new instance of Category.



10
11
12
13
14
# File 'lib/rrbayes/category.rb', line 10

def initialize(name, classifier)
  @name = name
  @db = classifier.db
  persist
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#attributes_score(attributes) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/rrbayes/category.rb', line 25

def attributes_score(attributes)
  total = evidences_total.to_f
  attributes.map do |attribute, evidences|
    (evidences_for(attribute) || 0.1).to_f / total
  end.inject(0) do |score, attr_likelyhood|
    score + Math.log(attr_likelyhood)
  end
end

#documents_totalObject



42
43
44
# File 'lib/rrbayes/category.rb', line 42

def documents_total
  @db.get(documents_key)
end

#evidences_for(attribute) ⇒ Object



34
35
36
# File 'lib/rrbayes/category.rb', line 34

def evidences_for(attribute)
  @db.hget(name, attribute)
end

#evidences_totalObject



38
39
40
# File 'lib/rrbayes/category.rb', line 38

def evidences_total
  @db.get(evidences_key)
end

#learn(frequency_map) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/rrbayes/category.rb', line 16

def learn(frequency_map)
  @db.pipelined do
    frequency_map.each do |attribute, evidences|
      store_evidences(attribute, evidences)
    end
    increment_documents
  end
end