yoshiislandblog.net
元営業の駆け出しアラサーSEが、休日にMACと戯れた際の殴り書きメモ。日々勉強。日々進歩。

この記事は3年以上前に書かれた記事で内容が古い可能性があります

ubuntu on vagrantにMecab python環境構築

2018-05-11

pyenv virtualenvの設定の続き

Mecab環境を構築する。

UbuntuにMecabのインストール

まずは、pythonに入れる前にUbuntuにMecabをインストール

(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$ sudo apt-get install mecab
(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$ sudo apt-get install mecab-ipadic
(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$ sudo apt-get install python-dev

確認

文字化けしてしまう。

(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$ mecab
すもももももももものうち
すもももももももものうち	����,����,*,*,*,*,*
EOS
^C
(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$

以下の通り、utf8の辞書を入れることで解決した。

mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$ sudo apt-get install mecab-ipadic-utf8

(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$ mecab
すもももももももものうち
すもも	名詞,一般,*,*,*,*,すもも,スモモ,スモモ
も	助詞,係助詞,*,*,*,*,も,モ,モ
もも	名詞,一般,*,*,*,*,もも,モモ,モモ
も	助詞,係助詞,*,*,*,*,も,モ,モ
もも	名詞,一般,*,*,*,*,もも,モモ,モモ
の	助詞,連体化,*,*,*,*,の,ノ,ノ
うち	名詞,非自立,副詞可能,*,*,*,うち,ウチ,ウチ
EOS

PythonへMecabをインストール

pythonにMecabをインストールする

(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$ pip install mecab-python3
Collecting mecab-python3
  Using cached https://files.pythonhosted.org/packages/25/e9/bbf5fc790a2bedd96fbaf47a84afa060bfb0b3e0217e5f64b32bd4bbad69/mecab-python3-0.7.tar.gz
    Complete output from command python setup.py egg_info:

    /bin/sh: 1: mecab-config: not found
    Traceback (most recent call last):
      File "<string>", line 20, in <module>
      File "/tmp/pip-build-h9_qlk7o/mecab-python3/setup.py", line 41, in <module>
        include_dirs=cmd2("mecab-config --inc-dir"),
      File "/tmp/pip-build-h9_qlk7o/mecab-python3/setup.py", line 21, in cmd2
        return cmd1(strings).split()
      File "/tmp/pip-build-h9_qlk7o/mecab-python3/setup.py", line 18, in cmd1
        return os.popen(strings).readlines()[0][:-1]
    IndexError: list index out of range

できない。

(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$ mecab-config
The program 'mecab-config' is currently not installed. To run 'mecab-config' please ask your administrator to install the package 'libmecab-dev'

と言われるので、その通りコマンドを入れる。

(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$ sudo apt-get install libmecab-dev

これでインストールできる

(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$ pip install mecab-python3
Collecting mecab-python3
  Using cached https://files.pythonhosted.org/packages/25/e9/bbf5fc790a2bedd96fbaf47a84afa060bfb0b3e0217e5f64b32bd4bbad69/mecab-python3-0.7.tar.gz
Installing collected packages: mecab-python3
  Running setup.py install for mecab-python3
Successfully installed mecab-python3-0.7
You are using pip version 7.1.2, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$

vimの文字化けで困ったら

vimの文字化けで困ったら以下参照
vimで日本語が文字化けする

PythonでMecabを動かす

以下のようにエラーが出てきた。

(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$ python mecab_test.py
Traceback (most recent call last):
  File "mecab_test.py", line 11, in <module>
    print(result)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

以下の通り環境変数追記することで解決。
私の場合は、~/.bash_aliasesに記載する。

(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$ export LC_ALL=$LANG
(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$ echo "export LC_ALL=$LANG" >> ~/.bash_aliases
(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$ source ~/.bash_aliases

実行。

(mecab_work) vagrant@vagrant-ubuntu-trusty-64:~/mecab_work$ python mecab_test.py
すもも	スモモ	すもも	名詞-一般
も	モ	も	助詞-係助詞
もも	モモ	もも	名詞-一般
も	モ	も	助詞-係助詞
もも	モモ	もも	名詞-一般
の	ノ	の	助詞-連体化
うち	ウチ	うち	名詞-非自立-副詞可能
EOS