Module: Ankusa::Classifier

Included in:
KLDivergenceClassifier, NaiveBayesClassifier
Defined in:
lib/ankusa/classifier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#classnamesObject (readonly)

Returns the value of attribute classnames.



4
5
6
# File 'lib/ankusa/classifier.rb', line 4

def classnames
  @classnames
end

Instance Method Details

#initialize(storage) ⇒ Object



6
7
8
9
10
# File 'lib/ankusa/classifier.rb', line 6

def initialize(storage)
  @storage = storage
  @storage.init_tables
  @classnames = @storage.classnames
end

#train(klass, text) ⇒ Object

text can be either an array of strings or a string klass is a symbol



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ankusa/classifier.rb', line 14

def train(klass, text)
  th = TextHash.new(text)
  th.each { |word, count|
    @storage.incr_word_count klass, word, count
    yield word, count if block_given?
  }
  @storage.incr_total_word_count klass, th.word_count
  doccount = (text.kind_of? Array) ? text.length : 1
  @storage.incr_doc_count klass, doccount
  @classnames << klass unless @classnames.include? klass
  # cache is now dirty of these vars
  @doc_count_totals = nil
  @vocab_sizes = nil
  th
end

#untrain(klass, text) ⇒ Object

text can be either an array of strings or a string klass is a symbol



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ankusa/classifier.rb', line 32

def untrain(klass, text)
  th = TextHash.new(text)
  th.each { |word, count|
    @storage.incr_word_count klass, word, -count
    yield word, count if block_given?
  }
  @storage.incr_total_word_count klass, -th.word_count
  doccount = (text.kind_of? Array) ? text.length : 1
  @storage.incr_doc_count klass, -doccount
  # cache is now dirty of these vars
  @doc_count_totals = nil
  @vocab_sizes = nil
  th
end