Class: TokenMap

Inherits:
Object
  • Object
show all
Defined in:
lib/libsvm_preprocessor/token_map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTokenMap

Returns a new instance of TokenMap.



5
6
7
8
# File 'lib/libsvm_preprocessor/token_map.rb', line 5

def initialize
  @hash_of_ngrams = {}
  @current_ngram_id = 0
end

Instance Attribute Details

#hash_of_ngramsObject (readonly)

Returns the value of attribute hash_of_ngrams.



3
4
5
# File 'lib/libsvm_preprocessor/token_map.rb', line 3

def hash_of_ngrams
  @hash_of_ngrams
end

Instance Method Details

#token_map(ary_of_ngrams, testing: false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/libsvm_preprocessor/token_map.rb', line 10

def token_map(ary_of_ngrams, testing: false)
  if !testing
    ary_of_ngrams.each { |ngram| @hash_of_ngrams[ngram] ||= next_ngram_id }
    ary_of_ngrams.map { |ngram| { @hash_of_ngrams[ngram] => ngram } }
  else
    ary_of_ngrams.map do |ngram|
      { @hash_of_ngrams[ngram] => ngram }
    end.select do |hash|
      hash.keys.first
    end
  end

end