LoginSignup
8
5

More than 5 years have passed since last update.

Raspberry Piでpythonからmecabを使う初期設定

Last updated at Posted at 2018-12-08

mecab導入にいろいろてこずった結局、OSを初期化して以下の手順でうまくいった。

環境

項目 内容
デバイス Raspberry Pi 3 ModelB
OS 2018-11-13-raspbian-stretch

実行

ラズパイの端末から以下を順に実行。
ラズパイにディスプレイつなげても、sshで接続して操作してもどちらでも良い。

apt-getをアップデート

【実行内容】
sudo apt-get upgrade
【出力】
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
アップグレードパッケージを検出しています... 完了
アップグレード: 0 個、新規インストール: 0 個、削除: 0 個、保留: 0 個。

apt-getをアップグレード

【実行内容】
sudo apt-get upgrade
【出力】
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
アップグレードパッケージを検出しています... 完了
アップグレード: 0 個、新規インストール: 0 個、削除: 0 個、保留: 0 個。

mecab本体や辞書などをインストール

【実行内容】
sudo apt-get install mecab libmecab-dev mecab-ipadic-utf8 python-mecab
【出力1】
この操作後に追加で 138 MB のディスク容量が消費されます。
続行しますか? [Y/n] 

と聞かれるのでYと入力してエンター

【出力2】
得:1 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf libmecab2 armhf 0.996-3.1 [218 kB]
取得:2 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf libmecab-dev armhf 0.996-3.1 [271 kB]
取得:3 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian stretch/main armhf mecab-utils armhf 0.996-3.1 [7,478 B]
・・・・・・省略・・・・・・・・・
done!

mecab-python3をインストール

【実行内容】
sudo pip3 install mecab-python3
【出力(エラー)】
  Downloading https://files.pythonhosted.org/packages/ac/48/295efe525df40cbc2173748eb869290e81a57e835bc41f6d3834fc5dad5f/mecab-python3-0.996.1.tar.gz
Building wheels for collected packages: mecab-python3
・・・・省略・・・・
  error: command 'swig' failed with exit status 1

  ----------------------------------------
  Failed building wheel for mecab-python3
  Running setup.py clean for mecab-python3
Failed to build mecab-python3
Installing collected packages: mecab-python3
  Running setup.py install for mecab-python3 ... error
    Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-72x8lhhq/mecab-python3/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-8dwpaus1-record/install-record.txt --single-version-externally-managed --compile --user --prefix=:
    /usr/lib/python3.5/distutils/dist.py:261: UserWarning: Unknown distribution option: 'long_description_content_type'
      warnings.warn(msg)
    running install
    running build
    running build_py
    running build_ext
    building '_MeCab' extension
    swigging MeCab.i to MeCab_wrap.cpp
    swig -python -shadow -c++ -I/usr/include -o MeCab_wrap.cpp MeCab.i
    unable to execute 'swig': No such file or directory
    error: command 'swig' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-72x8lhhq/mecab-python3/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-8dwpaus1-record/install-record.txt --single-version-externally-managed --compile --user --prefix=" failed with error code 1 in /tmp/pip-build-72x8lhhq/mecab-python3/

error: command 'swig' failed with exit status 1と言われているので、swigをインストールする。

【実行内容】
sudo apt-get install swig
【出力】
省略(特にエラーが無ければOK)

再度mecab-python3のインストールを実行

【実行内容】
sudo pip3 install mecab-python3
【出力】
Collecting mecab-python3
  Downloading https://files.pythonhosted.org/packages/ac/48/295efe525df40cbc2173748eb869290e81a57e835bc41f6d3834fc5dad5f/mecab-python3-0.996.1.tar.gz
Building wheels for collected packages: mecab-python3
  Running setup.py bdist_wheel for mecab-python3 ... done
  Stored in directory: /root/.cache/pip/wheels/73/71/4f/63a79925b5e9bb38932043917cc60140beb8022ac14a952b1e
Successfully built mecab-python3
Installing collected packages: mecab-python3
Successfully installed mecab-python3-0.996.1

成功したっぽい!

python3から実行してみる

【実行内容】
# pythonを起動
python3
【実行内容】
import MeCab
mecab = MeCab.Tagger ("-Ochasen")
print(mecab.parse("すもももふともももももももも"))
【出力】
すもも   スモモ   すもも   名詞-一般
も モ も 助詞-係助詞
ふと  フト  ふと  副詞-一般
もも  モモ  もも  名詞-一般
も モ も 助詞-係助詞
もも  モモ  もも  名詞-一般
も モ も 助詞-係助詞
もも  モモ  もも  名詞-一般
の ノ の 助詞-連体化
うち  ウチ  うち  名詞-非自立-副詞可能

ちゃんと動いてる!!

8
5
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
8
5