-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_json.py
executable file
·215 lines (174 loc) · 4.84 KB
/
make_json.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
#!/usr/bin/env python3
from collections import defaultdict
import json
import yaml
def humanize_pos(pos):
return {
"A-": "adjective",
"C-": "conjunction",
"D-": "adverb",
"N-": "noun",
"P-": "preposition",
"RA": "article",
"RD": "demonstrative",
"RI": "indefinite/interrogative pronoun",
"RP": "personal pronoun",
"RR": "relative pronoun",
"V-": "verb",
"X-": "particle",
}[pos]
CASES = {
"N": "nominative",
"G": "genitive",
"D": "dative",
"A": "accusative",
"V": "vocative",
}
NUMBERS = {
"S": "singular",
"P": "plural",
}
GENDERS = {
"M": "masculine",
"F": "feminine",
"N": "neuter",
"-": "",
}
DEGREES = {
"-": "",
}
ASPECTS_TENSES = {
"A": "aorist",
"P": "present",
"F": "future",
"I": "imperfect",
"X": "perfect",
}
ASPECTS = {
"P": "continuous",
"A": "aorist",
"X": "perfect"
}
VOICES = {
"A": "active",
"M": "1st medio-passive",
"P": "2nd medio-passive",
}
MOODS = {
"I": "indicative",
"N": "infinitive",
"S": "subjunctive",
"P": "participle",
}
PERSONS = {
"1": "1st person",
"2": "2nd person",
"3": "3rd person",
}
def humanize_parse(pos, parse):
person, aspect_tense, voice, mood, case, number, gender, degree = parse
if pos in ["C-", "D-", "P-", "X-"]:
return ""
elif pos == "A-":
return " ".join([CASES[case], NUMBERS[number], GENDERS[gender], DEGREES[degree]])
elif pos in ["N-", "RA", "RD", "RI", "RP", "RR"]:
return " ".join([CASES[case], NUMBERS[number], GENDERS[gender]])
elif pos == "V-":
if aspect_tense not in "AF" and voice == "P":
voice = "M"
if mood in "DSO":
return " ".join([
ASPECTS[aspect_tense],
VOICES[voice],
MOODS[mood],
PERSONS[person],
NUMBERS[number]
])
if mood == "I":
return " ".join([
ASPECTS_TENSES[aspect_tense],
VOICES[voice],
MOODS[mood],
PERSONS[person],
NUMBERS[number]
])
elif mood == "P":
return " ".join([
ASPECTS[aspect_tense],
VOICES[voice],
MOODS[mood],
CASES[case],
NUMBERS[number],
GENDERS[gender]
])
elif mood == "N":
return " ".join([
ASPECTS[aspect_tense],
VOICES[voice],
MOODS[mood]
])
GLOSS_OVERRIDES = {
"Μωϋσῆς": "Moses",
}
def get_gloss(lemma):
if lemma in GLOSS_OVERRIDES:
return GLOSS_OVERRIDES[lemma]
if "gloss" not in lexical_entries[lemma]:
print("no gloss for {}".format(lemma))
quit()
return lexical_entries[lemma]["gloss"]
# assumes it's in a directory nextdoor
with open("../morphological-lexicon/lexemes.yaml") as f:
lexical_entries = yaml.load(f)
words = []
forms = {}
lexemes = {}
children = defaultdict(list)
with open("raw_data/john_3_a.txt") as f:
for line in f:
word_id, bcv, para_id, sent_id, pos, parse, crit, text, word, norm, lemma, rel, head = line.strip().split()
if (lemma, pos) not in lexemes:
lexeme_id = len(lexemes)
lexemes[(lemma, pos)] = lexeme_id
else:
lexeme_id = lexemes[(lemma, pos)]
if (norm, pos, parse, lexeme_id) not in forms.keys():
form_id = len(forms)
forms[(norm, pos, parse, lexeme_id)] = form_id
else:
form_id = forms[(norm, pos, parse, lexeme_id)]
if head == "None":
head = None
else:
children[head].append(word_id)
words.append({
"word_id": word_id,
"text": text,
"form_id": form_id,
"rel": rel,
"head": head,
})
for word in words:
word["children"] = children[word["word_id"]]
with open("base.json", "w") as f:
json.dump(words, f, indent=2, sort_keys=True, ensure_ascii=False)
ordered_forms = []
for t, form_id in sorted(forms.items(), key=lambda pair: pair[1]):
form, pos, parse, lexeme_id = t
ordered_forms.append({
"form": form,
"parse": humanize_parse(pos, parse),
"lexeme_id": lexeme_id,
})
with open("forms.json", "w") as f:
json.dump(ordered_forms, f, indent=2, sort_keys=True, ensure_ascii=False)
ordered_lexemes = []
for t, lexeme_id in sorted(lexemes.items(), key=lambda pair: pair[1]):
lemma, pos = t
ordered_lexemes.append({
"lemma": lemma,
"pos": humanize_pos(pos),
"gloss": get_gloss(lemma),
})
with open("lexemes.json", "w") as f:
json.dump(ordered_lexemes, f, indent=2, sort_keys=True, ensure_ascii=False)