Module: Dawg::Finder

Included in:
Dawg, MemoryDawg
Defined in:
lib/dawg/finder.rb

Instance Method Summary collapse

Instance Method Details

#get_childs(node) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dawg/finder.rb', line 39

def get_childs(node)
  results = []
  node.each_edge do |letter|
    next_node = node[letter]
    if next_node != nil
      results += get_childs(next_node).map{|s| Word.new(letter) + s}
      results << Word.new(letter, next_node.final)
    end
  end
  results
end

#lookup(word) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dawg/finder.rb', line 7

def lookup(word)
  node = @the_node
  word.each_char do |letter|
    next_node = node[letter]
    if next_node != nil
      node = next_node
      next
    else
      return false
    end
  end
  node.final
end

#query(word) ⇒ Object

get all words with given prefix


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dawg/finder.rb', line 22

def query(word)
  node = @the_node
  results = []
  word.split("").each do |letter|
    next_node = node[letter]
    if next_node != nil
      node = next_node
      next
    else
      return ['']
    end
  end
  results << Word.new(word, node.final)
  results += get_childs(node).map{|s| Word.new(word) + s}
  results.select{|r| r.final}.map{|r| r.to_s }
end

#set_the_node(node) ⇒ Object


3
4
5
# File 'lib/dawg/finder.rb', line 3

def set_the_node(node)
  @the_node = node
end