Class: FisherClassifier::Classifier

Inherits:
Object
  • Object
show all
Includes:
Meta
Defined in:
lib/fisher_classifier/classifier.rb

Instance Method Summary collapse

Methods included from Meta

#meta_classify

Constructor Details

#initialize(config) ⇒ Classifier

Returns a new instance of Classifier.



6
7
8
# File 'lib/fisher_classifier/classifier.rb', line 6

def initialize(config)
  @config = config
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *values, &block) ⇒ Object (private)



97
98
99
100
101
102
103
# File 'lib/fisher_classifier/classifier.rb', line 97

def method_missing(key, *values, &block)
  if @config.respond_to?(key, values)
    @config.call key, *values
  else
    @config.get(key)
  end
end

Instance Method Details

#classify(text) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fisher_classifier/classifier.rb', line 17

def classify(text)
  features = get_features(text)
  best = default_category
  max = fisher_threshold

  categories.each do |category|
    prob = fisher_prob(category, features)

    if prob > max
      best = category
      max = prob
    end
  end

  best
end

#train(text, category) ⇒ Object



10
11
12
13
14
15
# File 'lib/fisher_classifier/classifier.rb', line 10

def train(text, category)
  get_features(text).each do |feature|
    inc_feature(feature, category)
    inc_category(category)
  end
end