Class: GuessWhoNoFuzzy::Tokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/guess_who_no_fuzzy/tokenizer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Tokenizer

Returns a new instance of Tokenizer.



7
8
9
# File 'lib/guess_who_no_fuzzy/tokenizer.rb', line 7

def initialize(str)
  @str = str
end

Class Method Details

.tokenize!(str) ⇒ Object



3
4
5
# File 'lib/guess_who_no_fuzzy/tokenizer.rb', line 3

def self.tokenize!(str)
  self.new(str).tokenize!
end

Instance Method Details

#tokenize!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/guess_who_no_fuzzy/tokenizer.rb', line 11

def tokenize!
  token_arrays = []
  str_size = @str.size

  str_size.times do |i|
    first_slice = i + 1
    first_name = @str.slice(0, first_slice)

    (str_size - i).times do |j|
      mid_slice = j + 1
      mid_name = @str.slice(first_slice, mid_slice)

      last_slice = first_slice + mid_slice
      last_name = @str.slice(last_slice...str_size)

      token_array = [first_name]
      token_array << mid_name unless blank?(mid_name)
      token_array << last_name unless blank?(last_name)
      token_arrays << token_array
    end
  end

  token_arrays
end