Class: HarmoniousDictionary::RsegEngine::Dict

Inherits:
Engine
  • Object
show all
Defined in:
lib/harmonious_dictionary/engines/dict.rb

Instance Method Summary collapse

Methods inherited from Engine

#run, #running?, #stop

Constructor Details

#initialize(&block) ⇒ Dict

Returns a new instance of Dict.



4
5
6
7
8
# File 'lib/harmonious_dictionary/engines/dict.rb', line 4

def initialize(&block)
  @dict_path = block.call
  @word = ''
  super
end

Instance Method Details

#dictionaryObject



10
11
12
# File 'lib/harmonious_dictionary/engines/dict.rb', line 10

def dictionary
  @@root
end

#process(char) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/harmonious_dictionary/engines/dict.rb', line 14

def process(char)
  @root ||= load_dict(@dict_path)
  @node ||= @root

  match = false
  word = nil
  
  if @node[char]
    @word << char
    @node = @node[char]
    match = true
  else
    if @node[:end] || @word.chars.to_a.length == 1
      word = @word
    else
      word = @word.chars.to_a
    end
  
    @node = @root
    @word = ''
    match = false
  end
  [match, word]
end