Class: Vivlio::Starter::CLI::IndexCommands::YomiInferrer

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio/starter/cli/index/yomi_inferrer.rb

Overview

MeCab による読み推測クラス

Instance Method Summary collapse

Constructor Details

#initializeYomiInferrer

Returns a new instance of YomiInferrer.



25
26
27
28
# File 'lib/vivlio/starter/cli/index/yomi_inferrer.rb', line 25

def initialize
  @mecab = nil
  @available = nil
end

Instance Method Details

#available?Boolean

MeCab が利用可能かどうか

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vivlio/starter/cli/index/yomi_inferrer.rb', line 58

def available?
  return @available unless @available.nil?

  @available = begin
    require 'natto'
    # MeCab の初期化を試行
    @mecab = Natto::MeCab.new
    true
  rescue LoadError => e
    Common.log_warn("natto gem がインストールされていません: #{e.message}")
    Common.log_warn('索引機能では MeCab による読み推測が利用できません')
    Common.log_warn('gem install natto を実行してください')
    false
  rescue StandardError => e
    Common.log_warn("MeCab の初期化に失敗しました: #{e.message}")
    Common.log_warn('MeCab がシステムにインストールされているか確認してください')
    Common.log_warn('macOS: brew install mecab mecab-ipadic')
    Common.log_warn('Ubuntu: sudo apt-get install mecab libmecab-dev mecab-ipadic-utf8')
    false
  end
end

#infer(text) ⇒ String

テキストの読みを推測

Parameters:

  • text (String)

    読みを推測するテキスト

Returns:

  • (String)

    ひらがなの読み(推測できない場合は元のテキスト)



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vivlio/starter/cli/index/yomi_inferrer.rb', line 33

def infer(text)
  return text unless available?

  yomi_parts = []

  mecab.parse(text) do |node|
    next if node.is_eos?

    # 読み情報を取得(feature の 8 番目 = カタカナ読み)
    features = node.feature.split(',')
    reading = features[7] if features.size > 7

    yomi_parts << if reading && reading != '*' && !reading.empty?
                    katakana_to_hiragana(reading)
                  else
                    # 読みが取得できない場合は表層形をそのまま使用
                    node.surface
                  end
  end

  result = yomi_parts.join
  result.empty? ? text : result
end