LoginSignup
11
12

More than 5 years have passed since last update.

【PHP】MeCabの導入手順と利用方法

Posted at

phpでMeCabを利用できるようにするまでの備忘録です。

導入環境

今回導入する環境です。

$ cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
$ php -v
PHP 5.4.16 (cli) (built: Nov 15 2017 16:33:54)

MeCabを導入

公式サイトの手順に従って作業を進めます。
URL:http://taku910.github.io/mecab/

$ cd /usr/local/src
$ wget "https://drive.google.com/uc?export=download&id=0B4y35FiV1wh7cENtOXlicTFaRUE" -O mecab-0.996.tar.gz
$ cd mecab-0.996/
$ ./configure --with-charset=utf8
$ make
$ make install

※ エラーが発生した場合の対処法を下記に載せておきます。

エラー発生(その1)

$ ./configure
checking for a BSD-compatible install... /bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/local/src/mecab-0.996':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

下記コマンドを実行します。

$ sudo yum install gcc

エラー発生(その2)

$ ./configure
(省略)
checking if g++ supports stl <vector> (required)... configure: WARNING: g++ stl <vector> does not work
checking if g++ supports stl <list> (required)... configure: WARNING: g++ stl <list> does not work
checking if g++ supports stl <map> (required)... configure: WARNING: g++ stl <map> does not work
checking if g++ supports stl <set> (required)... configure: WARNING: g++ stl <set> does not work
checking if g++ supports stl <queue> (required)... configure: WARNING: g++ stl <queue> does not work
checking if g++ supports stl <functional> (required)... configure: WARNING: g++ stl <functional> does not work
checking if g++ supports stl <algorithm> (required)... configure: WARNING: g++ stl <algorithm> does not work
checking if g++ supports stl <string> (required)... configure: WARNING: g++ stl <string> does not work
checking if g++ supports stl <iostream> (required)... configure: WARNING: g++ stl <iostream> does not work
checking if g++ supports stl <sstream> (required)... configure: WARNING: g++ stl <sstream> does not work
checking if g++ supports stl <fstream> (required)... configure: WARNING: g++ stl <fstream> does not work
checking if g++ supports template <class T> (required)... configure: WARNING: g++ template <class T> does not work
checking if g++ supports const_cast<> (required)... configure: WARNING: g++ const_cast<> does not work
checking if g++ supports static_cast<> (required)... configure: WARNING: g++ static_cast<> does not work
checking if g++ supports reinterpret_cast<> (required)... configure: WARNING: g++ reinterpret_cast<> does not work
checking if g++ supports namespaces (required) ... no
checking if g++ supports \__thread (optional)... no
configure: WARNING: \__thread keyword is not supported on this environment. Error handling of MeCab, e.g., MeCab::getLastError(), is not thread safe.
checking if g++ supports template <class T> (required)... configure: WARNING: g++ template <class T> does not work
checking if g++ supports GCC native atomic operations (optional)... no
checking if g++ supports OSX native atomic operations (optional)... no
checking if g++ environment provides all required features... no
configure: error: Your compiler is not powerful enough to compile MeCab.         If it should be, see config.log for more information of why it failed.

下記コマンドを実行します。

$ sudo yum install gcc-c++

IPA辞書を導入

公式で推奨されているMeCab用のIPA辞書をインストールします。

$ wget "https://drive.google.com/uc?export=download&id=0B4y35FiV1wh7MWVlSDBCSXZMTXM" -O mecab-ipadic-2.7.0-20070801.tar.gz
$ tar zxfv mecab-ipadic-2.7.0-20070801.tar.gz
$ cd mecab-ipadic-2.7.0-20070801
$ ./configure --with-charset=utf8
$ make
$ make install

動作確認

インストールしたMeCabの動作確認を行います。問題なければ下記のように出力されます。

$ echo すもももももももものうち | mecab
すもも   名詞,一般,\*,*,*,*,すもも,スモモ,スモモ
も 助詞,係助詞,*,*,*,*,も,モ,モ
もも  名詞,一般,*,*,*,*,もも,モモ,モモ
も 助詞,係助詞,*,*,*,*,も,モ,モ
もも  名詞,一般,*,*,*,*,もも,モモ,モモ
の 助詞,連体化,*,*,*,*,の,ノ,ノ
うち  名詞,非自立,副詞可能,*,*,*,うち,ウチ,ウチ
EOS

文字化けが発生した場合

文字化けが発生したら./configureにオプションが正しく付与されているか確認しましょう。
間違えた場合はアンイストール(make uninstall)して最初から作業します。

$ echo すもももももももものうち | mecab
すもももももももものうち    ����,����,*,*,*,*,*
EOS

php-macabをインストール

phpでMeCabを利用するためにphp-mecabをインストールします。

$ git clone https://github.com/rsky/php-mecab.git
$ cd php-mecab/mecab/
$ phpize
$ ./configure --with-mecab=/usr/local/src/mecab-0.996/mecab-config
$ make
$ make test
$ make install

$ ls /usr/lib64/php/modules/ | grep mecab # 出力確認
mecab.so

$ vim /etc/php.d/mecab.ini # 新規ファイルを作成
# 下記コードを記載
extension=mecab.so

$ service httpd restart # apacheを再起動
$ php -m | grep mecab # phpモジュールにmecabが含まれることを確認
mecab

※ エラーが発生した場合の対処法を下記に載せておきます。

エラー発生(その1)

$ git clone https://github.com/rsky/php-mecab.git
-bash: git: command not found

下記コマンドを実行します。

$ sudo yum install git

エラー発生(その2)

$ phpize
Can't find PHP headers in /usr/include/php
The php-devel package is required for use of this command.

下記コマンドを実行します。

$ sudo yum install php-devel

phpでMeCabを利用する

phpでMeCabを利用するサンプルコードです。

mecab_test.php
<?php
$str = 'すもももももももものうち';

$mecab = new Mecab_Tagger();
$nodes = $mecab->parseToNode($str);
foreach ($nodes as $n) {
    $items = $n->getFeature();
    echo $items . PHP_EOL;
}
$ php mecab_test.php
BOS/EOS,*,*,*,*,*,*,*,*
名詞,一般,*,*,*,*,すもも,スモモ,スモモ
助詞,係助詞,*,*,*,*,も,モ,モ
名詞,一般,*,*,*,*,もも,モモ,モモ
助詞,係助詞,*,*,*,*,も,モ,モ
名詞,一般,*,*,*,*,もも,モモ,モモ
助詞,連体化,*,*,*,*,の,ノ,ノ
名詞,非自立,副詞可能,*,*,*,うち,ウチ,ウチ
BOS/EOS,*,*,*,*,*,*,*,*

お疲れ様でした。

11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12