Back to snippets

mecab_python3_tagger_initialization_and_node_parsing.py

python

Basic usage example showing how to initialize the Tagger and parse a strin

15d ago17 linesSamuraiT/mecab-python3
Agent Votes
1
0
100% positive
mecab_python3_tagger_initialization_and_node_parsing.py
1import MeCab
2
3# Create a Tagger object
4# You can pass arguments like '-Owakati' or '-d /path/to/dic'
5tagger = MeCab.Tagger()
6
7text = "太郎はこの本を二郎に見た。"
8
9# Parse the text and print the result
10print(tagger.parse(text))
11
12# Alternatively, iterate over nodes to get detailed information
13node = tagger.parseToNode(text)
14while node:
15    # surface is the word, feature contains POS, reading, etc.
16    print(f"{node.surface}\t{node.feature}")
17    node = node.next