class OmniCat::Classifier

Attributes

strategy[RW]

classification strategy

Public Class Methods

new(classifier) click to toggle source

nodoc

# File lib/omnicat/classifier.rb, line 25
def initialize(classifier)
  @strategy = classifier
end

Public Instance Methods

strategy=(classifier) click to toggle source

Changes classifier strategy and train new strategy if needed

# File lib/omnicat/classifier.rb, line 31
def strategy=(classifier)
  is_interchangeable?(classifier)
  if @strategy && classifier.category_count == 0
    previous_strategy = @strategy
    @strategy = classifier
    convert_categories_with_docs(previous_strategy)
  else
    @strategy = classifier
  end
end

Private Instance Methods

convert_categories_array(categories) click to toggle source

nodoc

# File lib/omnicat/classifier.rb, line 53
def convert_categories_array(categories)
  categories.each do |category|
    convert_category(category)
  end
end
convert_categories_hash(categories) click to toggle source

nodoc

# File lib/omnicat/classifier.rb, line 60
def convert_categories_hash(categories)
  categories.each do |_, category|
    convert_category(category)
  end
end
convert_categories_with_docs(previous_strategy) click to toggle source

nodoc

# File lib/omnicat/classifier.rb, line 44
def convert_categories_with_docs(previous_strategy)
  if previous_strategy.categories.is_a?(Hash)
    convert_categories_hash(previous_strategy.categories)
  else
    convert_categories_array(previous_strategy.categories)
  end
end
convert_category(category) click to toggle source

nodoc

# File lib/omnicat/classifier.rb, line 67
def convert_category(category)
  @strategy.add_category(category.name)
  if category.docs.is_a?(Hash)
    convert_docs_hash(category.name, category.docs)
  else
    convert_docs_array(category.name, category.docs)
  end
end
convert_doc(category_name, doc) click to toggle source

nodoc

# File lib/omnicat/classifier.rb, line 91
def convert_doc(category_name, doc)
  doc.count.times do
    @strategy.train(category_name, doc.content)
  end
end
convert_docs_array(category_name, docs) click to toggle source

nodoc

# File lib/omnicat/classifier.rb, line 77
def convert_docs_array(category_name, docs)
  docs.each do |doc|
    convert_doc(category_name, doc)
  end
end
convert_docs_hash(category_name, docs) click to toggle source

nodoc

# File lib/omnicat/classifier.rb, line 84
def convert_docs_hash(category_name, docs)
  docs.each do |_, doc|
    convert_doc(category_name, doc)
  end
end
is_interchangeable?(classifier) click to toggle source

nodoc

# File lib/omnicat/classifier.rb, line 98
def is_interchangeable?(classifier)
  unless classifier.category_size_limit == 0
    if @strategy.category_count > classifier.category_size_limit
      raise StandardError,
        'New classifier category size limit is less than the current classifier\'s category count.'
    end
  end
end