LoginSignup
18
28

More than 5 years have passed since last update.

pythonで言語処理するためのライブラリインストール方法(Mecab/Cabocha)

Last updated at Posted at 2017-11-27
  • 英語だと、nltkライブラリを活用すればいいし、そもそも英語は単語ごとに区切られているので、単語をそのまま特徴ベクトルに変換して機械学習すればいい。
  • 日本語の場合は、
    • nltkライブラリで扱える範囲が限定されている
    • 形態素解析(=単語ごとに分割して、品詞タグを付与)して、
    • 構文解析(=文節ごとに区切って係り受けの関係を付与)するのが必要

ということで、日本語を扱う上でのライブラリのインストールめもです。
日本語の場合、下記のツールが利用できます。

  • 形態素解析
    • MeCab、JUMAN++、kuromoji、など
  • 構文解析
    • CaboCha、KNP、など

今回の投稿は、
形態素解析として、MeCab
構文解析として、CaboCha をpythonから利用するためのインストールメモです。

実際にこれらのツールをpythonで利用する場合のコードサンプルはこちら
-> pythonで日本語文の感情分析(+言語処理の基礎)

検証環境

$ cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 

$ python -V
Python 3.5.4

install

mecab

git clone https://github.com/taku910/mecab.git
cd mecab/mecab
./configure  --enable-utf8-only
make
make check
sudo make install
  • check following
/usr/local/etc/mecabrc
/usr/local/bin/mecab
/usr/local/bin/mecab-config

ipa-dic

  • dic for mecab
cd ../mecab-ipadic
./configure --with-charset=utf8
make
sudo make install
  • check
$ echo 'MeCab はフリーソフトウェアです' | mecab

mecab for python

  • use pip install
pip install mecab-python3
  • in tha case that error occurred when you use form python.
ImportError: libmecab.so.2: cannot open shared object file: No such file or directory
  • check libraly path and fix it.
ldconfig -p | grep 'mecab'

echo '/usr/local/lib' >> ./mylib.conf
sudo cp  mylib.conf /etc/ld.so.conf.d/
sudo ldconfig

ldconfig -p | grep 'mecab'

NEologd

Cabocha

  • CRF++
 wget "https://drive.google.com/uc?export=download&id=0B4y35FiV1wh7QVR6VXJ5dWExSTQ" -O CRF++-0.58.tar.gz
 $ tar zxfv CRF++-0.58.tar.gz
 $ cd CRF++-0.58
 $ ./configure
 $ make
 $ sudo make install
  • Cabocha
$ wget "https://googledrive.com/host/0B4y35FiV1wh7cGRCUUJHVTNJRnM/cabocha-0.69.tar.bz2" -O cabocha-0.69.tar.bz2
$ tar jxvf cabocha-0.69.tar.bz2
$ cd cabocha-0.69
$ ./configure --with-charset=utf8
$ make
$ sudo make install
  • Cabocha for python
$ cd cabocha-0.69/python
$ vim setup.py
def cmd2(str):
#   return string.split (cmd1(str)) この行を削除
    return cmd1(str).split() # この行を挿入

$ sudo python setup.py build_ext
$ sudo python setup.py install
$ sudo ldconfig

実際にこれらのツールをpythonで利用する場合のコードサンプルはこちら

-> pythonで日本語文の感情分析(+言語処理の基礎)

18
28
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
18
28