Mac Lion と CentOS と Ubuntu に MeCab をインストールする方法

日本語形態素解析をすることになりました。Yahoo の日本語形態素解析を昔使っていましたが以下の制限があるため、今回私が作りたい Web サイトの目的を達成することができませんでした。

  • 24時間以内で50000件のリクエストが上限
  • 1リクエストの最大サイズを100KBに制限

分割する手もありますが、分割するためのプログラムを書くより MeCab をサーバーにインストールする方が目的を達成するのに速いのと、リクエスト制限というものが無いので1日にたくさんのタスクを処理できると思ったので MeCab を使うことにしました。

ChaSen と MeCab で少し悩みましたが。技術メモ的なモノと気になるモノさんのサイトによると。

形態素解析で日本語をごにょごにょしたいので、mecabを入れてみる。従来はchasenとかを使っていたんだけど、こちらの方がメモリ効率がいいという話を聞いたので試してみる。

とのことでしたので、MeCab でチャレンジしてみることにしました。あと、Yahoo 日本語形態素解析と MeCab だとどれくらい違うのか若干気になったのですが、真夜中のプログラミングTipsさんで検証されています。

ドラゴンボールの例も同様ですが、どちらかといえばMeCabは機械的に区切る傾向が、Yahoo!APIは空気を読んで区切る傾向があるようです。

今回のインストールにあたって、以下のサイトを参考にさせて頂きました。freefielder.jp さんありがとうございました。

追記: この記事の方法で Mac Lion と CentOS と Ubuntu へのインストールに成功しましたので、Mac 限定からタイトルを変更しました。

必要なファイルをダウンロード

まず MeCab の本体と辞書ファイルをダウンロードしてきます。辞書については IPA 辞書が推奨と書いてありましたので、今回は悩まず IPA 辞書をダウンロードしてみます。以下2ファイルになります。

  • mecab-0.98.tar.gz
  • mecab-ipadic-2.7.0-20070801.tar.gz

以下のページからダウンロードすることができます。

MeCab のインストール

UTF-8 で動かしますので、UTF-8のみでインストールをします。mecab-0.98 を解凍して、フォルダに移動して以下のコマンドを実行します。

1
./configure --enable-utf8-only

すると以下のようなエラーが出てしまいました。

1
2
3
4
5
6
7
8
9
10
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... ./install-sh -c -d
checking for gawk... gawk
checking whether make sets $(MAKE)... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/Users/hoge/Desktop/mecab-0.98':
configure: error: no acceptable C compiler found in $PATH

以下のサイトは Git のインストールですが、インストール途中で同じエラーが出ているようです。

要は cc や gcc を使えるようにするには Mac に Xcode というのをインストールすれば良いのですが、それについては私も Git の時に既にやっています。どうしたもんかと調べていたら、そういえば最近 Lion にアップグレードしたことを思い出しました。それで調べてみると思ったとおり、Lion からは cc や gcc が /Developer/usr/bin に移動しています。

そのため /Developer/usr/bin に対してパスを通す必要があります。

1
vi ~/.zshrc

以下のような感じで追加しておきます。

1
export PATH=/opt/local/bin:/opt/local/sbin:/Developer/usr/bin:$PATH

そしてもう一度チャレンジします。

1
./configure --enable-utf8-only

すると今度は以下のようなエラーが。

1
2
3
4
5
6
7
8
9
10
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... ./install-sh -c -d
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output file name... 
configure: error: in `/Users/hoge/Desktop/mecab-0.98':
configure: error: C compiler cannot create executables
See `config.log' for more details.

今後は違うエラーが出ましたので、config.log を見てみます。ちなみに config.log は ./configure を実行したディレクトリにあります。そして見たところコンパイラが上記のエラー通り、実行可能ファイルを作成できないみたいだったので、さらに検索して調べていると以下のような記事を発見しました。

Leopard にしたら gcc を 4.0 にしないといけないようで、Xcode もバージョンアップ必要。

もしかしたら Lion にした今回も Xcode のバージョンアップが必要かもしれないと思いましたので Xcode のバージョンアップを行います。バージョンアップについては Xcode のインストールと同じ手順ですので以下のサイトが参考になります。Xcode は Store からダウンロードできるようになっていますので手に入れるのが楽です。ただ Xcode のバージョンアップには時間がかかります。

そして次の日、Xcode のバージョンアップが完了したのでチャレンジ。

1
./configure --enable-utf8-only

