-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_inflections.py
428 lines (383 loc) · 17.1 KB
/
add_inflections.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import argparse
import gzip
import json
import sys
from functools import cached_property
from itertools import zip_longest
from pathlib import Path
from pyglossary.glossary_type import EntryType
from pyglossary.glossary_v2 import Glossary
from spylls.hunspell.data.aff import Aff
from spylls.hunspell.data.dic import Word
from spylls.hunspell.dictionary import Dictionary
SCRIPT_DIR = Path(__file__).resolve().parent
# Unmunched Hunspell dictionary format, see: https://github.com/anezih/HunspellWordForms
# [
# ...
# {
# "build": {
# "PFX": [],
# "SFX": [
# "building",
# "builds"
# ],
# "Cross": []
# }
# },
# {
# "builder": {
# "PFX": [],
# "SFX": [
# "builders",
# "builder's"
# ],
# "Cross": []
# }
# },
# ...
# ]
class InflBase:
def __init__(self, source_path: str, glos_format: str = "") -> None:
self.source_path = source_path
self.glos_format = glos_format
@cached_property
def path(self) -> Path:
return Path(self.source_path).resolve()
@cached_property
def InflDict(self) -> dict[str,list[str]]:
raise NotImplementedError
def get_infl(self, word: str, pfx: bool = False, cross: bool = False) -> set[str]:
raise NotImplementedError
class Unmunched(InflBase):
@cached_property
def InflDict(self) -> dict[str,dict[str,list[str]]]:
if not self.path.exists():
raise FileNotFoundError(f"Couldn't find Unmunched dictionary at: {self.source_path}")
temp: list[dict[str,dict[str,list[str]]]] = list()
if self.path.name.endswith(".gz"):
try:
with gzip.open(self.path, "rt", encoding="utf-8") as f:
temp: list[dict[str,dict[str,list[str]]]] = json.load(f)
except:
raise Exception("[!] Couldn't open gzipped json file. Check the filename/path.")
elif self.path.name.endswith(".json"):
try:
with open(self.path, "r", encoding="utf-8") as f:
temp: list[dict[str,dict[str,list[str]]]] = json.load(f)
except:
raise Exception("[!] Couldn't open json file. Check the filename/path.")
_infl_dict: dict[str,dict[str, list[str]]] = dict()
for it in temp:
for key, val in it.items():
if key in _infl_dict.keys():
_infl_dict[key]["PFX"] += val["PFX"]
_infl_dict[key]["SFX"] += val["SFX"]
_infl_dict[key]["Cross"] += val["Cross"]
else:
_infl_dict[key] = val
return _infl_dict
def get_infl(self, word: str, pfx: bool = False, cross: bool = False) -> set[str]:
afx = set()
afx_dict = self.InflDict.get(word)
if afx_dict:
afx.update(afx_dict["SFX"])
if pfx:
afx.update(afx_dict["PFX"])
if cross:
afx.update(afx_dict["Cross"])
return afx
class InflGlosSource(InflBase):
@cached_property
def InflDict(self) -> dict[str,set[str]]:
if not self.path.exists():
raise FileNotFoundError(f"Couldn't find InflGlosSource dictionary at: {self.source_path}")
glos = Glossary()
if self.glos_format:
glos.directRead(filename=str(self.path), format=self.glos_format)
else:
glos.directRead(filename=str(self.path))
_infl_dict: dict[str,set[str]] = dict()
for entry in glos:
if len(entry.l_word) > 1:
headword = entry.l_word[0]
if headword in _infl_dict.keys():
_infl_dict[headword].update(entry.l_word[1:])
else:
_infl_dict[headword] = set(entry.l_word[1:])
return _infl_dict
def get_infl(self, word: str, pfx: bool = False, cross: bool = False) -> set[str]:
return self.InflDict.get(word, set())
class HunspellDic(InflBase):
# Taken from: https://gist.github.com/zverok/c574b7a9c42cc17bdc2aa396e3edd21a
def unmunch(self, word: Word, aff: Aff) -> dict[str,dict[str,set[str]]]:
result = {
word.stem : {
"PFX" : set(),
"SFX" : set(),
"Cross" : set()
}
}
if aff.FORBIDDENWORD and aff.FORBIDDENWORD in word.flags:
return result
suffixes = [
suffix
for flag in word.flags
for suffix in aff.SFX.get(flag, [])
if suffix.cond_regexp.search(word.stem)
]
prefixes = [
prefix
for flag in word.flags
for prefix in aff.PFX.get(flag, [])
if prefix.cond_regexp.search(word.stem)
]
for suffix in suffixes:
root = word.stem[0:-len(suffix.strip)] if suffix.strip else word.stem
suffixed = root + suffix.add
if not (aff.NEEDAFFIX and aff.NEEDAFFIX in suffix.flags):
result[word.stem]["SFX"].add(suffixed)
secondary_suffixes = [
suffix2
for flag in suffix.flags
for suffix2 in aff.SFX.get(flag, [])
if suffix2.cond_regexp.search(suffixed)
]
for suffix2 in secondary_suffixes:
root = suffixed[0:-len(suffix2.strip)] if suffix2.strip else suffixed
result[word.stem]["SFX"].add(root + suffix2.add)
for prefix in prefixes:
root = word.stem[len(prefix.strip):]
prefixed = prefix.add + root
if not (aff.NEEDAFFIX and aff.NEEDAFFIX in prefix.flags):
result[word.stem]["PFX"].add(prefixed)
if prefix.crossproduct:
additional_suffixes = [
suffix
for flag in prefix.flags
for suffix in aff.SFX.get(flag, [])
if suffix.crossproduct and not suffix in suffixes and suffix.cond_regexp.search(prefixed)
]
for suffix in suffixes + additional_suffixes:
root = prefixed[0:-len(suffix.strip)] if suffix.strip else prefixed
suffixed = root + suffix.add
result[word.stem]["Cross"].add(suffixed)
secondary_suffixes = [
suffix2
for flag in suffix.flags
for suffix2 in aff.SFX.get(flag, [])
if suffix2.crossproduct and suffix2.cond_regexp.search(suffixed)
]
for suffix2 in secondary_suffixes:
root = suffixed[0:-len(suffix2.strip)] if suffix2.strip else suffixed
result[word.stem]["Cross"].add(root + suffix2.add)
return result
@cached_property
def InflDict(self) -> dict[str,dict[str,list[str]]]:
if not self.path.exists():
raise FileNotFoundError(f"Couldn't find Hunspell dictionary at: {self.source_path}")
base_name = self.path.parent / self.path.stem
hunspell_dictionary = Dictionary.from_files(str(base_name))
aff: Aff = hunspell_dictionary.aff
all_words: list[Word] = hunspell_dictionary.dic.words
results: list[dict[str,dict[str,set[str]]]] = list()
for word in all_words:
unmunched = self.unmunch(word, aff)
if any([unmunched[word.stem]["SFX"], unmunched[word.stem]["PFX"], unmunched[word.stem]["Cross"]]):
results.append(unmunched)
_infl_dict: dict[str,dict[str, set[str]]] = dict()
for it in results:
for key, val in it.items():
if key in _infl_dict.keys():
_infl_dict[key]["PFX"].update(val["PFX"])
_infl_dict[key]["SFX"].update(val["SFX"])
_infl_dict[key]["Cross"].update(val["Cross"])
else:
_infl_dict[key] = val
return _infl_dict
def get_infl(self, word: str, pfx: bool = False, cross: bool = False) -> set[str]:
afx = set()
afx_dict = self.InflDict.get(word)
if afx_dict:
afx.update(afx_dict["SFX"])
if pfx:
afx.update(afx_dict["PFX"])
if cross:
afx.update(afx_dict["Cross"])
return afx
class AddInflections:
def __init__(self, input_dictionary_path: str, input_dictionary_format: str,
output_format: str, pfx: bool, cross: bool, keep_existing_inflections: bool,
infl_glos_source_paths: list[str], infl_glos_formats: list[str],
hunspell_dic_paths: list[str], unmunched_path: str) -> None:
self.input_dictionary_path = input_dictionary_path
self.input_dictionary_format = input_dictionary_format
self.output_format = output_format
self.pfx = pfx
self.cross = cross
self.keep_existing_inflections = keep_existing_inflections
self.infl_glos_source_paths = infl_glos_source_paths
self.infl_glos_formats = infl_glos_formats
self.hunspell_dic_paths = hunspell_dic_paths
self.unmunched_path = unmunched_path
def get_path(self, path: str) -> Path:
p = Path(path).resolve()
if not p.exists():
raise FileNotFoundError(f"Couldn't find file at: {path}")
return p.resolve()
def sort_glos(self, glos: Glossary) -> list[EntryType]:
entrytype_lst = [g for g in glos if g.defiFormat != "b"]
entrytype_lst.sort(key=lambda x: (x.l_word[0].encode("utf-8").lower(), x.l_word[0]))
return entrytype_lst
@property
def OutputFormat(self) -> str:
return self.output_format if self.output_format else "Stardict"
@cached_property
def BaseName(self) -> str:
p = self.get_path(self.input_dictionary_path)
return p.stem
@cached_property
def OutDir(self) -> Path:
p = SCRIPT_DIR / f"{self.BaseName}_With_Inflections"
if not p.exists():
p.mkdir()
return p
@property
def OutPath(self) -> Path:
return self.OutDir / self.BaseName
@cached_property
def InflDicts(self) -> list[InflBase]:
infl_lst: list[InflBase] = list()
if self.unmunched_path:
infl_lst.append(Unmunched(source_path=self.unmunched_path))
if self.infl_glos_source_paths:
for p,f in zip_longest(self.infl_glos_source_paths, self.infl_glos_formats):
infl_lst.append(
InflGlosSource(source_path=p, glos_format=f)
)
if self.hunspell_dic_paths:
for hu in self.hunspell_dic_paths:
infl_lst.append(
HunspellDic(source_path=hu)
)
print(f"> Created {len(infl_lst)} inflection dictionaries.")
return infl_lst
def get_infl(self, word: str) -> set[str]:
res: set[str] = set()
for infl_dict in self.InflDicts:
res.update(infl_dict.get_infl(word, self.pfx, self.cross))
if word in res:
res.remove(word)
return res
@cached_property
def InputGlos(self) -> Glossary:
glos = Glossary()
glos.directRead(self.input_dictionary_path, self.input_dictionary_format)
print("> Read input dictionary.")
return glos
@cached_property
def SortedInputGlos(self) -> list[EntryType]:
_sorted = self.sort_glos(self.InputGlos)
print("> Sorted input dictionary.")
return _sorted
@cached_property
def InputGlosLength(self) -> int:
return len(self.SortedInputGlos)
@cached_property
def OutputGlos(self) -> Glossary:
glos_syn = Glossary()
glos_syn.setInfo("title", self.InputGlos.getInfo("title"))
glos_syn.setInfo("description", self.InputGlos.getInfo("description"))
glos_syn.setInfo("author", self.InputGlos.getInfo("author"))
for data_entry in self.InputGlos:
if data_entry.defiFormat == "b":
glos_syn.addEntry(
glos_syn.newDataEntry(
data_entry.s_word, data_entry.data
)
)
print("> Created output dictionary.")
return glos_syn
def main(self) -> None:
cnt = 0
total_new_inflections_found = 0
for entry in self.SortedInputGlos:
headword = entry.l_word[0]
inflections = self.get_infl(headword)
total_new_inflections_found += len(inflections)
if self.keep_existing_inflections and (len(entry.l_word) > 1):
inflections.update(entry.l_word[1:])
l_word = [headword, *inflections]
self.OutputGlos.addEntry(
self.OutputGlos.newEntry(
word=l_word,
defi=entry.defi,
defiFormat=entry.defiFormat
)
)
cnt += 1
print(
f"\r> Processed {cnt:,} / {self.InputGlosLength:,} words. Total new inflections found: {total_new_inflections_found:,}",
end="\r")
print("")
if self.output_format == "Stardict":
self.OutputGlos.write(str(self.OutPath), format=self.output_format, dictzip=False)
else:
self.OutputGlos.write(str(self.OutPath), format=self.output_format)
class ArgparseNS:
input_dictionary_path: str = None
input_dictionary_format: str = None
output_format: str = "Stardict"
pfx: bool = False
cross: bool = False
keep_existing_inflections: bool = False
infl_glos_source_paths: list[str] = list()
infl_glos_formats: list[str] = list()
hunspell_dic_paths: list[str] = list()
unmunched_path: str = None
if __name__ == '__main__':
Glossary.init()
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, description=
"""
Add inflection information to any dictionary format supported by PyGlossary.
""")
parser.add_argument("-i", "--input-dictionary", dest="input_dictionary_path",
help="Input dictionary path.", required=True)
parser.add_argument("-u", "--unmunched-json", dest="unmunched_path",
help=f"<language>.json(.gz)")
parser.add_argument("--glos-infl-sources", dest="infl_glos_source_paths", nargs="+", default=list(),
help="""Paths of dictionaries that will be used as an inflection source by-themselves or together with unmunched json file.
Separate multiple sources with a space between them.""")
parser.add_argument("--glos-infl-source-formats", dest="infl_glos_formats", nargs="+", default=list(),
choices=Glossary.readFormats, metavar="", help="""--glos-infl-sources dictionary format(s),
allowed values are same as --input-format. Separate multiple formats with a space between them.""")
parser.add_argument("-hu", "--hunspell-dic-paths", dest="hunspell_dic_paths", nargs="+", default=list(),
metavar="", help="""Paths of the Hunspell .dic or .aff files.
Separate multiple sources with a space between them.""")
parser.add_argument("--input-format", dest="input_dictionary_format",
default=None, choices=Glossary.readFormats,
help=f"Allowed values: {', '.join(Glossary.readFormats)}", metavar="")
parser.add_argument("--output-format", dest="output_format", default="Stardict",
choices=Glossary.writeFormats,
help=f"Allowed values: {', '.join(Glossary.writeFormats)}", metavar="")
parser.add_argument("-p", "--add-prefixes", dest="pfx", action="store_true", default=False,
help="""Add prefixes from the unmunched json.""")
parser.add_argument("-c", "--add-cross-products", dest="cross", action="store_true", default=False,
help="""Add cross products from the unmunched json.""")
parser.add_argument("-k", "--keep", dest="keep_existing_inflections", action="store_true", default=False,
help="Keep existing inflections.")
args = parser.parse_args(namespace=ArgparseNS)
if not (args.unmunched_path or args.infl_glos_source_paths or args.hunspell_dic_paths):
sys.exit("[!] You need to specify at least one inflection source: --unmunched-json, --glos-infl-sources, --hunspell-paths or all.")
add_inflections = AddInflections(
input_dictionary_path=args.input_dictionary_path,
input_dictionary_format=args.input_dictionary_format,
output_format=args.output_format,
pfx=args.pfx,
cross=args.cross,
keep_existing_inflections=args.keep_existing_inflections,
infl_glos_source_paths=args.infl_glos_source_paths,
infl_glos_formats=args.infl_glos_formats,
hunspell_dic_paths=args.hunspell_dic_paths,
unmunched_path=args.unmunched_path
)
add_inflections.main()