-
Notifications
You must be signed in to change notification settings - Fork 12
/
greek_inflexion.py
214 lines (192 loc) · 7.94 KB
/
greek_inflexion.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
from collections import defaultdict
import re
from inflexion import Inflexion
from inflexion.lexicon import Lexicon
from accent import calculate_accent, strip_accents, debreath, rebreath
from fileformat import load_stemming, load_lexicon
class GreekInflexion:
def __init__(self, stemming_file, lexicon_file=None, strip_length=False):
self.ruleset = load_stemming(stemming_file, strip_length)
if lexicon_file:
(
self.lexicon, self.form_override, self.accent_override,
self.segmented_lemmas
) = load_lexicon(lexicon_file, pre_processor=debreath)
else:
self.lexicon = Lexicon()
self.form_override = {}
self.accent_override = defaultdict(list)
self.segmented_lemmas = {}
self.inflexion = Inflexion()
self.inflexion.add_lexicon(self.lexicon)
self.inflexion.add_stemming_rule_set(self.ruleset)
def find_stems(self, lemma, key, tags=None):
return self.lexicon.find_stems(
lemma, key, tags, stem_post_processor=rebreath)
def generate(self, lemma, key, tags=None):
overrides = self.form_override.get((lemma, key))
if overrides:
if isinstance(overrides, str):
overrides = [overrides]
return {
override: [{"override": "form"}]
for override in overrides
}
generated = defaultdict(list)
for orig_form, details in self.inflexion.generate(
lemma, key, tags).items():
for detail in details:
segmented_lemma = self.segmented_lemmas.get(lemma)
accent_form, accent_notes = calculate_accent(
orig_form, key, lemma, segmented_lemma, detail["stem"],
self.inflexion, self.accent_override)
detail.update({"original_form": orig_form})
detail.update({"accent_notes": accent_notes})
generated[accent_form].append(detail)
return generated
def possible_stems(self, form, key_regex=None):
for key, stem in self.ruleset.possible_stems(debreath(form)):
if key_regex is None or re.match(key_regex, key):
if stem != "h":
yield key, rebreath(strip_accents(stem))
def possible_stems2(self, form, key_regex=None):
for key, stem in self.ruleset.possible_stems2(debreath(form)):
if key_regex is None or re.match(key_regex, key):
yield key, rebreath(strip_accents(stem))
def parse(self, form):
return self.inflexion.parse(
debreath(form), stem_post_processor=strip_accents)
def conjugate(self, lemma, *TVMs, tags=None):
print("-")
print(" lemma: {}".format(lemma))
if tags:
print()
print(" tags:")
for tag in tags:
print(" - {}".format(tag))
for TVM in TVMs:
print()
if TVM[2] in "ISO":
for PN in ["1S", "2S", "3S", "1P", "2P", "3P"]:
parse = TVM + "." + PN
form = "/".join(
self.generate(lemma, parse, tags=tags).keys())
if form:
print(" {}: {}".format(parse, form))
elif TVM[2] == "D":
if "." not in TVM:
for PN in ["2S", "3S", "2P", "3P"]:
parse = TVM + "." + PN
form = "/".join(
self.generate(lemma, parse, tags=tags).keys())
if form:
print(" {}: {}".format(parse, form))
else:
form = "/".join(
self.generate(lemma, parse, tags=tags).keys())
if form:
print(" {}: {}".format(TVM, form))
elif TVM[2] == "N":
parse = TVM
form = "/".join(
self.generate(lemma, parse, tags=tags).keys())
if form:
print(" {}: {}".format(parse, form))
elif TVM[2] == "P":
if TVM.endswith(".N"):
for NG in ["SM", "SF", "SN"]:
parse = TVM + NG
form = "/".join(
self.generate(lemma, parse, tags=tags).keys())
if form:
print(" {}: {}".format(parse, form))
else:
for CNG in ["NSM", "NSF", "NSN", "GSM", "GSF", "GSN"]:
parse = TVM + "." + CNG
form = "/".join(
self.generate(lemma, parse, tags=tags).keys())
if form:
print(" {}: {}".format(parse, form))
print()
print()
def conjugate_core(self, lemma, *TVMs, tags=None):
result = []
for TVM in TVMs:
out = {}
if TVM[2] in "ISO":
for PN in ["1S", "2S", "3S", "1P", "2P", "3P"]:
parse = TVM + "." + PN
form = "/".join(
self.generate(lemma, parse, tags=tags).keys())
if form:
out[parse] = form
elif TVM[2] == "D":
if "." not in TVM:
for PN in ["2S", "3S", "2P", "3P"]:
parse = TVM + "." + PN
form = "/".join(
self.generate(lemma, parse, tags=tags).keys())
if form:
out[parse] = form
else:
form = "/".join(
self.generate(lemma, parse, tags=tags).keys())
if form:
out[parse] = form
elif TVM[2] == "N":
parse = TVM
form = "/".join(
self.generate(lemma, parse, tags=tags).keys())
if form:
out[parse] = form
elif TVM[2] == "P":
if TVM.endswith(".N"):
for NG in ["SM", "SF", "SN"]:
parse = TVM + NG
form = "/".join(
self.generate(lemma, parse, tags=tags).keys())
if form:
out[parse] = form
else:
for CNG in ["NSM", "NSF", "NSN", "GSM", "GSF", "GSN"]:
parse = TVM + "." + CNG
form = "/".join(
self.generate(lemma, parse, tags=tags).keys())
if form:
out[parse] = form
result.append((TVM, out))
return result
def decline_core(self, lemma, TVM, tags=None):
if TVM[2] != "P":
raise ValueError
result = []
for G in "MFN":
forms = {}
for CN in [
"NS", "GS", "DS", "AS", "VS",
"NP", "GP", "DP", "AP", "VP"
]:
parse = TVM + "." + CN + G
form = "/".join(
self.generate(lemma, parse, tags=tags).keys())
if form:
forms[parse] = form
result.append(forms)
return result
def decline(self, lemma, TVM, tags=None):
if TVM[2] != "P":
raise ValueError
print("-")
print(" lemma: {}".format(lemma))
for G in "MFN":
print()
for CN in [
"NS", "GS", "DS", "AS", "VS",
"NP", "VP", "GP", "DP", "AP"
]:
parse = TVM + "." + CN + G
form = "/".join(
self.generate(lemma, parse, tags=tags).keys())
print(" {}: {}".format(parse, form))
print()
print()