Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active October 5, 2016 10:46
Show Gist options
  • Save kurozumi/e116e3586642a1f69d484c26911dbd76 to your computer and use it in GitHub Desktop.
Save kurozumi/e116e3586642a1f69d484c26911dbd76 to your computer and use it in GitHub Desktop.
【Python】MeCabを使って文章を解析して名詞だけ取得する方法
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