Class: LightMecab::Morpheme

Inherits:
Object
  • Object
show all
Defined in:
lib/light-mecab.rb

Constant Summary collapse

@@i18n =
::YAML.load_file(File.expand_path(File.join(__FILE__, '..', 'locale', 'morpheme.yml')))

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Morpheme

Returns a new instance of Morpheme.

Parameters:

  • text (String)


32
33
34
# File 'lib/light-mecab.rb', line 32

def initialize(text)
  @nodes = self.class.analyze(text)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object

Parameters:

  • method_name (Symbol)


42
43
44
45
46
47
# File 'lib/light-mecab.rb', line 42

def method_missing(method_name)
  if !self.class.i18n(method_name)
    raise NoMethodError
  end
  extract(self.class.i18n(method_name))
end

Class Method Details

.analyze(text) ⇒ Array <MeCab::Node>

Parameters:

  • text (String)

Returns:

  • (Array <MeCab::Node>)


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/light-mecab.rb', line 12

def analyze(text)
  nodes = []
  node = ::MeCab::Tagger.new.parseToNode(text)
  while node
    nodes << node
    node = node.next
  end
  nodes.shift
  nodes.pop
  nodes
end

.i18n(key) ⇒ String

Parameters:

  • key (String or Symbol)

Returns:

  • (String)


26
27
28
# File 'lib/light-mecab.rb', line 26

def i18n(key)
  key.is_a?(Symbol) ? @@i18n[key.to_s] : @@i18n[key]
end

Instance Method Details

#countInteger

Returns:

  • (Integer)


37
38
39
# File 'lib/light-mecab.rb', line 37

def count
  @nodes.size
end