class NBClass::Classifier

Constants

SMALL_PROBABILITY

Public Class Methods

new() click to toggle source
# File lib/nb_class/classifier.rb, line 10
def initialize
  @category_examples = {}
  @occurrences = {}
  @global_occurrences = {}
  @probabilities = {}
  @global_probabilities = {}
  @category_occurrences = {}
  @total_elements = 0
  @total_phrases = 0;
end

Public Instance Methods

categories() click to toggle source
# File lib/nb_class/classifier.rb, line 21
def categories
  @category_examples.keys
end
category_probability(category) click to toggle source
# File lib/nb_class/classifier.rb, line 84
def category_probability(category)
  @category_occurrences[category].to_f / @total_phrases.to_f
end
category_probability_given_elements(params) click to toggle source
# File lib/nb_class/classifier.rb, line 88
def category_probability_given_elements(params)
  category = params[:category]
  elements = params[:elements]
  probability = 1.0
  elements.each do |element|
    element_probability_given_category = element_probability_given_category(category: category, element: element)
    element_global_probability = element_probability(element)
    element_probability = element_probability_given_category / element_global_probability
    probability *= element_probability
  end
  category_probability = category_probability(category)
  probability *= category_probability
  probability
end
classify(elements) click to toggle source
# File lib/nb_class/classifier.rb, line 39
def classify(elements)
  elements = Utils.break_phrase_in_word_array(elements)
  max_category = nil
  max_probability = 0.0
  @occurrences.keys.each do |key|
    probability = category_probability_given_elements(category: key,elements: elements)
    if probability > max_probability
      max_probability = probability
      max_category = key
    end
  end
  max_category
end
element_occurrence(params) click to toggle source
# File lib/nb_class/classifier.rb, line 53
def element_occurrence(params)
  category = params[:category]
  element = params[:element]
  if category
    @occurrences[category][element]
  else
    @global_occurrences[element]
  end
end
element_probability(element) click to toggle source
# File lib/nb_class/classifier.rb, line 75
def element_probability(element)
  element_occurrence = @global_occurrences[element]
  unless element_occurrence.nil?
    element_occurrence.to_f / @total_elements
  else
    SMALL_PROBABILITY
  end
end
element_probability_given_category(params) click to toggle source
# File lib/nb_class/classifier.rb, line 63
def element_probability_given_category(params)
  category = params[:category]
  element  = params[:element]
  element_occurrence_in_category = @occurrences[category][element]
  total_elements_in_category = @occurrences[category].map{ |item| item[1] }.inject{ |sum, item| sum + item }
  unless element_occurrence_in_category.nil?
    element_occurrence_in_category.to_f / total_elements_in_category.to_f
  else
    SMALL_PROBABILITY
  end
end
method_missing(name, *args, &block) click to toggle source
# File lib/nb_class/classifier.rb, line 103
def method_missing(name, *args, &block)
  @category_examples[name] = PhraseArray.new
  define_singleton_method name do
    @category_examples[name]
  end
  public_send(name)
end
train() click to toggle source
# File lib/nb_class/classifier.rb, line 25
def train
  @category_examples.keys.each do |category|
    @category_examples[category].each do |phrase|
      phrase.each do |word|
        count_element_occurrence_in_category(category,word)
        count_element_occurrence_in_global(word)
        @total_elements += 1
      end
      count_category_occurrence(category)
      @total_phrases += 1
    end
  end
end

Private Instance Methods

count_category_occurrence(category) click to toggle source
# File lib/nb_class/classifier.rb, line 132
def count_category_occurrence(category)
  if @category_occurrences[category].nil?
    @category_occurrences[category] = 1
  else
    @category_occurrences[category] += 1
  end
end
count_element_occurrence_in_category(category, element) click to toggle source
# File lib/nb_class/classifier.rb, line 113
def count_element_occurrence_in_category(category, element)
  unless @occurrences.has_key?(category)
    @occurrences[category] = {}
  end
  if !@occurrences[category].has_key?(element)
    @occurrences[category][element] = 1
  else
    @occurrences[category][element] += 1
  end
end
count_element_occurrence_in_global(element) click to toggle source
# File lib/nb_class/classifier.rb, line 124
def count_element_occurrence_in_global(element)
  unless @global_occurrences.has_key?(element)
    @global_occurrences[element] = 1
  else
    @global_occurrences[element] += 1
  end
end