-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_tf.py
269 lines (239 loc) · 10.1 KB
/
convert_to_tf.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
import re
from datetime import datetime
from tf.fabric import Fabric
import csv
import unicodedata
from betacode import decode
from greekbeta_to_unicode import gk_decode
from rules import rule_affected, solution_for_rule
#read in csv
sdbh_counter = 0
ketiv = 0
qere = 0
rafe = 0
line = ""
sdbh_contents = []
sdbh_books = [ "Gen", "Exod", "Lev", "Num", "Deut", "Josh", "Judg", "1Sam", "2Sam", "1Kgs", "2Kgs", "Isa", "Jer", "Ezek", "Hos", "Joel", "Amos", "Obad", "Jonah", "Mic", "Nah", "Hab", "Zeph", "Hag", "Zech", "Mal", "Ps", "Job", "Prov", "Ruth", "Song", "Eccl", "Lam", "Esth", "Dan", "Ezra", "Neh", "1Chr", "2Chr" ]
print("Reading csv ...")
with open('processed_tfdata.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if rule_affected(row["RecordId"]):
solution = solution_for_rule(row["RecordId"])
if solution:
sdbh_contents = sdbh_contents + solution
continue
sdbh_counter += 1
to_decode = re.sub(r'[\d\/]', '', row['HebrewText'])
to_decode = re.sub(r'~', ' ', to_decode)
if re.search(r'\*\*', to_decode):
qere+=1
continue
if re.search(r'\*', to_decode):
ketiv += 1
to_decode = re.sub(r'\*', '', to_decode)
if re.search(r',', to_decode):
to_decode = re.sub(r'\,', '', to_decode)
rafe += 1
if to_decode == "_":
continue
sdbh_contents.append({
"id": row["RecordId"],
"betacode": row["HebrewText"],
"hebrew": decode(to_decode),
"domain": row["LexDomain"],
"sdbh": row["SDBH"],
"glemma": row["GLemma"]
})
print("Completed csv prep")
glemma_missing_counter = 0
def get_glemma(obj):
if "glemma" in obj:
return obj["glemma"]
else:
global glemma_missing_counter
glemma_missing_counter += 1
return ""
# Get words from SDBH
def get_sdbh(book_abbreviation):
sdbh_words = []
sdbh_references = []
sdbh_ids = []
for i, match in enumerate(sdbh_contents):
if not match["id"].startswith(book_abbreviation):
continue
if match["hebrew"] == "ס" or match["hebrew"] == "ף" or match["hebrew"] == "ן":
continue
sdbh_words.append(match["hebrew"])
sdbh_references.append(match["id"])
sdbh_ids.append(i)
return sdbh_words, sdbh_references, sdbh_ids
TF = Fabric(locations='/home/jcuenod/Programming/text-fabric-data', modules='hebrew/etcbc4c')
api = TF.load('g_cons_utf8')
api.makeAvailableIn(globals())
def get_tf(book_abbreviation):
verse_node = T.nodeFromSection((book_abbreviation, 1, 1), lang='sblspaceless')
book_node = L.u(verse_node, otype='book')[0]
tf_words = []
tf_references = []
tf_ids = []
for w in L.d(book_node, otype='word'):
tf_words.append(F.g_cons_utf8.v(w))
tf_references.append(str(T.sectionFromNode(w)))
tf_ids.append(w)
return tf_words, tf_references, tf_ids
def replace_finals(word):
return word.replace("ך", "כ") \
.replace("ם", "מ") \
.replace("ן", "נ") \
.replace("ף", "פ") \
.replace("ץ", "צ")
def _norm(w):
return replace_finals(unicodedata.normalize("NFKD", w)).replace(" ", "")
def normalised_compare(w1, w2):
return _norm(w1) == _norm(w2)
node_data = []
domains_to_ignore = ["Negators", "Identifiers"]
do_print = True
# for book_abbreviation in ["Gen"]:
for book_abbreviation in sdbh_books:
tf_words, tf_references, tf_ids = get_tf(book_abbreviation)
sdbh_words, sdbh_references, sdbh_ids = get_sdbh(book_abbreviation)
offset_counter = 0
def increment_offset_counter(offset_counter):
offset_counter += 1
# if offset_counter % 2500 == 0:
# print("offsets: ", offset_counter)
return offset_counter
counter = 0
if do_print:
print(book_abbreviation, "SDBH units: ", len(sdbh_words), " TF units: ", len(tf_words))
offset_sdbh = 0
offset_tf = 0
i = -1
while i + offset_tf + 1 < len(tf_words) and i + offset_sdbh + 1 < len(sdbh_words):
i += 1
# for i in range(len(min(sdbh_words, tf_words))):
if normalised_compare(sdbh_words[i + offset_sdbh], tf_words[i + offset_tf]):
# if write_to_file:
# write to file
node_data.append((_norm(sdbh_words[i+offset_sdbh]), sdbh_contents[sdbh_ids[i+offset_sdbh]]["domain"], sdbh_references[i+offset_sdbh], get_glemma(sdbh_contents[sdbh_ids[i+offset_sdbh]])))
continue
elif sdbh_words[i + offset_sdbh] == "ישׂשכר" and tf_words[i + offset_tf] == "ישׂשׂכר":
# variant spelling of Issachar
node_data.append((_norm(sdbh_words[i+offset_sdbh]), sdbh_contents[sdbh_ids[i+offset_sdbh]]["domain"], sdbh_references[i+offset_sdbh], get_glemma(sdbh_contents[sdbh_ids[i+offset_sdbh]])))
continue
elif len(sdbh_words[i + offset_sdbh]) > len(tf_words[i + offset_tf]):
# data from sdbh for one word goes to two...
to_test = tf_words[i + offset_tf] + tf_words[i + offset_tf + 1]
temp_offset = offset_tf + 1
while len(_norm(to_test)) < len(_norm(sdbh_words[i + offset_sdbh])):
temp_offset += 1
to_test += tf_words[i + temp_offset]
if normalised_compare(sdbh_words[i + offset_sdbh], to_test):
for j in range(temp_offset - offset_tf + 1):
if tf_words[i + offset_tf + j] == "":
node_data.append(("","","",""))
else:
node_data.append((_norm(sdbh_words[i+offset_sdbh]), sdbh_contents[sdbh_ids[i+offset_sdbh]]["domain"], sdbh_references[i+offset_sdbh], get_glemma(sdbh_contents[sdbh_ids[i+offset_sdbh]])))
offset_tf = temp_offset
# if do_print:
# print(" tf fix:", sdbh_references[i + offset_sdbh], ":SD: ", sdbh_words[i + offset_sdbh], "==", to_test, " :TF:", tf_references[i + offset_tf], "OFFSETS (tf, sdbh):", offset_tf, offset_sdbh)
continue
elif len(tf_words[i + offset_tf]) > len(sdbh_words[i + offset_sdbh]):
# data from multiple sdbh goes to one tf word...
to_test = sdbh_words[i + offset_sdbh] + sdbh_words[i + offset_sdbh + 1]
temp_offset = offset_sdbh + 1
while len(_norm(to_test)) < len(_norm(tf_words[i + offset_tf])):
temp_offset += 1
to_test += sdbh_words[i + temp_offset]
if normalised_compare(tf_words[i + offset_tf], to_test):
possibles = []
last_resort = []
prefer_me = []
for j in range(temp_offset - offset_sdbh + 1):
if sdbh_contents[sdbh_ids[i+offset_sdbh+j]]["domain"] == "":
# ignore empty lexical domains
continue
if sdbh_contents[sdbh_ids[i+offset_sdbh+j]]["domain"] in domains_to_ignore:
# ignore certain domains
last_resort.append((_norm(sdbh_words[i+offset_sdbh+j]), sdbh_contents[sdbh_ids[i+offset_sdbh+j]]["domain"], sdbh_references[i+offset_sdbh+j], get_glemma(sdbh_contents[sdbh_ids[i+offset_sdbh+j]])))
continue
if len(_norm(sdbh_words[i+offset_sdbh+j])) == 1:
# ignore certain domains
last_resort.append((_norm(sdbh_words[i+offset_sdbh+j]), sdbh_contents[sdbh_ids[i+offset_sdbh+j]]["domain"], sdbh_references[i+offset_sdbh+j], get_glemma(sdbh_contents[sdbh_ids[i+offset_sdbh+j]])))
continue
if sdbh_contents[sdbh_ids[i+offset_sdbh+j]]["domain"] in map(lambda x: x[1], possibles):
# if we already have this category, ignore it a second time
#although maybe we should use the second one instead?
continue
if sdbh_contents[sdbh_ids[i+offset_sdbh+j]]["domain"] == "Names of Locations":
# ignore names of locations -- maybe we will just end up using this value
prefer_me.append((_norm(sdbh_words[i+offset_sdbh+j]), sdbh_contents[sdbh_ids[i+offset_sdbh+j]]["domain"], sdbh_references[i+offset_sdbh+j], get_glemma(sdbh_contents[sdbh_ids[i+offset_sdbh+j]])))
continue
possibles.append((_norm(sdbh_words[i+offset_sdbh+j]), sdbh_contents[sdbh_ids[i+offset_sdbh+j]]["domain"], sdbh_references[i+offset_sdbh+j], get_glemma(sdbh_contents[sdbh_ids[i+offset_sdbh+j]])))
if len(possibles) > 1:
print("POSSIBLES:", possibles)
elif len(possibles) == 0:
if len(prefer_me) > 0:
if len(prefer_me) == 1:
node_data.append(prefer_me[0])
else:
print("PREFER: ", prefer_me)
elif len(last_resort) > 0:
if len(last_resort) == 1:
node_data.append(last_resort[0])
else:
print("LAST RESORT:", last_resort)
else:
# print("LAST RESORT/PREFER: not enough options...")
node_data.append(("","","",""))
else:
if len(prefer_me) > 1:
if len(prefer_me) == 1:
node_data.append(prefer_me[0])
else:
print("PREFER: ", prefer_me)
else:
node_data.append(possibles[0])
# if do_print:
# print("sdbh fix:", sdbh_references[i + offset_sdbh], ":SD: ", to_test, "=", tf_words[i + offset_tf], " :TF:", tf_references[i + offset_tf], "OFFSETS (tf, sdbh):", offset_tf, offset_sdbh)
offset_sdbh = temp_offset
continue
if do_print:
print(sdbh_references[i + offset_sdbh], ":SD: ", sdbh_words[i + offset_sdbh], "=!=", tf_words[i + offset_tf], " :TF:", tf_references[i + offset_tf], "OFFSETS (tf, sdbh):", offset_tf, offset_sdbh)
# Having tested, I'm fairly confident that there aren't any of these that mess up alignment:
print("Guess I'll just append whatever...")
node_data.append((_norm(sdbh_words[i+offset_sdbh]), sdbh_contents[sdbh_ids[i+offset_sdbh]]["domain"], sdbh_references[i+offset_sdbh], get_glemma(sdbh_contents[sdbh_ids[i+offset_sdbh]])))
counter += 1
if counter > 10:
print("TOO MANY ISSUES - EXITING")
exit()
if do_print:
if len(sdbh_words) - offset_sdbh != len(tf_words) - offset_tf:
print("\nlast 5 words:")
print("sdbh_words: ", " ".join(sdbh_words[len(sdbh_words) - 5:]), sdbh_references[-1])
print(" tf_words: ", " ".join(tf_words[len(tf_words) - 5:]), tf_references[-1])
print("WARNING: lists of unequal length, not all data has been compared")
else:
print("success ({0} nodes)".format(str(len(node_data))))
sdbh_filename = "sdbh.tf"
sdbh_fileheader = '''@node
@valueType=str
@writtenBy=James Cuénod & SDBH
@dateWritten={0}
'''.format(datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"))
print("writing file:", sdbh_filename)
with open(sdbh_filename, mode='wt', encoding='utf-8') as out:
out.write(sdbh_fileheader)
out.write('\n'.join(map(lambda x: x[1], node_data)))
lxx_filename = "lxxlexeme.tf"
lxx_fileheader = '''@node
@valueType=str
@writtenBy=James Cuénod & CCAT
@dateWritten={0}
'''.format(datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"))
print("writing file:", lxx_filename)
with open(lxx_filename, mode='wt', encoding='utf-8') as out:
out.write(lxx_fileheader)
out.write('\n'.join(map(lambda x: gk_decode(x[3]), node_data)))