RailsアプリからEUC-JPエンコードされた辞書を持つMeCabを使う方法
通常RailsアプリからMeCabを利用するには、MeCab側でUTF-8の辞書を選択すると思います。
しかしHyperEstraierとMeCabを連携させる為には、MeCab辞書がEUC-JPエンコーディングである必要があります。
そこで、RailsアプリからEUC-JP環境のMeCabを使う為に、以下のようなフィルタを定義しました。
-
require 'nkf'
-
require 'MeCab'
-
-
module MeCab
-
class Tagger
-
def parseToNode_with_eucjp_to_utf8(text)
-
text = NKF.nkf("-W -e", text)
-
parseToNode_without_eucjp_to_utf8(text)
-
end
-
alias_method_chain :parseToNode, :eucjp_to_utf8
-
end
-
class Node
-
def surface_with_eucjp_to_utf8
-
NKF.nkf("-E -w", surface_without_eucjp_to_utf8)
-
end
-
alias_method_chain :surface, :eucjp_to_utf8
-
def feature_with_eucjp_to_utf8
-
NKF.nkf("-E -w", feature_without_eucjp_to_utf8)
-
end
-
alias_method_chain :feature, :eucjp_to_utf8
-
end
-
end
これで
・MeCabに単語を入力する際には「UTF-8 => EUC-JP」変換
・MeCabから結果が出力された際には「EUC-JP => UTF-8」変換
というフィルタができます。
ps.
これでalias_method_chainの使い方を覚えた。











