class Bayes::PlainBayes

Public Instance Methods

estimate(tokens, take=15) click to toggle source
# 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) click to toggle source
# 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