Module: JLPT::Engine

Defined in:
lib/jlpt/core/engine.rb

Overview

Morphological Analysis Engine Wrapper for JLPT.

Uses MeCab (natto gem) if installed on system, with automatic fallback to a pure-Ruby morphological segmentation engine.

Defined Under Namespace

Modules: PureRubyFallback

Class Method Summary collapse

Class Method Details

.mecab_available?Boolean

Check if MeCab is available on host system

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
# File 'lib/jlpt/core/engine.rb', line 16

def mecab_available?
  return @mecab_available unless @mecab_available.nil?

  @mecab_available = begin
    require 'natto'
    !Natto::MeCab.new.nil?
  rescue LoadError, StandardError
    false
  end
end

.parse(text) ⇒ Array<Hash>

Parse Japanese text into structured token node hashes

Parameters:

  • text (String)

    Japanese input text

Returns:

  • (Array<Hash>)

    token nodes



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jlpt/core/engine.rb', line 31

def parse(text)
  return [] if text.nil? || text.to_s.strip.empty?

  cleaned = Preprocessor.clean(text)
  return [] if cleaned.empty?

  if mecab_available?
    parse_with_mecab(Natto::MeCab.new, cleaned)
  else
    parse_with_fallback(cleaned)
  end
end