LoginSignup
18
12

More than 5 years have passed since last update.

Macにmecab-python3を導入しようとして、苦戦した話

Last updated at Posted at 2019-04-10

概要

チャットボット作成の練習としてmecab-python3をMacにインストールしようとしたら意外と苦戦したので、手順と苦戦したポイントを共有します。

インストール環境

  • MacBook Air (Early 2015)
  • macOS Mojava 10.14.1
  • Xcode 10.2
  • pip 19.0.3
  • Homebrew 2.1.0

1. MeCabのインストール

最初にMeCab本体と辞書をインストールします。これがないとmecab-python3も入れられません。

インストール手順

Terminal
$ brew install mecab
$ brew install mecab-ipadic

ここまで終わったら、ちゃんとインストールできたか確かめましょう。
ターミナルでmecabと打つと、入力待ち状態になり、適当な文章を打ってEnter

以下のように解説が出ればMeCabのインストールは完了です。

Terminal
$ mecab
おはようございます
おはよう    感動詞,*,*,*,*,*,おはよう,オハヨウ,オハヨー
ござい   助動詞,*,*,*,五段・ラ行特殊,連用形,ござる,ゴザイ,ゴザイ
ます  助動詞,*,*,*,特殊・マス,基本形,ます,マス,マス
EOS

2. SWIGのインストール

SWIG (Simplified Wrapper and Interface Generator) は、C/C++ で書かれたプログラムやライブラリを、Tcl/Tk、Perl、Python、Ruby、PHP、Luaなどのスクリプト言語や、Java、C#、Common Lisp、Scheme、Ocamlなどのプログラミング言語に接続するためのオープンソースのツールである。 (Wikipedia)

MeCabはC++で書かれているんですかね?
とりあえずSWIGはMeCabをpython3で使えるようにしてくれるツールのようです。
mecab-python3をインストールする前にこれを入れておかないとエラーがでます。

インストール手順

これは簡単

Terminal
$ brew install swig

インストールできたかは以下のコマンドでバージョンが出ればわかります。

Terminal
$ swig -version

SWIG Version 3.0.12

Compiled with clang++ [x86_64-apple-darwin18.0.0]

Configured options: +pcre

Please see http://www.swig.org for reporting bugs and further information

3. mecab-python3のインストール

さて、おまたせしました!
やっと本命のmecab-python3のインストールです!
以下のコマンドを入力してください

Terminal
$ pip install mecab-python3

これでいける...はずなのに変なエラーが出てきました

Terminal
$ pip install mecab-python3

Collecting mecab-python3
  Using cached https://files.pythonhosted.org/packages/ac/48/295efe525df40cbc2173748eb869290e81a57e835bc41f6d3834fc5dad5f/mecab-python3-0.996.1.tar.gz
