Class: Bayes::PlainBayes

Inherits:
FilterBase show all
Defined in:
lib/bayes.rb

Defined Under Namespace

Classes: Corpus

Instance Attribute Summary

Attributes inherited from FilterBase

#charset, #db_name, #ham, #spam

Instance Method Summary collapse

Methods inherited from FilterBase

#[], #convert, #initialize, #save

Constructor Details

This class inherits a constructor from Bayes::FilterBase

Instance Method Details

#estimate(tokens, take = 15) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/bayes.rb', line 191

def estimate(tokens, take=15)
	s = tokens.uniq.map{|i| score(i)}.compact.sort{|a, b| (0.5-a).abs <=> (0.5-b)}.reverse[0...take]
	return nil if s.empty? || s.include?(1.0) && s.include?(0.0)

	prod = s.inject(1.0){|r, i| r*i}
	return prod/(prod+s.inject(1.0){|r, i| r*(1-i)})
end

#score(token) ⇒ Object



184
185
186
187
188
189
# File 'lib/bayes.rb', line 184

def score(token)
	return nil unless @spam.include?(token) || @ham.include?(token)
	s = @spam[token]
	h = @ham[token]
	s/(s+h)
end