mecab-pythonをMac OSX 10.7 Lion、Python2.7にインストールする

入門 自然言語処理

入門 自然言語処理

苦節一月半、mecab-pythonをMac OSX 10.7 Lion, Python2.7にインストールできました。

時系列で載せます。

まず、Homebrewでmecabと辞書をインストールした(後にアンインストールした)。

以下、mecab-python-0.991との戦いの記録

mecab-python-0.991 r_onodr$ ARCHFLAGS='-arch i386 -arch x86_64' python setup.py build

lipo: can't figure out the architecture type of: /var/folders/mq/pxbw7qg97994xjhnhf444dr00000gn/T//ccAJndUU.out
error: command 'gcc-4.2' failed with exit status 1

エラー。なんか、gcc-4.2ってヤツが真面目に働く気がないようだ。

代わりを探す。

mecab-python-0.991 r_onodr$ which gcc
/usr/bin/gcc

gccさんにがんばってもらう。

mecab-python-0.991 r_onodr$ CC=/usr/bin/gcc python setup.py build
...
ld: warning: ignoring file /usr/local/Cellar/mecab/0.99/lib/libmecab.dylib, file was built for unsupported file format which is not the architecture being linked (i386)

「お!」と思ったけどまたエラー。

i386?32bit??」って思って、色々試す(要らなかったかも…)。

mecab-python-0.991 r_onodr$ CC=/usr/bin/gcc ARCHFLAGS='-arch i386' python setup.py build
mecab-python-0.991 r_onodr$ CC=/usr/bin/gcc CFLAGS='-m32' python setup.py build
...
ld: warning: ignoring file /usr/local/Cellar/mecab/0.99/lib/libmecab.dylib, file was built for unsupported file format which is not the architecture being linked (i386)

結局同じエラー。

どうやら、Homebrewでインストールしたmecab本体が32bitなのが悪いっぽい。
というわけで、アンインストール。

~ r_onodr$ brew uninstall mecab
~ r_onodr$ brew uninstall mecab-ipadic

そして、mecabと辞書をダウンロードしてきて64bitでインストール!

mecab-0.993 r_onodr$ CC="gcc -m64" CXX="g++ -m64" ./configure --with-charset=utf8
mecab-0.993 r_onodr$ make
mecab-0.993 r_onodr$ sudo make install

mecab-ipadic-2.7.0-20070801 r_onodr$ CC="gcc -m64" CXX="g++ -m64" ./configure --with-charset=utf8
mecab-ipadic-2.7.0-20070801 r_onodr$ make
mecab-ipadic-2.7.0-20070801 r_onodr$ make install

mecab-python-0.991 r_onodr$ python setup.py build
mecab-python-0.991 r_onodr$ python setup.py install

通った!!

テスト

~ r_onodr$ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MeCab
>>> m = MeCab.Tagger("-Ochasen")
>>> str = "八頭身モナーはキモい"
>>> dst = m.parse(str)
>>> n = m.parseToNode(str)
>>> print dst
八頭身	ハットウシン	八頭身	名詞-一般		
モナー	モナー	モナー	名詞-一般		
は	ハ	は	助詞-係助詞		
キモ	キモ	キモ	名詞-固有名詞-一般		
い	イ	い	名詞-一般		
EOS

>>> while n:
...     print n.surface + " " + n.feature.split(",")[0]
...     n = n.next
... 
 BOS/EOS
八頭身 名詞
モナー 名詞
は 助詞
キモ 名詞
い 名詞
 BOS/EOS
>>> 

わーい♪


【参考になったページ】
MeCab のインストール | 自宅サーバFedora http://honana.com/mysql/tritonn/mecab.html
OS X Lion, make/configure issue, trying to compile 64-bit libFLAC/libFLAC++ - Ars Technica OpenForum http://arstechnica.com/civis/viewtopic.php?f=20&t=1164408
Rob Hess氏によるSIFTの実装をOSX SnowLeopardにインストール. | GO HOME http://kzm42.blog24.fc2.com/blog-entry-31.html
éternuement: Snow Leopardで32bitビルドの仕方 http://eternuement.blogspot.com/2011/02/snow-leopard32bit.html
Work-around for Mac OS X python package install error ― "lipo: can’t figure out the architecture type" | post past :: james murty http://www.jamesmurty.com/2011/01/29/work-around-osx-lipo-figure-out-architecture-type/
python - Why is GCC ignoring ARCHFLAGS in Snow Leopard? - Stack Overflow http://stackoverflow.com/questions/6988528/why-is-gcc-ignoring-archflags-in-snow-leopard

入門 ソーシャルデータ ―データマイニング、分析、可視化のテクニック

入門 ソーシャルデータ ―データマイニング、分析、可視化のテクニック