Building wheels for collected packages: mecab-python3
  Building wheel for mecab-python3 (setup.py) ... error
  Complete output from command /Users/Lappiness/.pyenv/versions/anaconda3-4.1.1/bin/python -u -c "import setuptools, tokenize;__file__='/private/var/folders/n1/wmr8p0zd6lb265ryzrg55rz80000gn/T/pip-install-ls4os91_/mecab-python3/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /private/var/folders/n1/wmr8p0zd6lb265ryzrg55rz80000gn/T/pip-wheel-y30lec8x --python-tag cp35:
  running bdist_wheel
  running build
  running build_py
  running build_ext
  building '_MeCab' extension
  swigging MeCab.i to MeCab_wrap.cpp
  swig -python -shadow -c++ -I/usr/local/Cellar/mecab/0.996/include -o MeCab_wrap.cpp MeCab.i
  /usr/local/Cellar/mecab/0.996/include/mecab.h:848: Warning 302: Identifier 'set_sentence' redefined by %extend (ignored),
  MeCab.i:89: Warning 302: %extend definition of 'set_sentence'.
  creating build
  creating build/temp.macosx-10.6-x86_64-3.5
  /usr/bin/clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/Lappiness/.pyenv/versions/anaconda3-4.1.1/include -arch x86_64 -I/usr/local/Cellar/mecab/0.996/include -I/Users/Lappiness/.pyenv/versions/anaconda3-4.1.1/include/python3.5m -c MeCab_wrap.cpp -o build/temp.macosx-10.6-x86_64-3.5/MeCab_wrap.o
  warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
  MeCab_wrap.cpp:3051:10: fatal error: 'stdexcept' file not found
  #include <stdexcept>
           ^~~~~~~~~~~
  1 warning and 1 error generated.
  error: command '/usr/bin/clang' 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 /Users/Lappiness/.pyenv/versions/anaconda3-4.1.1/bin/python -u -c "import setuptools, tokenize;__file__='/private/var/folders/n1/wmr8p0zd6lb265ryzrg55rz80000gn/T/pip-install-ls4os91_/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 /private/var/folders/n1/wmr8p0zd6lb265ryzrg55rz80000gn/T/pip-record-rwbur4g_/install-record.txt --single-version-externally-managed --compile:
    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/local/Cellar/mecab/0.996/include -o MeCab_wrap.cpp MeCab.i
    /usr/local/Cellar/mecab/0.996/include/mecab.h:848: Warning 302: Identifier 'set_sentence' redefined by %extend (ignored),
    MeCab.i:89: Warning 302: %extend definition of 'set_sentence'.
    creating build
    creating build/temp.macosx-10.6-x86_64-3.5
    /usr/bin/clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/Lappiness/.pyenv/versions/anaconda3-4.1.1/include -arch x86_64 -I/usr/local/Cellar/mecab/0.996/include -I/Users/Lappiness/.pyenv/versions/anaconda3-4.1.1/include/python3.5m -c MeCab_wrap.cpp -o build/temp.macosx-10.6-x86_64-3.5/MeCab_wrap.o
    warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
    MeCab_wrap.cpp:3051:10: fatal error: 'stdexcept' file not found
    #include <stdexcept>
             ^~~~~~~~~~~
    1 warning and 1 error generated.
    error: command '/usr/bin/clang' failed with exit status 1

    ----------------------------------------
Command "/Users/Lappiness/.pyenv/versions/anaconda3-4.1.1/bin/python -u -c "import setuptools, tokenize;__file__='/private/var/folders/n1/wmr8p0zd6lb265ryzrg55rz80000gn/T/pip-install-ls4os91_/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 /private/var/folders/n1/wmr8p0zd6lb265ryzrg55rz80000gn/T/pip-record-rwbur4g_/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/n1/wmr8p0zd6lb265ryzrg55rz80000gn/T/pip-install-ls4os91_/mecab-python3/

なにやら色々書いてありますが、『〇〇をインストールして』とか『××が見つかりません』みたいな簡単なエラーじゃなさそう(泣)
万事休すかと思いきや、検索すると同じような問題に遭遇した方が他にもいたようで、どうやらコマンドラインツールが最新のものだとうまくいかないようです
そんなの調べないとわからない(T _ T)

過去のコマンドラインツールはAppleのデベロッパー用サイトからダウンロードできます
https://developer.apple.com/download/more/
上記のサイトにログインして、私は下記のXcode 9.4のパッケージをダウンロードしました。
スクリーンショット 2019-04-11 2.00.39.png

このパッケージからインストールして、いざ再チャレンジ

Terminal
$ pip install mecab-python3
Collecting mecab-python3
  Using cached https://files.pythonhosted.org/packages/ac/48/295efe525df40cbc2173748eb869290e81a57e835bc41f6d3834fc5dad5f/mecab-python3-0.996.1.tar.gz
Building wheels for collected packages: mecab-python3
  Building wheel for mecab-python3 (setup.py) ... done
  Stored in directory: /Users/Lappiness/Library/Caches/pip/wheels/73/71/4f/63a79925b5e9bb38932043917cc60140beb8022ac14a952b1e
Successfully built mecab-python3
Installing collected packages: mecab-python3
Successfully installed mecab-python3-0.996.1

嘘のようにあっさりインストール完了しました。(^^;;

とりあえずサンプルコードも載せときます。

Sapmle.py
import MeCab
mecab = MeCab.Tagger("-Ochasen") # MeCabオブジェクトを作成
malist = mecab.parse("すもももももももものうち") # 形態素解析を行う
print(malist)
実行結果
すもも   スモモ   すもも   名詞-一般       
も モ も 助詞-係助詞        
もも  モモ  もも  名詞-一般       
も モ も 助詞-係助詞        
もも  モモ  もも  名詞-一般       
の ノ の 助詞-連体化        
うち  ウチ  うち  名詞-非自立-副詞可能       
EOS

ちゃんと動きました。
まあ、めでたしめでたし

参考にした記事

18
12
1

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
12