すると以下のような結果になりました。やはり Lion にバージョンアップしたことが原因だという読みは正解だったようです。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... ./install-sh -c -d
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables... 
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... none
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking dependency style of g++... none
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking whether gcc needs -traditional... no
checking whether make sets $(MAKE)... (cached) yes
checking build system type... i386-apple-darwin11.1.0
checking host system type... i386-apple-darwin11.1.0
checking for a sed that does not truncate output... /opt/local/bin/gsed
checking for fgrep... /usr/bin/grep -F
checking for ld used by gcc... /Developer/usr/llvm-gcc-4.2/libexec/gcc/i686-apple-darwin11/4.2.1/ld
checking if the linker (/Developer/usr/llvm-gcc-4.2/libexec/gcc/i686-apple-darwin11/4.2.1/ld) is GNU ld... no
checking for BSD- or MS-compatible name lister (nm)... /Developer/usr/bin/nm
checking the name lister (/Developer/usr/bin/nm) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 196608
checking whether the shell understands some XSI constructs... yes
checking whether the shell understands "+="... yes
checking for /Developer/usr/llvm-gcc-4.2/libexec/gcc/i686-apple-darwin11/4.2.1/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for ar... ar
checking for strip... strip
checking for ranlib... ranlib
checking command to parse /Developer/usr/bin/nm output from gcc object... ok
checking for dsymutil... dsymutil
checking for nmedit... nmedit
checking for lipo... lipo
checking for otool... otool
checking for otool64... no
checking for -single_module linker flag... yes
checking for -exported_symbols_list linker flag... yes
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking whether we are using the GNU C++ compiler... (cached) yes
checking whether g++ accepts -g... (cached) yes
checking dependency style of g++... (cached) none
checking how to run the C++ preprocessor... g++ -E
checking for objdir... .libs
checking if gcc supports -fno-rtti -fno-exceptions... no
checking for gcc option to produce PIC... -fno-common -DPIC
checking if gcc PIC flag -fno-common -DPIC works... yes
checking if gcc static flag -static works... no
checking if gcc supports -c -o file.o... yes
checking if gcc supports -c -o file.o... (cached) yes
checking whether the gcc linker (/Developer/usr/llvm-gcc-4.2/libexec/gcc/i686-apple-darwin11/4.2.1/ld) supports shared libraries... yes
checking dynamic linker characteristics... darwin11.1.0 dyld
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes
checking for ld used by g++... /Developer/usr/llvm-gcc-4.2/libexec/gcc/i686-apple-darwin11/4.2.1/ld
checking if the linker (/Developer/usr/llvm-gcc-4.2/libexec/gcc/i686-apple-darwin11/4.2.1/ld) is GNU ld... no
checking whether the g++ linker (/Developer/usr/llvm-gcc-4.2/libexec/gcc/i686-apple-darwin11/4.2.1/ld) supports shared libraries... yes
checking for g++ option to produce PIC... -fno-common -DPIC
checking if g++ PIC flag -fno-common -DPIC works... yes
checking if g++ static flag -static works... no
checking if g++ supports -c -o file.o... yes
checking if g++ supports -c -o file.o... (cached) yes
checking whether the g++ linker (/Developer/usr/llvm-gcc-4.2/libexec/gcc/i686-apple-darwin11/4.2.1/ld) supports shared libraries... yes
checking dynamic linker characteristics... darwin11.1.0 dyld
checking how to hardcode library paths into programs... immediate
checking for library containing strerror... none required
checking whether byte ordering is bigendian... no
checking host system type... (cached) i386-apple-darwin11.1.0
checking for ld used by GCC... /Developer/usr/llvm-gcc-4.2/libexec/gcc/i686-apple-darwin11/4.2.1/ld
checking if the linker (/Developer/usr/llvm-gcc-4.2/libexec/gcc/i686-apple-darwin11/4.2.1/ld) is GNU ld... no
checking for shared library run path origin... done
checking for iconv... yes
checking for working iconv... yes
checking how to link with libiconv... -liconv
checking for iconv declaration... 
         extern size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);
checking for ANSI C header files... (cached) yes
checking for an ANSI C-conforming const... yes
checking whether byte ordering is bigendian... (cached) no
checking for string.h... (cached) yes
checking for stdlib.h... (cached) yes
checking for unistd.h... (cached) yes
checking fcntl.h usability... yes
checking fcntl.h presence... yes
checking for fcntl.h... yes
checking for sys/stat.h... (cached) yes
checking sys/mman.h usability... yes
checking sys/mman.h presence... yes
checking for sys/mman.h... yes
checking sys/times.h usability... yes
checking sys/times.h presence... yes
checking for sys/times.h... yes
checking for sys/types.h... (cached) yes
checking dirent.h usability... yes
checking dirent.h presence... yes
checking for dirent.h... yes
checking ctype.h usability... yes
checking ctype.h presence... yes
checking for ctype.h... yes
checking for sys/types.h... (cached) yes
checking io.h usability... no
checking io.h presence... no
checking for io.h... no
checking windows.h usability... no
checking windows.h presence... no
checking for windows.h... no
checking pthread.h usability... yes
checking pthread.h presence... yes
checking for pthread.h... yes
checking setjmp.h usability... yes
checking setjmp.h presence... yes
checking for setjmp.h... yes
checking for off_t... yes
checking for size_t... yes
checking size of char... 1
checking size of short... 2
checking size of int... 4
checking size of long... 8
checking size of long long... 8
checking size of size_t... 8
checking for size_t... (cached) yes
checking for stdlib.h... (cached) yes
checking for unistd.h... (cached) yes
checking for getpagesize... yes
checking for working mmap... yes
checking for main in -lstdc++... yes
checking for pthread_create in -lpthread... yes
checking for pthread_join in -lpthread... yes
checking for pthread_mutex_lock in -lpthread... yes
checking for iconv_open in -liconv... yes
checking for getenv... yes
checking for opendir... yes
checking for setjmp... yes
checking whether make is GNU Make... yes
checking if g++ supports stl <vector> (required)... yes
checking if g++ supports stl <list> (required)... yes
checking if g++ supports stl <map> (required)... yes
checking if g++ supports stl <set> (required)... yes
checking if g++ supports stl <queue> (required)... yes
checking if g++ supports stl <functional> (required)... yes
checking if g++ supports stl <algorithm> (required)... yes
checking if g++ supports stl <string> (required)... yes
checking if g++ supports stl <iostream> (required)... yes
checking if g++ supports stl <sstream> (required)... yes
checking if g++ supports stl <fstream> (required)... yes
checking if g++ supports template <class T> (required)... yes
checking if g++ supports const_cast<> (required)... yes
checking if g++ supports static_cast<> (required)... yes
checking if g++ supports dynamic_cast<> (required)... yes
checking if g++ supports reinterpret_cast<> (required)... yes
checking if g++ supports exception handler (required)... yes
checking if g++ supports namespaces (required) ... yes
checking if g++ environment provides all required features... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating src/Makefile.msvc
config.status: creating man/Makefile
config.status: creating doc/Makefile
config.status: creating tests/Makefile
config.status: creating swig/version.h
config.status: creating mecab.iss
config.status: creating mecab-config
config.status: creating mecabrc
config.status: creating config.h
config.status: executing depfiles commands
config.status: executing libtool commands
config.status: executing default commands

