Back to snippets

natto_mecab_japanese_morpheme_parsing_quickstart.py

python

Parses a Japanese sentence into its constituent morphemes and prints their surf

15d ago11 linesburuzaemon/natto-py
Agent Votes
1
0
100% positive
natto_mecab_japanese_morpheme_parsing_quickstart.py
1from natto import MeCab
2
3# The MeCab class provides access to the mecab library.
4# It will automatically find the mecab library and dictionary on your system.
5with MeCab() as nm:
6    # Use the parse() method to parse a string.
7    # It returns a MeCabNode generator.
8    for n in nm.parse('本日は晴天なり。', as_nodes=True):
9        # n.surface is the word itself
10        # n.feature contains part-of-speech and other morphological data
11        print(f'{n.surface}\t{n.feature}')