0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

メモリの少ないさくらVPS(ubuntu18.04)にmecab-ipadic-neologdをインストールしてpythonから使う

Last updated at Posted at 2020-09-21

概要

ubuntu18.04LTSをインストールしたさくらVPSにmecab-ipadic-neologdをインストールしてpythonから呼べるようにしました。
mecab-ipadic-neologdはメモリ不足のために、サーバ上でインストールできなかったので、ローカルで作った辞書をscpしました。

環境

さくらのVPSの2Gプラン
OSは以下の通り

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.4 LTS (Bionic Beaver)"
(以下略)

$ python -V
Python 3.6.8

mecabのインストール

リモートサーバ上で下記コマンドを実行します。

sudo apt install mecab
sudo apt install libmecab-dev
sudo apt install mecab-ipadic-utf8

動作確認します。

$ echo 神ってる | mecab
神	名詞,一般,*,*,*,*,神,カミ,カミ
って	助詞,格助詞,連語,*,*,*,って,ッテ,ッテ
る	助動詞,*,*,*,文語・ル,基本形,る,ル,ル
EOS

参考:ubuntu 18.10 に mecab をインストール

mecab-ipadic-neologdのコピー

今回はローカルのMacPCでmecab-ipadic-neologdを導入済みだったのでそこから辞書をscpします。

ローカルのmacでmecab-ipadic-neologdの辞書の場所を確認します。

$ sudo find / -name mecabrc
/usr/local/etc/mecabrc
/usr/local/Cellar/mecab/0.996/.bottle/etc/mecabrc

$ cat /etc/usr/local/mecabrc
;
; Configuration file of MeCab
;
; $Id: mecabrc.in,v 1.3 2006/05/29 15:36:08 taku-ku Exp $;
;
;dicdir =  /usr/local/lib/mecab/dic/ipadic
dicdir =  /usr/local/lib/mecab/dic/mecab-ipadic-neologd
(以下略)

辞書をローカルからリモートにscpします。

$ scp -r /usr/local/lib/mecab/dic/mecab-ipadic-neologd username@domain.com:~/

リモートサーバ上でmecabの動作確認をします。

$ echo 神ってる | mecab -d ~/mecab-ipadic-neologd
神ってる	名詞,固有名詞,一般,*,*,*,神ってる,カミッテル,カミッテル
EOS

初期辞書では正しく認識されなかった「神ってる」が一単語で認識できているので、mecab-ipadic-neologdが使えていることがわかります。

参考:さくらVPSの安いプランとかでmecab-ipadic-NEologdを使う

デフォルト辞書をmecab-ipadic-neologdに設定

リモートサーバのmecabのデフォルト辞書をmecab-ipadic-neologdに設定します。
Macの場合とほぼ同じで、mecabrcを編集すればよいです。

リモートサーバ上のmecabrcを探して、中身を確認します。

$ sudo find / -name mecabrc
/etc/mecabrc

$ cat /etc/mecabrc
;
; Configuration file of MeCab
;
; $Id: mecabrc.in,v 1.3 2006/05/29 15:36:08 taku-ku Exp $;
;
dicdir = /var/lib/mecab/dic/debian
(以下略)

mecabの辞書はデフォルトで/var/lib/mecab/dicに格納されるようなので、mecab-ipadic-neologdもそこに移動し、mecabrcを対応するように変えます。

$ sudo mv ~/mecab-ipadic-neologd /var/lib/mecab/dic/
$ sudo nano /etc/mecabrc

(変更前)

;
; Configuration file of MeCab
;
; $Id: mecabrc.in,v 1.3 2006/05/29 15:36:08 taku-ku Exp $;
;
dicdir = /var/lib/mecab/dic/debian
(以下略)

(変更後)

;
; Configuration file of MeCab
;
; $Id: mecabrc.in,v 1.3 2006/05/29 15:36:08 taku-ku Exp $;
;
;dicdir = /var/lib/mecab/dic/debian
dicdir = /var/lib/mecab/dic/mecab-ipadic-neologd
(以下略)

正しく変更できていれば、辞書をしてせずとも「神ってる」を一語で認識できるようになります。

$ echo 神ってる | mecab -d ~/mecab-ipadic-neologd
神ってる	名詞,固有名詞,一般,*,*,*,神ってる,カミッテル,カミッテル
EOS

mecab-python3のインスト−ル

mecabをpython3から呼べるようにします。

sudo apt install swig
sudo apt install python3-pip
sudo pip3 install mecab-python3

pythonから呼べるか確認します。

$ python
Python 3.6.8 (default, May 23 2019, 19:27:09) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import MeCab
>>> MeCab.Tagger().parse('神ってる')

Failed initializing MeCab. Please see the README for possible solutions:

    https://github.com/SamuraiT/mecab-python3#common-issues

If you are still having trouble, please file an issue here, and include the
ERROR DETAILS below:

    https://github.com/SamuraiT/mecab-python3/issues

issueを英語で書く必要はありません。

------------------- ERROR DETAILS ------------------------
arguments: 
error message: [ifs] no such file or directory: /usr/local/etc/mecabrc
----------------------------------------------------------
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/shimaya/.pyenv/versions/3.6.8/lib/python3.6/site-packages/MeCab/__init__.py", line 124, in __init__
    super(Tagger, self).__init__(args)
RuntimeError

エラーが出ます。/usr/local/etc/mecabrcが存在しないと言われています。mecabrcは/etc/mecabrcにあるはずなので、存在しない場所を読みに行ってしまっているようです。
pythonが読みにいくmecabrcはMECABRCという環境変数を明示的に用意することで指定することができます。

$ nano ~/.bash_profile
(以下を追記)
export MECABRC=/etc/mecabrc

$ source ~/.bash_profile

再度試すと、「神ってる」を一語で形態素解析できました。

$ python
Python 3.6.8 (default, May 23 2019, 19:27:09) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import MeCab
>>> MeCab.Tagger().parse('神ってる')
'神ってる\t名詞,固有名詞,一般,*,*,*,神ってる,カミッテル,カミッテル\nEOS\n'

参考:ubuntu 18.10 に mecab をインストール
参考:Pythonから呼び出すMeCabのデフォルト辞書を変更する

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?