-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadLiftTags.py
371 lines (332 loc) · 10.8 KB
/
ReadLiftTags.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 5 21:10:45 2019
@author: Mark
"""
import re
re_span = re.compile(r"</?span( lang='(en|pt|mbj)')?>")
re_form = re.compile(r"</?form( lang='(en|pt|mbj)')?>")
re_text = re.compile(r'</?text[^>]*?>')
re_gloss = re.compile(r"</?gloss( lang='(en|pt|mbj)')?>")
re_rev = re.compile(r"</?reversal type='(en|pt|mbj)'>")
def tag_globals():
global entry_tags, entry_keys, sense_tags, sense_keys
entry_tags = {'<pronunciation':read_pronunciation,
'<note':read_note,
'<relation type':read_variant,
'<lexical-unit>':read_lu,
'<sense':read_sense_wrapper,
"<trait name='morph-type'":read_morph_type,
'<field type="':read_custom_field
}
entry_keys = {'dateModified':'date',
'<pronunciation':'pronunciation',
'<note':'note',
'<relation type':'variant_of',
'<lexical-unit>':'headword',
'<sense':'sense',
"<trait name='morph-type'":'morph_type',
'<field type="':'other_forms'
}
sense_tags = {'<definition>':read_definition,
'<grammatical-info':read_pos,
'<note':read_note,
'<gloss':read_gloss,
'<reversal type':read_reversal
}
sense_keys = {'<definition>':'def',
'<grammatical-info':'pos',
'<note':'note',
'<gloss':'gloss',
'<reversal type':'reverse'
}
def skip_head(r):
line, line_bytes = r_d_bytes(r)
if line.startswith('<?xml'):
while line != '</header>':
line = read_decode(r)
else:
assert line.startswith('<entry')
step_back(r, line_bytes)
def read_entry(r, id_only=False):
open_tag = read_decode(r)
assert open_tag.startswith('<entry')
entry_id = get_xml_attr(open_tag, 'id')
if entry_id.startswith('='):
entry_id = ' ' + entry_id
if not id_only:
entry_data = {k:None for k in entry_keys.values()}
entry_data['note'] = {}
entry_data['sense'] = []
entry_data['variant_of'] = {}
entry_data['other_forms'] = {}
entry_data['entry_id'] = entry_id
entry_data['date'] = get_xml_attr(open_tag, 'dateCreated')
entry_data['date_modified'] = get_xml_attr(open_tag, 'dateModified')
line, line_bytes = r_d_bytes(r)
while True:
for tag, funct in entry_tags.items():
if line.startswith(tag):
step_back(r, line_bytes)
key = entry_keys[tag]
data = funct(r)
if not id_only:
if key == 'sense':
entry_data[key].append(data)
elif not data:
pass
else:
assert not entry_data[key]
entry_data[key] = data
break
else:
assert line == '</entry>', line + ' ' + str(entry_data)
break
line, line_bytes = r_d_bytes(r)
if id_only:
return entry_id
entry_data = {k:(v if v else None) for k, v in entry_data.items()}
headword = entry_data['headword']
headword = ' '+headword if (type(headword) is str and headword.startswith('=')) else headword
entry_data['headword'] = headword
return entry_data
def read_sense(r, id_only=False):
open_tag = read_decode(r)
assert open_tag.startswith('<sense')
sense_id = get_xml_attr(open_tag, 'id')
if not id_only:
sense_data = {k:None for k in sense_keys.values()}
sense_data['sense_id'] = sense_id
line, line_bytes = r_d_bytes(r)
while True:
for tag, funct in sense_tags.items():
if line.startswith(tag):
step_back(r, line_bytes)
key = sense_keys[tag]
data = funct(r)
if not id_only:
assert not sense_data[key]
sense_data[key] = data
break
else:
# if current line did not match any tags
# end of sense
assert line == '</sense>', line
break
line, line_bytes = r_d_bytes(r)
if id_only:
return sense_id
sense_data = {k:(v if v else None) for k, v in sense_data.items()}
return sense_data
def read_sense_wrapper(r):
return read_sense(r, id_only=True)
def read_pronunciation(r):
open_tag = read_decode(r)
assert open_tag == '<pronunciation>'
s = read_decode(r)
s = read_form(s)
end_tag = read_decode(r)
assert end_tag == '</pronunciation>', end_tag
return s
def read_definition(r):
open_tag = read_decode(r)
assert open_tag == '<definition>'
# definition can frame multiple arguments
# for multiple languages
# save all to dict
this_def = {}
s = read_decode(r)
while s != '</definition>':
lang = get_xml_attr(s, 'lang')
assert lang not in this_def.keys(), lang
def_str = read_form(s)
this_def[lang] = def_str
s = read_decode(r)
return this_def
def read_variant(r):
variants = {}
line, line_bytes = r_d_bytes(r)
while line.startswith('<relation'):
ref, data = get_variant(line, r)
i=1
while ref=='null' and ref in variants:
tmp = ref + '_' + str(i)
if tmp not in variants:
ref = tmp
break
i+=1
assert ref not in variants
variants[ref] = data
line, line_bytes = r_d_bytes(r)
step_back(r, line_bytes)
return variants
def get_variant(s, r):
ref = get_xml_attr(s, 'ref')
ref = ref if ref else 'null'
var_type = get_xml_attr(s, 'type')
data = {'type': var_type}
s = read_decode(r)
while s.startswith('<trait '):
name = get_xml_attr(s, 'name')
value = get_xml_attr(s, 'value')
assert name in ('variant-type', 'complex-form-type', 'is-primary', 'hide-minor-entry'), name
if name in data and type(data[name]) is tuple:
data[name] = (*data[name], value)
elif name in data:
data[name] = (data[name], value)
else:
data[name] = value
s = read_decode(r)
summ = None
if s == "<field type='summary'>":
summ = read_decode(r)
summ = read_form(summ)
data['summary'] = summ
end_tag = read_decode(r)
assert end_tag == '</field>'
s = read_decode(r)
assert s == '</relation>', s + ' ' + str(data)
return ref, data
def read_note(r):
notes = {}
line, line_bytes = r_d_bytes(r)
while line.startswith('<note'):
note_type, this_note = get_note(line, r)
i=1
while note_type in notes:
tmp = note_type + '_' + str(i)
if tmp not in notes:
note_type = tmp
break
i+=1
notes[note_type] = this_note
line, line_bytes = r_d_bytes(r)
step_back(r, line_bytes)
return notes
def get_note(s, r):
if s == '<note>':
note_type = 'Note'
else:
note_type = get_xml_attr(s, 'type')
s = read_decode(r)
notes = []
while s.startswith('<form '):
notes.append(read_form(s))
s = read_decode(r)
end_tag = s
assert end_tag == '</note>', end_tag
notes = ', '.join(notes)
return note_type, notes
def read_span(s):
s = re_span.sub("", s)
assert 'span>' not in s, s
return s
def read_form(s):
s = read_span(s)
s = re_form.sub("", s)
s = read_text(s)
assert 'form>' not in s, s
return s.strip()
def read_gloss(r):
glosses = {}
line, line_bytes = r_d_bytes(r)
while line.startswith('<gloss'):
this_gloss, lang = get_gloss(line)
assert lang not in glosses
glosses[lang] = this_gloss
line, line_bytes = r_d_bytes(r)
step_back(r, line_bytes)
return glosses
def get_gloss(s):
lang = get_xml_attr(s, 'lang')
assert lang in ('pt', 'en'), lang
s = read_span(s)
s = re_gloss.sub("", s)
s = read_text(s)
assert 'gloss>' not in s
return (s.strip(), lang)
def read_custom_field(r):
data = {}
line, line_bytes = r_d_bytes(r)
while line.startswith('<field'):
datum_type = get_xml_attr(line, 'type')
line = read_decode(r)
datum = read_form(line)
assert datum_type not in data
data[datum_type] = datum
line = read_decode(r)
assert line.startswith('</field>')
line, line_bytes = r_d_bytes(r)
step_back(r, line_bytes)
return data
def read_reversal(r):
revs = {}
line, line_bytes = r_d_bytes(r)
while line.startswith('<reversal'):
this_rev, lang = get_reversal(line, r)
assert lang not in revs
revs[lang] = this_rev
line, line_bytes = r_d_bytes(r)
step_back(r, line_bytes)
return revs
def get_reversal(s, r):
lang = get_xml_attr(s, 'lang')
assert lang in ('pt', 'en'), lang
s = read_span(s)
s = re_rev.sub("", s)
s = read_form(s)
assert '<reversal' not in s
end_tag = read_decode(r)
assert end_tag == '</reversal>'
return (s.strip(), lang)
def read_text(s):
s = re_text.sub("", s)
assert 'text>' not in s
return s.strip()
def read_lu(r):
open_tag = read_decode(r)
assert open_tag == '<lexical-unit>', open_tag
s = read_decode(r)
lang = get_xml_attr(s, 'lang')
assert lang == 'mbj', lang
s = read_form(s)
end_tag = read_decode(r)
assert end_tag == '</lexical-unit>', end_tag
return s.strip()
def read_morph_type(r):
s = read_decode(r)
assert get_xml_attr(s, 'name') == 'morph-type'
return get_xml_attr(s, 'value')
def read_pos(r):
s = read_decode(r)
end_tag = read_decode(r)
assert end_tag == '</grammatical-info>', end_tag
return get_xml_attr(s, 'value')
def read_decode(r):
line = r.readline()
line = line.decode('utf8')
line = line.replace(',',';')
line = line.replace('"', "'")
line = line.strip()
return line
def r_d_bytes(r):
these_bytes = r.readline()
line = these_bytes.decode('utf8')
line = line.replace(',',';')
line = line.replace('"', "'")
line = line.strip()
return line, these_bytes
def step_back(r, line_bytes):
offset = len(line_bytes) * -1
r.seek(offset, 1)
def get_xml_attr(s, label):
split = s.split(sep="'")
kwarg_found = False
for chunk in split:
if kwarg_found:
return chunk
elif chunk.endswith(label+'='):
kwarg_found = True
else:
raise ValueError(f'No kwarg matching label {label} in string {s}.')
tag_globals()