2
3

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 5 years have passed since last update.

Macでmecabを使う。

Last updated at Posted at 2019-05-26

#mecabの準備

mecabのインストール

$ brew install mecab

mecabの辞書をインストール

$ brew install mecab-ipadic

mecabの辞書をパワーアップ

$ cd mecab-ipadic-neologd
$ ./bin/install-mecab-ipadic-neologd -n

辞書が入っている場所はこのコマンドで調べられる。

$ echo `mecab-config --dicdir`"/mecab-ipadic-neologd"

mecab-python3をインストール

$ brew install swig
$ pip install mecab-python3

Python3からMeCabを使う - Qiitaを参考に実行

mecab.py
import MeCab

# 変数には辞書のファイルパスを指定する
mecab = MeCab.Tagger ('-d /usr/local/lib/mecab/dic/mecab-ipadic-neologd')

text = '解析したいテキスト'
mecab.parse('')#文字列がGCされるのを防ぐ
node = mecab.parseToNode(text)
while node:
    #単語を取得
    word = node.surface
    #品詞を取得
    pos = node.feature.split(",")[1]
    print('{0} , {1}'.format(word, pos))
    #次の単語に進める
    node = node.next

参考

mecab-ipadic-neologd/README.ja.md at master · neologd/mecab-ipadic-neologd · GitHub
Python3からMeCabを使う - Qiita

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?