class Classiphier::Bayes

Public Class Methods

new() click to toggle source
# File lib/classiphier/bayes.rb, line 3
def initialize
  @data = Data.new
end

Public Instance Methods

classifications(sentence) click to toggle source
# File lib/classiphier/bayes.rb, line 17
def classifications(sentence)
  Hash.new(0).tap do |score|
    @data[:data].each do |category, data|
      sentence.words.each do |word|
        value = data[:data].fetch(word, 0.1).to_f
        score[category] += Math.log(value / data[:words])
      end

      value = @data[:data][category][:count].to_f
      score[category] += Math.log(value / @data[:count])
    end
  end
end
classify(sentence) click to toggle source
# File lib/classiphier/bayes.rb, line 13
def classify(sentence)
  classifications(sentence).min_by { |a| -a[1] }[0]
end
train(category, sentence) click to toggle source
# File lib/classiphier/bayes.rb, line 7
def train(category, sentence)
  @data.perform!
  @data[:data][category] ||= Data.new
  @data[:data][category].train(sentence)
end