class Bayes::Classifier
Attributes
categories[R]
Public Class Methods
new()
click to toggle source
# File lib/bayes/classifier.rb, line 5 def initialize @categories = {} end
Public Instance Methods
apply_weighting(category, coeff)
click to toggle source
# File lib/bayes/classifier.rb, line 32 def apply_weighting(category, coeff) ensure_category(category).apply_weighting(coeff) end
classify(string)
click to toggle source
# File lib/bayes/classifier.rb, line 36 def classify(string) words = string.word_hash.keys @categories.each_with_object({}) do |category, hash| hash[category[0]] = category[1].score_for(words) end.sort_by { |cat| -cat[1] }[0][0] end
ensure_category(category)
click to toggle source
# File lib/bayes/classifier.rb, line 13 def ensure_category(category) @categories[category] ||= Bayes::Category.new end
flush()
click to toggle source
# File lib/bayes/classifier.rb, line 47 def flush @categories.each{ |name, cat| cat.reset } end
flush_all()
click to toggle source
# File lib/bayes/classifier.rb, line 51 def flush_all @categories = {} end
pop_unused()
click to toggle source
# File lib/bayes/classifier.rb, line 43 def pop_unused @categories.delete_if{ |name,cat| cat.blank? } end
train(category, text)
click to toggle source
# File lib/bayes/classifier.rb, line 9 def train(category, text) ensure_category(category).train(text) end
train_with_array(category, lines)
click to toggle source
# File lib/bayes/classifier.rb, line 17 def train_with_array(category, lines) lines.each{ |line| train(category, line) } end
train_with_csv(filename, separator: "||")
click to toggle source
# File lib/bayes/classifier.rb, line 25 def train_with_csv(filename, separator: "||") csv = CSV.new File.read(filename), col_sep: separator, quote_char: "§" # hope § won't be used anywhere csv.each do |row| train row[1], row[0] end end
train_with_file(category, filename)
click to toggle source
# File lib/bayes/classifier.rb, line 21 def train_with_file(category, filename) train_with_array category, File.read(filename).split(/\r?\n/) end