Mac + Python + MeCab
プロジェクトがやや前倒しで進んでいるにも関わらず眠れないので、ちょっとpythonでmecabを使うとどんな感じか試してみた。
python25が入っていることを前提としてメモ。
参考: http://d.hatena.ne.jp/aircastle/20080925/1222269708
まずはMacPortでMeCabをInstall.
sudo port install py25-mecab
それからutf-8な辞書をInstall.(ターミナルもutf-8だから合わせといたほうが都合がよい)
sudo port install mecab-ipadic-utf8
このままだとeucだかsjisだかjisだかの辞書を使ってるようなので、設定を書き換えて先ほどInstallしたutf-8辞書を使うようにしてやる。
sudo vi /opt/local/etc/mecabrc
変更前: /opt/local/etc/mecabrc
; ; Configuration file of MeCab ; ; $Id: mecabrc.in,v 1.3 2006/05/29 15:36:08 taku-ku Exp $; ; dicdir = /opt/local/lib/mecab/dic/ipadic ; userdic = /home/foo/bar/user.dic ; output-format-type = wakati ; input-buffer-size = 8192 ; node-format = %m\n ; bos-format = %S\n ; eos-format = EOS\n
変更後: /opt/local/etc/mecabrc
; ; Configuration file of MeCab ; ; $Id: mecabrc.in,v 1.3 2006/05/29 15:36:08 taku-ku Exp $; ; ;dicdir = /opt/local/lib/mecab/dic/ipadic dicdir = /opt/local/lib/mecab/dic/ipadic-utf8 ; userdic = /home/foo/bar/user.dic ; output-format-type = wakati ; input-buffer-size = 8192 ; node-format = %m\n ; bos-format = %S\n ; eos-format = EOS\n
これでutf-8な辞書を使ってくれるようになったはずなので試してみる。
[malan:/Users/malan/test]$ python Python 2.5.2 (r252:60911, Oct 23 2008, 02:54:51) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import MeCab >>> mecab = MeCab.Tagger() >>> print mecab.parse("ほげほげテスト") ほ 動詞,自立,*,*,五段・ラ行,体言接続特殊2,ほる,ホ,ホ げ 名詞,接尾,一般,*,*,*,げ,ゲ,ゲ ほ 動詞,自立,*,*,五段・ラ行,体言接続特殊2,ほる,ホ,ホ げ 名詞,接尾,一般,*,*,*,げ,ゲ,ゲ テスト 名詞,サ変接続,*,*,*,*,テスト,テスト,テスト EOS
このままだと「ほげ」が単語として認識されなくて悲しいけど、とりあえず文字化けせずに表示されていればおk。