-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeclension.py
325 lines (293 loc) · 12 KB
/
declension.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
from collections import OrderedDict
AKKADIAN = {
'short_vowels': ['a', 'e', 'i', 'u'],
'macron_vowels': ['ā', 'ē', 'ī', 'ū'],
'circumflex_vowels': ['â', 'ê', 'î', 'û'],
'consonants': ['b', 'd', 'g', 'ḫ', 'k', 'l', 'm',
'n', 'p', 'q', 'r', 's', 'ṣ', 'š',
't', 'ṭ', 'w', 'y', 'z', 'ʾ']
}
ENDINGS = {
'm': {
'singular': {
'nominative': 'um',
'accusative': 'am',
'genitive': 'im'
},
'dual': {
'nominative': 'ān',
'oblique': 'īn'
},
'plural': {
'nominative': 'ū',
'oblique': 'ī'
}
},
'f': {
'singular': {
'nominative': 'tum',
'accusative': 'tam',
'genitive': 'tim'
},
'dual': {
'nominative': 'tān',
'oblique': 'tīn'
},
'plural': {
'nominative': ['ātum', 'ētum', 'ītum'],
'oblique': ['ātim', 'ētim', 'ītum']
}
}
}
class Syllabifier(object):
"""Split Akkadian words into list of syllables"""
def __init__(self, language=AKKADIAN):
self.language = language
def _is_consonant(self, char):
return char in self.language['consonants']
def _is_vowel(self, char):
return char in self.language['short_vowels'] + \
self.language['macron_vowels'] + \
self.language['circumflex_vowels']
def syllabify(self, word):
syllables = []
# If there's an initial vowel and the word is longer than 2 letters,
# and the third syllable is a not consonant (easy way to check for VCC pattern),
# the initial vowel is the first syllable.
# Rule (b.ii)
if self._is_vowel(word[0]):
if len(word) > 2 and not self._is_consonant(word[2]):
syllables.append(word[0])
word = word[1:]
# flip the word and count from the back:
word = word[::-1]
# Here we iterate over the characters backwards trying to match
# consonant and vowel patterns in a hierarchical way.
# Each time we find a match we store the syllable (in reverse order)
# and move the index ahead the length of the syllable.
syllables_reverse = []
i = 0
while i < len(word):
char = word[i]
# CV:
if self._is_vowel(char):
syllables_reverse.append(word[i + 1] + word[i])
i += 2
# CVC and VC:
elif self._is_consonant(char):
if self._is_vowel(word[i + 1]):
# If there are only two characters left, that's it.
if i + 2 >= len(word):
syllables_reverse.append(word[i + 1] + word[i])
break
# CVC
elif self._is_consonant(word[i + 2]):
syllables_reverse.append(word[i + 2] + word[i + 1] + word[i])
i += 3
# VC (remember it's backwards here)
elif self._is_vowel(word[i + 2]):
syllables_reverse.append(word[i + 1] + word[i])
i += 2
return syllables + syllables_reverse[::-1]
syll = Syllabifier()
def get_cv_pattern(word, pprint=False):
# input = iparras
# pattern = [('V', 1, 'i'), ('C', 1, 'p'), ('V', 2, 'a'), ('C', 2, 'r'),
# ('C', 2, 'r'), ('V', 2, 'a'), ('C', 3, 's')]
# pprint = V₁C₁V₂C₂C₂V₂C₃
subscripts = {
1: '₁',
2: '₂',
3: '₃',
4: '₄',
5: '₅',
6: '₆',
7: '₇',
8: '₈',
9: '₉',
0: '₀'
}
pattern = []
c_count = 1
v_count = 1
for char in word:
if char in AKKADIAN['consonants']:
cv = 'C'
else:
cv = 'V'
# remove length:
if char in AKKADIAN['macron_vowels']:
char = AKKADIAN['short_vowels'][AKKADIAN['macron_vowels'].index(char)]
elif char in AKKADIAN['circumflex_vowels']:
char = AKKADIAN['short_vowels'][AKKADIAN['circumflex_vowels'].index(char)]
if char not in [x[2] for x in pattern]:
if cv == 'C':
count = c_count
c_count += 1
elif cv == 'V':
count = v_count
v_count += 1
pattern.append((cv, count, char))
elif char in [x[2] for x in pattern]:
pattern.append((cv, next(x[1] for x in pattern if x[2] == char), char))
if pprint:
output = ''
for item in pattern:
output += (item[0] + subscripts[item[1]])
return output
return pattern
def get_stem(noun, gender, mimation=True):
stem = ''
if mimation and noun[-1:] == 'm':
# noun = noun[:-1]
pass
# Take off ending
if gender == 'm':
if noun[-2:] in list(ENDINGS['m']['singular'].values()) + \
list(ENDINGS['m']['dual'].values()):
stem = noun[:-2]
elif noun[-1] in list(ENDINGS['m']['plural'].values()):
stem = noun[:-1]
else:
print("Unknown masculine noun: {}".format(noun))
elif gender == 'f':
if noun[-4:] in ENDINGS['f']['plural']['nominative'] + \
ENDINGS['f']['plural']['oblique']:
stem = noun[:-4] + 't'
elif noun[-3:] in list(ENDINGS['f']['singular'].values()) + \
list(ENDINGS['f']['dual'].values()):
stem = noun[:-3] + 't'
elif noun[-2:] in list(ENDINGS['m']['singular'].values()) + \
list(ENDINGS['m']['dual'].values()):
stem = noun[:-2]
else:
print("Unknown feminine noun: {}".format(noun))
else:
print("Unknown noun: {}".format(noun))
return stem
def get_bound_form(noun, gender):
syllables = syll.syllabify(noun)
stem = get_stem(noun, gender)
cv = get_cv_pattern(stem)
# Based on Huehnergard Appendix 6.C.1: base in -VC
if [letter[0] for letter in cv[-2:]] == ['V', 'C'] or stem in ['nakr']:
# a. 2-syllable
if len(syllables) > 2:
# awīlum > awīl, nakrum > naker
if stem in ['nakr']:
return 'naker'
else:
return stem
# b. 1-syllable
elif len(syllables) > 1:
# bēlum > bēl
return stem
# c. abum, aḫum
if stem in ['ab', 'aḫ']:
return stem + 'i'
# Appendix 6.C.2: base in -C₁C₁
if cv[-1][:2] == cv[-2][:2]:
# a. 1-syllable
if 3 > len(syllables) > 1:
return stem + 'i'
# b. 2-syllable, -tt
if len(syllables) > 2 and cv[-1][2] + cv[-2][2] == 'tt':
return stem + 'i'
# c. 2-syllable, other
if len(syllables) > 2:
return stem[:-1]
# Appendix 6.C.3: base in -C₁C₂, C₂ ≠ t, i.e. pVrs
if cv[-1][0] == cv[-2][0] and cv[-1][1] != cv[-2][1]:
return stem[:-1] + stem[1] + stem[-1]
# Appendix 6.C.4: base in -Ct (fem.)
if cv[-1][2] == 't' and cv[-2][0] == 'C':
if len(syllables) > 2:
return stem + 'i'
# Need to deal with fem. Ptcpl. māḫirtum -> māḫirat
if len(syllables) > 1:
# These are case by case
if stem in ['qīšt']:
return stem + 'i'
if stem in ['mārt']:
return stem[:-1] + stem[1] + stem[-1]
# Appendix 6.C.5: base in -V
# Weak nouns...
def decline_noun(noun, gender, case=None, number=None, mimation=True):
stem = get_stem(noun, gender)
declension = []
for case in ENDINGS[gender]['singular']:
if gender == 'm':
form = stem + ENDINGS[gender]['singular'][case]
else:
form = stem + ENDINGS[gender]['singular'][case][1:]
declension.append((form, {'case': case, 'number': 'singular'}))
for case in ENDINGS[gender]['dual']:
if gender == 'm':
form = stem + ENDINGS[gender]['dual'][case]
else:
form = stem + ENDINGS[gender]['dual'][case][1:]
declension.append((form, {'case': case, 'number': 'dual'}))
for case in ENDINGS[gender]['plural']:
if gender == 'm':
form = stem + ENDINGS[gender]['plural'][case]
else:
if stem[-3] in AKKADIAN['macron_vowels']:
theme_vowel = stem[-3]
else:
theme_vowel = 'ā'
ending = [x for x in ENDINGS[gender]['plural'][case] if x[0] == theme_vowel]
if stem[-2] in AKKADIAN['short_vowels']:
form = stem[:-2] + ending[0]
elif stem[-1] in AKKADIAN['consonants'] and stem[-2] in AKKADIAN['macron_vowels']:
form = stem + ending[0]
else:
form = stem[:-1] + ending[0]
declension.append((form, {'case': case, 'number': 'plural'}))
return declension
def test_get_stem():
print(get_stem('ilum', 'm') == 'il')
print(get_stem('šarrū', 'm') == 'šarr')
print(get_stem('ilātum', 'f') == 'ilt')
print(get_stem('bēltān', 'f') == 'bēlt')
def test_decline_noun():
print(sorted(decline_noun('ilum', 'm')) ==
sorted([('ilim', {'case': 'genitive', 'number': 'singular'}),
('ilum', {'case': 'nominative', 'number': 'singular'}),
('ilam', {'case': 'accusative', 'number': 'singular'}),
('ilīn', {'case': 'oblique', 'number': 'dual'}),
('ilān', {'case': 'nominative', 'number': 'dual'}),
('ilī', {'case': 'oblique', 'number': 'plural'}),
('ilū', {'case': 'nominative', 'number': 'plural'})]))
print(sorted(decline_noun('šarrum', 'm')) ==
sorted([('šarrim', {'case': 'genitive', 'number': 'singular'}),
('šarrum', {'case': 'nominative', 'number': 'singular'}),
('šarram', {'case': 'accusative', 'number': 'singular'}),
('šarrīn', {'case': 'oblique', 'number': 'dual'}),
('šarrān', {'case': 'nominative', 'number': 'dual'}),
('šarrī', {'case': 'oblique', 'number': 'plural'}),
('šarrū', {'case': 'nominative', 'number': 'plural'})]))
print(sorted(decline_noun('iltum', 'f')) ==
sorted([('iltim', {'case': 'genitive', 'number': 'singular'}),
('iltum', {'case': 'nominative', 'number': 'singular'}),
('iltam', {'case': 'accusative', 'number': 'singular'}),
('iltīn', {'case': 'oblique', 'number': 'dual'}),
('iltān', {'case': 'nominative', 'number': 'dual'}),
('ilātim', {'case': 'oblique', 'number': 'plural'}),
('ilātum', {'case': 'nominative', 'number': 'plural'})]))
print(sorted(decline_noun('šarratum', 'f')) ==
sorted([('šarratim', {'case': 'genitive', 'number': 'singular'}),
('šarratum', {'case': 'nominative', 'number': 'singular'}),
('šarratam', {'case': 'accusative', 'number': 'singular'}),
('šarratīn', {'case': 'oblique', 'number': 'dual'}),
('šarratān', {'case': 'nominative', 'number': 'dual'}),
('šarrātim', {'case': 'oblique', 'number': 'plural'}),
('šarrātum', {'case': 'nominative', 'number': 'plural'})]))
print(sorted(decline_noun('nārum', 'f')) ==
sorted([('nārim', {'case': 'genitive', 'number': 'singular'}),
('nārum', {'case': 'nominative', 'number': 'singular'}),
('nāram', {'case': 'accusative', 'number': 'singular'}),
('nārīn', {'case': 'oblique', 'number': 'dual'}),
('nārān', {'case': 'nominative', 'number': 'dual'}),
('nārātim', {'case': 'oblique', 'number': 'plural'}),
('nārātum', {'case': 'nominative', 'number': 'plural'})]
))