Sha256: 0310a88aa78828196a5d5fee34a793a2d5c2a7874d7c2706199ccf0ea308d8cf

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

require File.dirname(__FILE__) + '/classifiers/strategy'
require File.dirname(__FILE__) + '/classifiers/strategy_internals/category'
require 'forwardable'

module OmniCat
  class Classifier
    extend Forwardable

    # classification strategy
    attr_accessor :strategy

    # delegate category methods
    def_delegators :@strategy, :add_category, :add_categories

    # delegate training methods
    def_delegators :@strategy, :train, :train_batch, :untrain, :untrain_batch

    # delegate classification methods
    def_delegators :@strategy, :classify, :classify_batch

    # delegate base methods
    def_delegator :@strategy, :to_hash

    # nodoc
    def initialize(classifier)
      @strategy = classifier
    end

    def strategy=(classifier)
      is_interchangeable?(classifier)
      if @strategy && classifier.doc_count == 0
        previous_strategy = @strategy
        @strategy = classifier
        # pass previous strategy contents into the new one
        previous_strategy.categories.each do |category_name, category|
          @strategy.add_category(category_name)
          category.docs.each do |_, doc|
            doc.count.times do
              @strategy.train(category_name, doc.content)
            end
          end
        end
      else
        @strategy = classifier
      end
    end

    private
      def is_interchangeable?(classifier)
        if classifier.category_size_limit
          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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
omnicat-0.2.2 lib/omnicat/classifier.rb
omnicat-0.2.1 lib/omnicat/classifier.rb
omnicat-0.2.0 lib/omnicat/classifier.rb