-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_germanic_thesaurus.py
195 lines (164 loc) · 6.87 KB
/
parse_germanic_thesaurus.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
# Parse the Germanic Thesaurus into a machine-readable JSON format
import csv
import json
"""
array around entry
english_to_germanic[lemma][pos] = [{
"alternatives": {
DEFINITION1: [ALTERNATIVE1, ALTERNATIVE2],
},
"germanic_like_alternatives": germanic_like,
"details": {
"notes": [NOTES],
"examples": [
{
"example": EXAMPLE_GX,
"related_lemmas": RELATED_LEMMAS_OPTIONAL,
}
],
"germanic_like_examples": [],
"links": [LINKS_LX],
"related_lemmas": [],
}
}]
"""
def process_pos(pos: str) -> str:
pos = pos.strip()
if pos == "n": return "Noun"
pos = pos.replace("adj", "Adjective")
pos = pos.replace("adv", "Adverb")
pos = pos.replace("conj", "Conjunction")
pos = pos.replace("interj", "Interjection")
pos = pos.replace("phr", "Phrase")
pos = pos.replace("pref", "Prefix")
pos = pos.replace("prep phr", "Prepositional Phrase")
pos = pos.replace("prep", "Preposition")
pos = pos.replace("suff", "Suffix")
pos = pos.replace("vb", "Verb")
return pos
def process_germanic(g: str) -> str:
germanic = g.replace(" [D", "<br/><br/>[D")
germanic = germanic.replace("[D", "[Definition ")
germanic = germanic.replace("|D", "|Definition ")
return germanic
def process_details(d: str) -> str:
details = d.replace("[N", "\n[Note ")
details = details.replace("|N", "|Note ")
details = details.replace("[G", "\n[Example ")
details = details.replace("[O", "\n[Example ") # Mistake
details = details.replace("|G", "|Example ")
details = details.replace("[F", "\n[Germanic-like example ")
details = details.replace("|F", "|Germanic-like example ")
details = details.replace("[L", "\n[Link ")
details = details.replace("|L", "|Link ")
details = details.replace("[S", "\n[Related lemmas")
details = details.replace("|S", "|Related lemmas")
details = details.replace("[D", "\n[Definition ")
details = details.replace("|D", "|Definition ")
details = details.strip()
details = details.replace("\n", "<br/>")
return details
english_to_germanic = {}
germanic_thesaurus = {}
with open("in/germanic_thesaurus_2.csv", "r") as f:
germanic_thesaurus = list(csv.reader(f))
with open("in/germanic_thesaurus_2.csv", "r") as f:
csv_reader = csv.reader(f, delimiter=",")
line_count = 0
for row in csv_reader:
if line_count == 0 or line_count == 1:
line_count += 1
continue
rank = row[0]
lemmas = row[1]
pos = process_pos(row[2])
germanic = row[3]
germanic = process_germanic(germanic)
germanic_like = row[4]
details = row[5]
details = process_details(details)
# Add O(NUMBER)
lemmas = lemmas.split(",")
lemmas = [l.strip() for l in lemmas]
lemmas = [l.removeprefix("`") for l in lemmas]
# Lemmas: ['time period']
# Lemmas: ['title']
# Lemmas: ['to all appearances', 'by all appearances', 'from all appearances']
# print(f"Lemmas: {lemmas}")
# if "." in rank:
# in_sub_lemma = True
# sub_lemmas_since_root = int(rank.split(".")[1])
# # sub_lemma_root = ['606', '`term', 'n', '', '', '[S] come to terms with, in terms of', '']
# sub_lemma_root = germanic_thesaurus[line_count - sub_lemmas_since_root]
# print(f"sub_lemma_root = {sub_lemma_root}")
# input("Continue?\n")
# elif "." not in rank:
# in_sub_lemma = False
# sub_lemma_root = None
sub_lemmas = []
# print(f"germanic_thesaurus[line_count + 1] = {germanic_thesaurus[line_count + 1]}")
# germanic_thesaurus[line_count + 1] = ['51.1', '`just as', 'phr', '',
# '', "[G1] He left just as I was coming in → He left right as/when I
# was coming in → He left at the same time as I was coming in\n[G2]
# Just as you'd ... → (Much) In (much) the same way you'd ... → As you
# would ...\n[S] equally", '']
# If you are on the root lemma
if not line_count + 1 == len(germanic_thesaurus):
if ".1" in germanic_thesaurus[line_count + 1][0]:
# print(f"find: .1 in {germanic_thesaurus[line_count + 1]}")
sl_count = 0
while True:
if "." in germanic_thesaurus[line_count + 1 + sl_count][0]:
# print(f"find: . in {germanic_thesaurus[line_count + 1 + sl_count]}")
sl = germanic_thesaurus[line_count + 1 + sl_count]
sl[1] = sl[1].strip()
sl[1] = sl[1].removeprefix("`")
sl[1] = sl[1].replace("`", "")
sl[2] = process_pos(sl[2])
sl[3] = process_germanic(sl[3])
sl[5] = process_details(sl[5])
sub_lemmas.append({
"lemma": sl[1],
"pos": sl[2],
"alternatives": sl[3],
"germanic_like_alternatives": sl[4],
"details": sl[5],
})
else:
break
sl_count += 1
# if len(sub_lemmas) > 1:
# print(f"\n\n=====\nSub lemmas: {sub_lemmas}")
# input("Continue?")
for i, lemma in enumerate(lemmas):
# If the word already exists
if lemma in english_to_germanic:
# If the parts of speech exists
if pos in english_to_germanic[lemma]:
# print(f"Something has gone wrong, POS already at {lemma}")
english_to_germanic[lemma][pos].append({
"alternatives": germanic,
"germanic_like_alternatives": germanic_like,
"details": details,
"sub_lemmas": sub_lemmas,
})
else:
english_to_germanic[lemma][pos] = [{
"alternatives": germanic,
"germanic_like_alternatives": germanic_like,
"details": details,
"sub_lemmas": sub_lemmas,
}]
# If the word doesn't exist
else:
english_to_germanic[lemma] = {
pos: [{
"alternatives": germanic,
"germanic_like_alternatives": germanic_like,
"details": details,
"sub_lemmas": sub_lemmas,
}]
}
line_count += 1
with open("out/english_to_germanic.json", "w") as f:
json.dump(english_to_germanic, f)