mecabをPHPで使えるphp_mecabをインストール


最近はMySQLにsennaとmecabを適用させて使っていますが、このmecabの機能をphpで使えるモジュールがあるそうで、早速インストールしてみた。

環境はこんな感じです

  • Fedora7
  • php5.2.4
  • mecab0.96

こちらのページでダウンロードできます。

インストール方法はお決まりのパターン。

# wget http://page2.xrea.jp/pub/php_mecab-0.2.0.tgz

# tar xfvz php_mecab-0.2.0.tgz

configureをする前にphpizeを実行します。

phpizeがないときは、yum install php-devel でインストール

# phpize

# ./configure–with-php-config=/path/to/php-config –with-mecab=/path/to/mecab-config

# make

# make install

これでPHPのextensionディレクトリにmecab.soができているはずです。

そしてphp.iniの設定。pnp.iniに

extension=mecab.so

を追加して、Apacheを再起動。

phpinfo()で確認するとmecabモジュールが読み込まれていれはインストールOK!

と、うまくいくはずなのですが、なぜか私はmakeの部分でエラーが・・・こんな感じ

/usr/local/src/php_mecab-0.2.0/mecab.c:283: error: expected declaration specifiers or ‘…’ before ‘tsrm_ls’

/usr/local/src/php_mecab-0.2.0/mecab.c: In function ‘zm_startup_mecab’:

/usr/local/src/php_mecab-0.2.0/mecab.c:738: error: too few arguments to function ‘zend_register_internal_class’

/usr/local/src/php_mecab-0.2.0/mecab.c:742: error: too few arguments to function ‘zend_register_internal_class’

/usr/local/src/php_mecab-0.2.0/mecab.c:747: error: expected expression before ‘void’

/usr/local/src/php_mecab-0.2.0/mecab.c:752: error: expected expression before ‘void’

/usr/local/src/php_mecab-0.2.0/mecab.c:762: error: too few arguments to function ‘zend_register_internal_class’

/usr/local/src/php_mecab-0.2.0/mecab.c:766: error: too few arguments to function ‘zend_register_internal_class’

/usr/local/src/php_mecab-0.2.0/mecab.c:788: error: too few arguments to function ‘zend_register_internal_class’

/usr/local/src/php_mecab-0.2.0/mecab.c: At top level:

/usr/local/src/php_mecab-0.2.0/mecab.c:1629: error: expected declaration specifiers or ‘…’ before ‘tsrm_ls’

/usr/local/src/php_mecab-0.2.0/mecab.c: In function ‘_php_mecab_get_required_element_type’:

/usr/local/src/php_mecab-0.2.0/mecab.c:1644: error: ‘tsrm_ls’ undeclared (first use in this function)

/usr/local/src/php_mecab-0.2.0/mecab.c:1644: error: (Each undeclared identifier is reported only once

/usr/local/src/php_mecab-0.2.0/mecab.c:1644: error: for each function it appears in.)

/usr/local/src/php_mecab-0.2.0/mecab.c: In function ‘zif_mecab_split’:

/usr/local/src/php_mecab-0.2.0/mecab.c:2032: error: too many arguments to function ‘_php_mecab_get_required_element_type’

うーん・・・ ̄ー ̄何なんだろう?phpかmecabのコンパイルおかしいのかなーなどといろいろ考えたが、どうやらmecab.cが曲者のようだ。とりあえずエラーメッセージの行のところをこんな感じでいじってみる。

283,1629行目 TSRMLS_CCをTSRMLS_DCに変更

738,742,762,766,788行目 zend_register_internal_classの第2引数にTSRMLS_CCを追加

747,752行目 TSRMLS_DCをTSRMLS_CCに変更

これでmakeが通った。めでたしめでたし。

おまけ

これを使うときは辞書ファイルのパスを通す必要があるのですが、いちいち通すのが面倒なのでiniに設定できます

辞書

mecab.default_dicdir=/path/

ユーザ辞書

mecab.default_userdic=/path/

Apachの再起動をお忘れなく

この時間帯に他の人が読んでる記事

eight-tree