LoginSignup
33
36

More than 5 years have passed since last update.

Ubuntu 18.04 LTSのPythonでMeCabを動かす方法

Last updated at Posted at 2017-05-15

Ubuntu 18.04 LTSのPythonでMeCabを動かす方法についてまとめます。

MeCabのインストール

  1. sudo apt install mecab libmecab-dev mecab-ipadic-utf8コマンドを実行し、MeCabをインストールします。
  2. (以下のいずれにも該当しない場合は飛ばしてください)
    • 形態素解析辞書としてmecab-ipadic-NEologdを使う場合
      1. sudo apt install git make curl xz-utils fileコマンドを実行し、必要なパッケージをインストールします。
      2. cd /tmpコマンドを実行し、tmpディレクトリに移動します。
      3. git clone --depth 1 https://github.com/neologd/mecab-ipadic-neologd.gitコマンドを実行し、mecab-ipadic-NEologdをcloneします。
      4. cd mecab-ipadic-neologdコマンドを実行し、cloneしたディレクトリに移動します。
      5. ./bin/install-mecab-ipadic-neologd -nコマンドを実行し、mecab-ipadic-NEologdをインストールします。
        ※途中でインストールして良いか聞かれるのでyesと入力します。
        ※途中でかなり重くなります。
    • 形態素解析辞書としてUniDicmecab-unidic-NEologdを使う場合
      1. UniDicをインストールします。
        • APTを使ってUniDicをインストールする場合 (通常はこちら)
          1. sudo apt install unidic-mecabコマンドを実行し、UniDicをインストールします。
          2. sudo update-alternatives --config mecab-dictionaryコマンドを実行し、デフォルトの辞書をUniDicに変更します。
        • 最新版のUniDicを使いたい場合
          1. 「UniDic」国語研短単位自動解析用辞書|最新版ダウンロードから最新版のUniDicをダウンロードします。
          2. /usr/lib/mecab/dicディレクトリ内にUniDicを解凍します。
      2. 【形態素解析辞書としてmecab-unidic-NEologdを使う場合のみ】
        1. sudo apt install git make curl xz-utils fileコマンドを実行し、必要なパッケージをインストールします。
        2. cd /tmpコマンドを実行し、tmpディレクトリに移動します。
        3. git clone --depth 1 https://github.com/neologd/mecab-unidic-neologdコマンドを実行し、mecab-unidic-NEologdをcloneします。
        4. cd mecab-unidic-neologdコマンドを実行し、cloneしたディレクトリに移動します。
        5. ./bin/install-mecab-unidic-neologd -nコマンドを実行し、mecab-unidic-NEologdをインストールします。
          ※途中でインストールして良いか聞かれるのでyesと入力します。
          ※途中でかなり重くなります。
    • MeCab公式のPythonバインディングを用いる場合
      • Python2の場合
        pip install mecab-pythonコマンドを実行し、MeCab公式のPythonバインディングをインストールします。
      • Python3の場合
        pip install mecab-python3コマンドを実行し、MeCab公式のPythonバインディングをインストールします。
    • natto-pyを用いる場合
      pip install natto-pyコマンドを実行し、natto-pyをインストールします。

mecab-ipadic-NEeologdやmecab-unidic-NEologdに関して

アップデート

週2回 (月, 木) アップデートが行われるようです。

  1. cd /tmpコマンドを実行し、tmpディレクトリに移動します。
    • mecab-ipadic-NEologdの場合
      1. git clone --depth 1 https://github.com/neologd/mecab-ipadic-neologd.gitコマンドを実行し、mecab-ipadic-NEologdをcloneします。
      2. cd mecab-ipadic-neologdコマンドを実行し、cloneしたディレクトリに移動します。
      3. ./bin/install-mecab-ipadic-neologd -nコマンドを実行し、mecab-ipadic-NEologdをインストールします。
    • mecab-unidic-NEologdの場合
      1. git clone --depth 1 https://github.com/neologd/mecab-unidic-neologd.gitコマンドを実行し、mecab-unidic-NEologdをcloneします。
      2. cd mecab-unidic-neologdコマンドを実行し、cloneしたディレクトリに移動します。
      3. ./bin/install-mecab-unidic-neologd -nコマンドを実行し、mecab-unidic-NEologdをインストールします。

辞書の使用方法

IPAdicやUniDicの場合

MeCabの引数に-d /var/lib/mecab/dic/{辞書名}を与えます。

mecab-ipadic-NEeologdやmecab-unidic-NEologdの場合

MeCabの引数に-d /usr/lib/mecab/dic/{辞書名}を与えます。

関連記事

33
36
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
33
36