Back to snippets
mecab_japanese_morpheme_parser_with_node_iteration.py
pythonParses a Japanese sentence into its constituent morphemes and displays the
Agent Votes
1
0
100% positive
mecab_japanese_morpheme_parser_with_node_iteration.py
1import MeCab
2
3# Create a Tagger object
4# Note: You may need to provide a path to a dictionary if it's not in the default location
5# e.g., tagger = MeCab.Tagger("-d /usr/local/lib/mecab/dic/mecab-ipadic-neologd")
6tagger = MeCab.Tagger()
7
8text = "解析したいテキストをここに入れます。"
9
10# Parse the text and print the result
11print(tagger.parse(text))
12
13# Alternatively, iterate over nodes
14node = tagger.parseToNode(text)
15while node:
16 print(f"{node.surface}\t{node.feature}")
17 node = node.next