Class: GuessWho::Tokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/guess_who/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/tokenizer.rb', line 7

def initialize(str)
  @raw_str = str
end

Class Method Details

.tokenize!(str) ⇒ Object



3
4
5
# File 'lib/guess_who/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
# File 'lib/guess_who/tokenizer.rb', line 11

def tokenize!
  tokens = []

  (0..@raw_str.size-1).each do |i|
    str = @raw_str.clone
    possible_firstname = str.slice(0..i)

    (possible_firstname.length..str.length).each do |j|
      combination = str.scan(/(?=(#{possible_firstname})([a-zA-Z]{,#{j}})([a-zA-Z]*))/)
      combination = combination.flatten.reject(&:empty?)
      tokens << combination unless combination.empty?
    end
  end

  return tokens.uniq
end