Created
April 9, 2014 11:56
-
-
Save totakke/10260476 to your computer and use it in GitHub Desktop.
Python 3 compatible MeCab-python example code
This file contains 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/python | |
# -*- coding: utf-8 -*- | |
from __future__ import print_function | |
import MeCab | |
import sys | |
import string | |
sentence = "太郎はこの本を二郎を見た女性に渡した。" | |
try: | |
print(MeCab.VERSION) | |
t = MeCab.Tagger (" ".join(sys.argv)) | |
print(t.parse(sentence)) | |
m = t.parseToNode(sentence) | |
while m: | |
print(m.surface, "\t", m.feature) | |
m = m.next | |
print("EOS") | |
lattice = MeCab.Lattice() | |
t.parse(lattice) | |
lattice.set_sentence(sentence) | |
len = lattice.size() | |
for i in range(len + 1): | |
b = lattice.begin_nodes(i) | |
e = lattice.end_nodes(i) | |
while b: | |
print("B[%d] %s\t%s" % (i, b.surface, b.feature)) | |
b = b.bnext | |
while e: | |
print("E[%d] %s\t%s" % (i, e.surface, e.feature)) | |
e = e.bnext | |
print("EOS"); | |
d = t.dictionary_info() | |
while d: | |
print("filename: %s" % d.filename) | |
print("charset: %s" % d.charset) | |
print("size: %d" % d.size) | |
print("type: %d" % d.type) | |
print("lsize: %d" % d.lsize) | |
print("rsize: %d" % d.rsize) | |
print("version: %d" % d.version) | |
d = d.next | |
except RuntimeError as e: | |
print("RuntimeError:", e); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I am new to MeCab. Could you suggest some resource from where I can start learning about it ?
Thanks