Class: Okura::WordDic::Naive::TrieNode
- Inherits:
-
Object
- Object
- Okura::WordDic::Naive::TrieNode
- Defined in:
- lib/okura/word_dic.rb
Instance Method Summary collapse
- #add(word, i = 0) ⇒ Object
- #find_all(str, i, res = Array.new) ⇒ Object
-
#initialize ⇒ TrieNode
constructor
A new instance of TrieNode.
Constructor Details
#initialize ⇒ TrieNode
Returns a new instance of TrieNode.
9 10 11 12 |
# File 'lib/okura/word_dic.rb', line 9 def initialize @nodes={} @leafs=[] end |
Instance Method Details
#add(word, i = 0) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/okura/word_dic.rb', line 13 def add word,i=0 if i==word.surface.length @leafs.push word else fst=word.surface[i] node=@nodes[fst] @nodes[fst]=node=TrieNode.new if node.nil? node.add word,i+1 end end |
#find_all(str, i, res = Array.new) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/okura/word_dic.rb', line 23 def find_all str,i,res=Array.new res.concat @leafs return res unless i < str.length node=@nodes[str[i]] return res if node.nil? node.find_all(str,i+1,res) res end |