Class: HiwaiKaeuta::Mecab::Nodes

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hiwai_kaeuta/mecab/nodes.rb

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Nodes

Returns a new instance of Nodes.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hiwai_kaeuta/mecab/nodes.rb', line 9

def initialize(text)
  raw_node = MeCab::Tagger.new.parseToNode(text)
  @nodes = [].tap do |nodes|
    location = 0
    while raw_node
      node = Node.new(raw_node)
      unless node.surface.empty?
        location = text.index(node.surface, location)
      end
      node.location = location
      nodes << node
      raw_node = raw_node.next
    end
  end
  @nodes.each_with_index { |node, index| node.index = index }
end