Last active
December 4, 2022 14:45
-
-
Save mkpoli/1de472d7ca7595e46427c2c1b4c098a0 to your computer and use it in GitHub Desktop.
MeCab の parseToNode の Python の generator の wrapper
This file contains hidden or 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 | |
| from typing import Generator | |
| from dataclasses import dataclass | |
| tagger = MeCab.Tagger() | |
| @dataclass(frozen=True) | |
| class Node: | |
| feature: str | |
| surface: str | |
| def parse(string: str) -> Generator[Node, None, None]: | |
| nodes = tagger.parseToNode(string) | |
| while nodes: | |
| yield Node(nodes.feature, nodes.surface) | |
| nodes = nodes.next | |
| for node in parse("日本語の文"): | |
| print(node.feature) |
This file contains hidden or 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 | |
| from typing import Generator | |
| from dataclasses import dataclass | |
| tagger = MeCab.Tagger() | |
| @dataclass(frozen=True) | |
| class Feature: | |
| pos1: str | |
| pos2: Optional[str] = None | |
| pos3: Optional[str] = None | |
| pos4: Optional[str] = None | |
| cType: Optional[str] = None | |
| cForm: Optional[str] = None | |
| lForm: Optional[str] = None | |
| lemma: Optional[str] = None | |
| orth: Optional[str] = None | |
| pron: Optional[str] = None | |
| kana: Optional[str] = None | |
| goshu: Optional[str] = None | |
| orthBase: Optional[str] = None | |
| pronBase: Optional[str] = None | |
| kanaBase: Optional[str] = None | |
| formBase: Optional[str] = None | |
| iType: Optional[str] = None | |
| iForm: Optional[str] = None | |
| iConType: Optional[str] = None | |
| fType: Optional[str] = None | |
| fForm: Optional[str] = None | |
| fConType: Optional[str] = None | |
| aType: Optional[str] = None | |
| aConType: Optional[str] = None | |
| aModType: Optional[str] = None | |
| @dataclass(frozen=True) | |
| class Node: | |
| feature: Feature | |
| surface: str | |
| def parse(string: str) -> Generator[Node, None, None]: | |
| nodes = tagger.parseToNode(string) | |
| while nodes: | |
| features = nodes.feature.split(",") | |
| def format(value: str) -> Optional[str]: | |
| return value if value != "*" else None | |
| feature = Feature(*list(map(format, features))[0:25]) | |
| yield Node(feature, nodes.surface) | |
| nodes = nodes.next |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment