-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbasic_types.py
More file actions
128 lines (104 loc) · 5.13 KB
/
Copy pathbasic_types.py
File metadata and controls
128 lines (104 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copyright: Ren Tatsumoto <tatsu at autistici.org> and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import dataclasses
import enum
import typing
from typing import Optional
class Separators:
# Separators that are passed as command line arguments to mecab and used to parse mecab's output.
component = "<ajt__component_separator>"
node = "<ajt__node_separator>"
footer = "<ajt__footer>"
class MecabAnalysis(typing.NamedTuple):
# What info about each word mecab should output to the user.
word: str
headword: str
katakana_reading: str
part_of_speech: str
inflection_type: str
COMPONENTS: typing.Final[MecabAnalysis] = MecabAnalysis(
word="%m",
headword="%f[6]",
katakana_reading="%f[7]",
part_of_speech="%f[0]",
inflection_type="%f[5]",
)
class PartOfSpeech(enum.Enum):
"""
Parts of speech that mecab can output.
"""
unknown = None
other = "その他" # e.g. よ, ァ
filler = "フィラー" # e.g. なんか, あのー, ま, えーと
adverb = "副詞" # e.g. たちまち, やや, あたかも, どんどん, 飽くまで
bound_auxiliary = "助動詞" # e.g らしい, です, ない, ます, たい, だ
particle = "助詞" # e.g. は, から, を, て
verb = "動詞" # e.g. 出る, 見せる, あげる, 雇う, 患う
noun = "名詞" # e.g. 全員, 国, 言語学, 代金. Note: non-i adjectives (e.g. 綺麗, 静か) are classified as nouns too.
i_adjective = "形容詞" # e.g. 広い, づらい, 近い, 深い, 怖い
interjection = "感動詞" # e.g. おはよう, ありがとう, お疲れ様
conjunction = "接続詞" # e.g. それなら, それより, ですけれど, ただし
prefix = "接頭詞" # e.g. 超, 無, 御, 前, 初
symbol = "記号" # e.g. (-_-;), ■, (^.^)/~~~, ¥, *
adnominal_adjective = "連体詞" # e.g. いかなる, ろくな, この, いろんな, 小さな
@classmethod
def _missing_(cls, value: object) -> "PartOfSpeech":
"""
If mecab for some reason outputs something that's not a member of this enum,
fall back to "unknown".
"""
return cls.unknown
ANY_ATTACHING = "接続"
@enum.unique
class Inflection(enum.Enum):
unknown = None
garu_attached = "ガル接続" # e.g 苦し (+ がる)
hypothetical = "仮定形" # e.g. 出かけれ (+ ば)
contraction_1 = "仮定縮約1" # e.g. 来れば => 来りゃ, ていれば => てりゃ
contraction_2 = "仮定縮約2" # e.g. なければ => なきゃ
nominal_connection = "体言接続" # たり => たる, 良い => 良き, らしい => らしき
nominal_connection_special = "体言接続特殊" # 負ける => 負けん, 戻れる => 戻れん, する => すん
nominal_connection_2 = "体言接続特殊2" # 変わる => 変わ, とちる => とち, 携わる => 携わ
imperative_e = "命令e" # に+なれ, (で+)あれ, (と+)思え, (ください+)ませ
imperative_i = "命令i" # (ご覧+)ください
imperative_ro = "命令ro" # (信用+)しろ, (を+)見せろ
imperative_yo = "命令yo" # (を+)助けよ, 与えよ, せよ
dictionary_form = "基本形" # する, 言う
modern_dictionary_form = "現代基本形"
classical_dictionary_form = "文語基本形"
sound_change_dictionary_form = "音便基本形"
dictionary_form_geminate_contraction = "基本形 - 促音便"
irrealis_u = "未然ウ接続"
irrealis_nu = "未然ヌ接続" # よから(+ぬ), (感謝+)せ(+ざる), 少なから(+ぬ)
irrealis_reru = "未然レル接続" # (解消+)さ(+れる), (失礼+)さ(+せ)
irrealis = "未然形" # (に+)持ち込ま(+れる), (しか+)知ら(+なかっ), (言葉+)行か(+なく)
irrealis_special = "未然特殊" # わかん+ない
continuative_gozai = "連用ゴザイ接続" # 有難う(+御座い)
continuative_ta = "連用タ接続" # 聞い(+て), (が+)分かっ(+て), (一度+)読ん(+で)
continuative_te = "連用テ接続" # (見え+)なく, (問題+)なく, 美味しく, 熱く
continuative_de = "連用デ接続" # (寝+)ない(+で), (変え+)ない(+で), (忘れ+)ない(+で)
continuative_ni = "連用ニ接続" # (消さ+)ず(+に)
continuative = "連用形" # 見つかり, 教わり, いただき, 探し(+た), (弄ば+)れ(+て)
@classmethod
def _missing_(cls, value: object) -> "Inflection":
"""
If mecab for some reason outputs something that's not a member of this enum,
fall back to "unknown".
"""
return cls.unknown
@dataclasses.dataclass(frozen=True)
class MecabParsedToken:
word: str
headword: str
katakana_reading: str | None # inflected reading
part_of_speech: PartOfSpeech
inflection_type: Inflection
assert tuple(field.name for field in dataclasses.fields(MecabParsedToken)) == tuple(type(COMPONENTS).__annotations__)
def main() -> None:
for k in COMPONENTS:
print(k)
print(PartOfSpeech("invalid"))
print(PartOfSpeech("副詞"))
print(PartOfSpeech("感動詞"))
if __name__ == "__main__":
main()