class Natto::MeCabNode

`MeCabNode` is a wrapper for the `struct mecab_node_t` structure holding the parsed `node`.

Values for the MeCab node attributes may be obtained by using the following `Symbol`s as keys to the layout associative array of `FFI::Struct` members.

## Usage An instance of `MeCabNode` is yielded to the block used with `MeCab#parse`, where the above-mentioned node attributes may be accessed by name.

nm = Natto::MeCab.new

nm.parse('卓球なんて死ぬまでの暇つぶしだよ。') do |n| 
  puts "#{n.surface}\t#{n.cost}" if n.is_nor? 
end
卓球     2874
なんて    4398
死ぬ     9261
まで     9386
       10007
暇つぶし 13324
       15346
       14396
       10194

While it is also possible to use the `Symbol` for the MeCab node member to index into the `FFI::Struct` layout associative array, please use the attribute accessors. In the case of `:surface` and `:feature`, MeCab returns the raw bytes, so `natto` will convert that into a string using the default encoding.

Constants

BOS_NODE

Virtual node representing the beginning of the sentence, c.f. `stat`.

EON_NODE

Virtual node representing the end of an N-Best MeCab node list, c.f. `stat`.

EOS_NODE

Virutual node representing the end of the sentence, c.f. `stat`.

NOR_NODE

Normal MeCab node defined in the dictionary, c.f. `stat`.

UNK_NODE

Unknown MeCab node not defined in the dictionary, c.f. `stat`.

Attributes

feature[RW]

@return [String] corresponding feature value.

pointer[R]

@return [FFI::Pointer] pointer to MeCab node struct.

surface[RW]

@return [String] surface morpheme surface value.

Public Class Methods

new(nptr) click to toggle source

Initializes this node instance. Sets the MeCab feature value for this node. @param nptr [FFI::Pointer] pointer to MeCab node

Calls superclass method
# File lib/natto/struct.rb, line 245
def initialize(nptr)
  super(nptr)
  @pointer = nptr

  if self[:feature]
    @feature = self[:feature].force_encoding(Encoding.default_external)
  end
end

Public Instance Methods

inspect() click to toggle source

Overrides `Object#inspect`. @return [String] encoded object id, stat, surface, and feature @see to_s

# File lib/natto/struct.rb, line 274
def inspect
  self.to_s
end
is_bos?() click to toggle source

Returns `true` if this is a virtual MeCab node representing the beginning of the sentence. @return [Boolean]

# File lib/natto/struct.rb, line 292
def is_bos?
  self.stat == BOS_NODE
end
is_eon?() click to toggle source

Returns `true` if this is a virtual MeCab node representing the end of the node list. @return [Boolean]

# File lib/natto/struct.rb, line 304
def is_eon?
  self.stat == EON_NODE
end
is_eos?() click to toggle source

Returns `true` if this is a virtual MeCab node representing the end of the sentence. @return [Boolean]

# File lib/natto/struct.rb, line 298
def is_eos?
  self.stat == EOS_NODE 
end
is_nor?() click to toggle source

Returns `true` if this is a normal MeCab node found in the dictionary. @return [Boolean]

# File lib/natto/struct.rb, line 280
def is_nor?
  self.stat == NOR_NODE
end
is_unk?() click to toggle source

Returns `true` if this is an unknown MeCab node not found in the dictionary. @return [Boolean]

# File lib/natto/struct.rb, line 286
def is_unk?
  self.stat == UNK_NODE
end
to_s() click to toggle source

Returns human-readable details for the MeCab node. Overrides `Object#to_s`.

  • encoded object id

  • underlying FFI pointer to MeCab Node

  • stat (node type: NOR, UNK, BOS/EOS, EON)

  • surface

  • feature

@return [String] encoded object id, underlying FFI pointer, stat, surface, and feature

Calls superclass method
# File lib/natto/struct.rb, line 263
def to_s
   [ super.chop,
     "@pointer=#{@pointer},",
     "stat=#{self[:stat]},", 
     "@surface=\"#{self.surface}\",",
     "@feature=\"#{self.feature}\">" ].join(' ')
end