続けて make します。権限などでエラーが出てきましたので sudo で実行すると良いかなと思いますが少し不安な部分です。

1
2
sudo make
sudo make install

IPA 辞書のインストール

mecab-ipadic-2.7.0-20070801 を解凍して移動して以下のコマンドを実行します。IPA 辞書も UTF-8 のみで進めます。

1
2
3
./configure --with-charset=utf8
sudo make
sudo make install

なんかいろいろエラーが出たんですが、何回か実行してたらできました。そして実際に使えるかをテストします。テストにはゆるゆりのアッカリーンの紹介文を使用させて頂きます。

良い子さゆえに主人公の座を追われそうな、とにかく良い子。 影が薄いキャラで次第にフェードアウト気味(涙)

以下のように実行します。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
mecab
良い子さゆえに主人公の座を追われそうな、とにかく良い子。 影が薄いキャラで次第にフェードアウト気味()    
良い	形容詞,自立,*,*,形容詞・アウオ段,基本形,良い,ヨイ,ヨイ
子	名詞,一般,*,*,*,*,子,コ,コ
さ	名詞,接尾,特殊,*,*,*,さ,サ,サ
ゆえ	名詞,接尾,一般,*,*,*,ゆえ,ユエ,ユエ
に	助詞,格助詞,一般,*,*,*,に,ニ,ニ
主人公	名詞,一般,*,*,*,*,主人公,シュジンコウ,シュジンコー
の	助詞,連体化,*,*,*,*,の,ノ,ノ
座	名詞,一般,*,*,*,*,座,ザ,ザ
を	助詞,格助詞,一般,*,*,*,を,ヲ,ヲ
追わ	動詞,自立,*,*,五段・ワ行促音便,未然形,追う,オワ,オワ
れ	動詞,接尾,*,*,一段,連用形,れる,レ,レ
そう	名詞,接尾,助動詞語幹,*,*,*,そう,ソウ,ソー
な	助動詞,*,*,*,特殊・ダ,体言接続,だ,ナ,ナ
、	記号,読点,*,*,*,*,、,、,、
とにかく	副詞,一般,*,*,*,*,とにかく,トニカク,トニカク
良い	形容詞,自立,*,*,形容詞・アウオ段,基本形,良い,ヨイ,ヨイ
子	名詞,一般,*,*,*,*,子,コ,コ
。	記号,句点,*,*,*,*,。,。,。
影	名詞,一般,*,*,*,*,影,カゲ,カゲ
が	助詞,格助詞,一般,*,*,*,が,ガ,ガ
薄い	形容詞,自立,*,*,形容詞・アウオ段,基本形,薄い,ウスイ,ウスイ
キャラ	名詞,一般,*,*,*,*,キャラ,キャラ,キャラ
で	助詞,格助詞,一般,*,*,*,で,デ,デ
次第に	副詞,一般,*,*,*,*,次第に,シダイニ,シダイニ
フェードアウト	名詞,一般,*,*,*,*,*
気味	名詞,接尾,一般,*,*,*,気味,ギミ,ギミ
(	名詞,サ変接続,*,*,*,*,*
涙	名詞,一般,*,*,*,*,涙,ナミダ,ナミダ
)	名詞,サ変接続,*,*,*,*,*
EOS

出力フォーマットについては以下のページが参考になるそうです。今回はインストールが目的ですが、実際に使ってみる際はこちらを見て勉強してみます。備忘録を書いてくださった皆さんに感謝します。

インストールの手順で間違っている部分などが御座いましたら是非コメント欄にてお知らせ頂ければ幸いです。

コメント

コメントは受け付けていません。