Class: VivlioStarter::CLI::IndexCommands::YomiInferrer
- Inherits:
-
Object
- Object
- VivlioStarter::CLI::IndexCommands::YomiInferrer
- Defined in:
- lib/vivlio_starter/cli/index/yomi_inferrer.rb
Overview
MeCab による読み推測クラス
Instance Method Summary collapse
-
#available? ⇒ Boolean
MeCab が利用可能かどうか book.yml の index_glossary.use_mecab: false は「導入済みでも使わない」という 著者の明示的な選択なので、導入案内の警告は出さずに静かに無効化する.
-
#infer(text) ⇒ String
テキストの読みを推測.
-
#initialize ⇒ YomiInferrer
constructor
A new instance of YomiInferrer.
Constructor Details
#initialize ⇒ YomiInferrer
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 が利用可能かどうか book.yml の index_glossary.use_mecab: false は「導入済みでも使わない」という 著者の明示的な選択なので、導入案内の警告は出さずに静かに無効化する
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/vivlio_starter/cli/index/yomi_inferrer.rb', line 64 def available? return @available unless @available.nil? return @available = false if Common::CONFIG&.index_glossary&.use_mecab == false @available = begin require 'natto' # MeCab の初期化を試行 @mecab = Natto::MeCab.new true rescue LoadError => e Common.log_warn("natto gem がインストールされていません: #{e.}") Common.log_warn('索引機能では MeCab による読み推測が利用できません') Common.log_warn('gem install natto を実行してください') false rescue StandardError => e Common.log_warn("MeCab の初期化に失敗しました: #{e.}") 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
テキストの読みを推測
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/vivlio_starter/cli/index/yomi_inferrer.rb', line 33 def infer(text) # 読みの個人辞書(import で蓄積)を MeCab より優先する。 override = overrides[text] return override if override 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 |