-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreplace_mistakes.py
258 lines (247 loc) · 9.79 KB
/
replace_mistakes.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Copyright: Ajatt-Tools and contributors; https://github.com/Ajatt-Tools
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import dataclasses
from collections.abc import Iterable, Sequence
from typing import Optional
try:
from .basic_types import Inflection, MecabParsedToken, PartOfSpeech
except ImportError:
from basic_types import Inflection, MecabParsedToken, PartOfSpeech
@dataclasses.dataclass
class WrappedToken:
token: MecabParsedToken
skip: bool = False
def replace_mistakes(tokens: Iterable[MecabParsedToken]) -> Iterable[MecabParsedToken]:
consumed = tuple(WrappedToken(token) for token in tokens)
for idx, wrapped in enumerate(consumed):
if wrapped.skip:
continue
yield from replace_mistake(wrapped.token, consumed, idx)
def slice_headwords(context: Sequence[WrappedToken], start: int, end: int) -> Optional[tuple[str, ...]]:
try:
return tuple(context[idx].token.headword for idx in range(start, end))
except IndexError:
return None
def take_headword(context: Sequence[WrappedToken], pos: int) -> Optional[str]:
try:
return context[pos].token.headword
except IndexError:
return None
def replace_mistake(token: MecabParsedToken, context: Sequence[WrappedToken], pos: int) -> Iterable[MecabParsedToken]:
if token.word == "放っ" and slice_headwords(context, pos + 1, pos + 3) in (("て", "おく"), ("て", "おける")):
yield dataclasses.replace(token, headword="放る", katakana_reading="ホウッ")
elif token.word == "歩いた" and token.headword == "歩み板":
yield dataclasses.replace(
token,
headword="歩く",
katakana_reading="アルイタ",
part_of_speech=PartOfSpeech.verb,
inflection_type=Inflection.continuative_ta,
)
elif token.word == "しろっ" and "て" == take_headword(context, pos + 1):
# しろって
yield MecabParsedToken(
word="しろ",
headword="する",
katakana_reading="シロ",
part_of_speech=PartOfSpeech.verb,
inflection_type=Inflection.imperative_ro,
)
yield MecabParsedToken(
word="って",
headword="って",
katakana_reading="ッテ",
part_of_speech=PartOfSpeech.particle,
inflection_type=Inflection.unknown,
)
elif token.word == "はおら" and token.headword == "はおる" and "ぬ" == take_headword(context, pos + 1):
# Xはおらぬ
yield MecabParsedToken(
word="は",
headword="は",
katakana_reading="ハ",
part_of_speech=PartOfSpeech.particle,
inflection_type=Inflection.unknown,
)
yield MecabParsedToken(
word="おら",
headword="おる",
katakana_reading="オラ",
part_of_speech=PartOfSpeech.verb,
inflection_type=Inflection.irrealis,
)
elif token.word == "降り" and token.katakana_reading == "オリ" and "が" == take_headword(context, pos - 1):
# 雪が降りました: オ=>フ
yield dataclasses.replace(
token,
headword="降る",
katakana_reading="フリ",
)
elif token.word == "旅立て" and token.headword == "旅立てる":
# 旅立てる isn't listed in the pitch accent database; replace it with 旅立つ
yield dataclasses.replace(
token,
headword="旅立つ",
)
elif token.word == "羽" and take_headword(context, pos + 1) == "撃":
context[pos + 1].skip = True
yield MecabParsedToken(
word="羽撃",
headword="羽撃く",
katakana_reading="ハバタ",
part_of_speech=PartOfSpeech.verb,
inflection_type=Inflection.irrealis,
)
elif token.word == "阿良" and slice_headwords(context, pos + 1, pos + 3) == ("々", "木"):
context[pos + 1].skip = True
context[pos + 2].skip = True
yield MecabParsedToken(
word="阿良々木",
headword="阿良々木",
katakana_reading="アララギ",
part_of_speech=PartOfSpeech.noun,
inflection_type=Inflection.dictionary_form,
)
elif token.word == "乗り" and slice_headwords(context, pos + 1, pos + 3) == ("込", "え"):
context[pos + 1].skip = True
context[pos + 2].skip = True
yield MecabParsedToken(
word="乗り込え",
headword="乗り込える",
katakana_reading="ノリコエ",
part_of_speech=PartOfSpeech.verb,
inflection_type=Inflection.continuative,
)
elif (
token.word == "助"
and token.katakana_reading == "スケ"
and slice_headwords(context, pos + 1, pos + 3) == ("から", "ない")
):
context[pos + 1].skip = True
yield MecabParsedToken(
word="助から",
headword="助かる",
katakana_reading="タスカラ",
part_of_speech=PartOfSpeech.verb,
inflection_type=Inflection.irrealis,
)
elif token.word == "いい気" and take_headword(context, pos + 1) == "分":
context[pos + 1].skip = True
yield MecabParsedToken(
word="いい",
headword="いい",
katakana_reading="イイ",
part_of_speech=PartOfSpeech.i_adjective,
inflection_type=Inflection.dictionary_form,
)
yield MecabParsedToken(
word="気分",
headword="気分",
katakana_reading="キブン",
part_of_speech=PartOfSpeech.noun,
inflection_type=Inflection.dictionary_form,
)
elif token.word == "しや" and token.headword == "視野":
yield dataclasses.replace(
token,
headword="してやる",
part_of_speech=PartOfSpeech.verb,
inflection_type=Inflection.nominal_connection_2,
)
elif token.word == "いいっ" and token.headword == "いい":
yield MecabParsedToken(
word="いい",
headword="いい",
katakana_reading="イイ",
part_of_speech=PartOfSpeech.i_adjective,
inflection_type=Inflection.dictionary_form,
)
yield MecabParsedToken(
word="っ",
headword="っ",
katakana_reading="ッ",
part_of_speech=PartOfSpeech.unknown,
inflection_type=Inflection.unknown,
)
elif token.word == "本当のところ":
yield MecabParsedToken(
word="本当",
headword="本当",
katakana_reading="ホントウ",
part_of_speech=PartOfSpeech.noun,
inflection_type=Inflection.dictionary_form,
)
yield MecabParsedToken(
word="の",
headword="の",
katakana_reading="ノ",
part_of_speech=PartOfSpeech.particle,
inflection_type=Inflection.unknown,
)
yield MecabParsedToken(
word="ところ",
headword="ところ",
katakana_reading="トコロ",
part_of_speech=PartOfSpeech.noun,
inflection_type=Inflection.dictionary_form,
)
elif token.word == "有り難う" and token.katakana_reading == "アリガタウ":
yield dataclasses.replace(token, katakana_reading="アリガトウ")
elif token.word == "出て" and token.headword == "出し手" and token.katakana_reading == "ダシテ":
yield dataclasses.replace(token, headword="出る", katakana_reading="デテ")
elif token.word == "悪い" and token.katakana_reading == "アクイ" and token.headword == "悪意":
yield dataclasses.replace(token, headword="悪い", katakana_reading="ワルイ")
elif token.word == "では" and token.katakana_reading == "デハ" and token.headword == "出端":
yield MecabParsedToken(
word="で",
headword="で",
katakana_reading="デ",
part_of_speech=PartOfSpeech.particle,
inflection_type=Inflection.unknown,
)
yield MecabParsedToken(
word="は",
headword="は",
katakana_reading="ハ",
part_of_speech=PartOfSpeech.particle,
inflection_type=Inflection.unknown,
)
elif token.word == "いた目" and token.katakana_reading == "イタメ" and token.headword == "板目":
yield MecabParsedToken(
word="い",
headword="いる",
katakana_reading="イ",
part_of_speech=PartOfSpeech.verb,
inflection_type=Inflection.continuative,
)
yield MecabParsedToken(
word="た",
headword="た",
katakana_reading="タ",
part_of_speech=PartOfSpeech.bound_auxiliary,
inflection_type=Inflection.continuative,
)
yield MecabParsedToken(
word="目",
headword="目",
katakana_reading="メ",
part_of_speech=PartOfSpeech.noun,
inflection_type=Inflection.unknown,
)
elif token.word == "軽そう" and token.katakana_reading == "ケイソウ" and token.headword == "軽装":
yield MecabParsedToken(
word="軽",
headword="軽い",
katakana_reading="カル",
part_of_speech=PartOfSpeech.i_adjective,
inflection_type=Inflection.garu_attached,
)
yield MecabParsedToken(
word="そう",
headword="そう",
katakana_reading="ソウ",
part_of_speech=PartOfSpeech.adverb,
inflection_type=Inflection.unknown,
)
else:
yield token