LoginSignup
16
10

More than 5 years have passed since last update.

MecabをPythonで使う

Last updated at Posted at 2017-12-04

Mecab

まずはMecabをインストール


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

正常にインストールできているかを確認

$ mecab
君の名は
君      名詞,代名詞,一般,*,*,*,君,キミ,キミ
の      助詞,連体化,*,*,*,*,の,ノ,ノ
名      名詞,一般,*,*,*,*,名,ナ,ナ
は      助詞,係助詞,*,*,*,*,は,ハ,ワ
EOS

mecab-ipadic-NEologd

辞書を変更する(もとの辞書は固有名詞などが弱い)


$ git clone --depth 1 https://github.com/neologd/mecab-ipadic-neologd.git
$ cd mecab-ipadic-neologd
$ ./bin/install-mecab-ipadic-neologd -n -a

mecab-python

Python2の場合

$ pip install mecab-python

Python3の場合

$ pip3 install mecab-python3

上は環境によって違うかもしれない

確認

test.py
import sys
import MeCab
#Taggerの引数に-dオプションとmecab-ipadic-neologdの場所を指定する
# ↓場所の確認
# echo `mecab-config --dicdir`"/mecab-ipadic-neologd"
m = MeCab.Tagger("-d ../mecab-ipadic-neologd")
text = m.parse("君の名前は")
print( text)
$ python3 test.py
君      キミ    君      名詞-代名詞-一般
の      ノ      の      助詞-連体化
名前    ナマエ  名前    名詞-一般
は      ハ      は      助詞-係助詞
EOS

以上

16
10
2

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
16
10