Class: Bayes::Category

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

Constant Summary collapse

MIN_SCORE =
0.0000001

Instance Method Summary collapse

Constructor Details

#initializeCategory

Returns a new instance of Category.



5
6
7
# File 'lib/bayes/category.rb', line 5

def initialize
  reset
end

Instance Method Details

#apply_weighting(coeff) ⇒ Object



29
30
31
32
33
# File 'lib/bayes/category.rb', line 29

def apply_weighting(coeff)
  top_words.each do |word|
    apply_weighting_for word, coeff
  end
end

#apply_weighting_for(word, coeff) ⇒ Object



35
36
37
38
39
40
# File 'lib/bayes/category.rb', line 35

def apply_weighting_for(word, coeff)
  if old_weight = @words[word]
    @words[word] = old_weight * coeff
    @words_count += @words[word] - old_weight
  end
end

#blank?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/bayes/category.rb', line 63

def blank?
  @words_count == 0
end

#forget(text) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/bayes/category.rb', line 21

def forget(text)
  text.word_hash.each do |word, count|
    @words[word] = @words[word].to_i - count
    @words.delete(word) if @words[word] == 0
    @words_count -= count
  end
end

#resetObject



9
10
11
12
# File 'lib/bayes/category.rb', line 9

def reset
  @words = {}
  @words_count = 0
end

#score_for(words) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bayes/category.rb', line 46

def score_for(words)
  if @words_count > 0
    words = words.word_hash.keys unless words.is_a? Array

    if words.any?
      words.map do |word|
        word_value = @words[word] || MIN_SCORE
        Math.log(word_value / @words_count.to_f)
      end.inject(:+)
    else
      Math.log(MIN_SCORE / @words_count)
    end
  else
    -Float::INFINITY
  end
end

#top_words(num = 100) ⇒ Object



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

def top_words(num = 100)
  @words.sort_by{ |w,c| -c }.slice(0,num).map{ |w| w[0] }
end

#train(text) ⇒ Object



14
15
16
17
18
19
# File 'lib/bayes/category.rb', line 14

def train(text)
  text.word_hash.each do |word, count|
    @words[word] = @words[word].to_i + count
    @words_count += count
  end
end