Last active
October 5, 2016 10:46
-
-
Save kurozumi/e116e3586642a1f69d484c26911dbd76 to your computer and use it in GitHub Desktop.
【Python】MeCabを使って文章を解析して名詞だけ取得する方法
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
import MeCab | |
mecab = MeCab.Tagger("mecabrc") | |
# MeCabを使って形態素解析をします。 | |
def ma_parse(sentence, fileter="名詞"): | |
node = mecab.parseToNode(sentence) | |
while node: | |
if node.feature.startswith(filter): | |
yield node.surface | |
node = node.next | |
if __name__ == "__main__": | |
sentence = """山崎まさよしは、日本のシンガーソングライター、俳優。愛称は「まさやん」。 | |
身長176cm、体重64kg、A型、滋賀県大津市出身、山口県防府市育ち。 | |
ライブでの基本はギター。レコーディングではマルチプレイヤーであり、作品(楽曲『未完成』など)によっては全ての楽器をこなしている。既婚 | |
""" | |
words = [word for word in ma_parse(sentence)] | |
print(words) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment