Created
January 17, 2013 10:24
-
-
Save Salinger/4555097 to your computer and use it in GitHub Desktop.
MeCabの動作確認用。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding:utf-8 -*- | |
import MeCab | |
m = MeCab.Tagger("-Ochasen") | |
string = u"それサバンナでも同じ事言えんの?" | |
# MeCabでUnicode文字列を扱う場合は、一度エンコードする必要がある。 | |
# この際、 | |
# node = tagger.parseToNode(string.encode("utf-8")) | |
# とすると、stringがパース中にガベコレされてしまって、 | |
# 変な挙動になる場合があるので注意。このように一度変数に代入すれば問題ない。 | |
string = string.encode("utf-8") | |
node = m.parseToNode(string) | |
# ノードを順番にたどる。 | |
# node.surface, node.feature はstr型なので、 | |
# この時点でunicode型に変換する方がトラブル防止のためにはいいかも。 | |
while node: | |
print node.surface, node.feature | |
node = node.next | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment