-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcg_to_conllu.py
executable file
·310 lines (300 loc) · 10.2 KB
/
cg_to_conllu.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
#!/usr/bin/env python3
import sys
import unicodedata
import utils
utils.load_volume(sys.argv[1], globals())
POS_MAP = {
'art': 'DET',
'verb': 'VERB',
'subs': 'NOUN',
'nmpr': 'PROPN',
'advb': 'ADV',
'prep': 'ADP',
'prps': 'PRON',
'prde': 'PRON',
'prin': 'PRON',
'intj': 'INTJ',
'nega': 'ADV',
'inrg': 'PART', # TODO?
'adjv': 'ADJ',
'punct': 'PUNCT',
'prn': 'PRON',
'aux': 'AUX',
'SCONJ': 'SCONJ'
}
MORPH = {
'perf': ['VerbForm=Fin', 'Mood=Ind', 'Aspect=Perf'],
'impf': ['VerbForm=Fin', 'Mood=Ind', 'Aspect=Imp'],
'wayq': ['VerbForm=Fin', 'Mood=Ind', 'Tense=Past'],
'impv': ['VerbForm=Fin', 'Mood=Imp'],
'infa': ['VerbForm=Inf'], # absolute
'infc': ['VerbForm=Inf'], # construct
'ptca': ['VerbForm=Part'],
'ptcp': ['VerbForm=Part', 'Voice=Pass'],
'm': ['Gender=Masc'],
'f': ['Gender=Fem'],
'sg': ['Number=Sing'],
'du': ['Number=Dual'],
'pl': ['Number=Plur'],
'p1': ['Person=1'],
'p2': ['Person=2'],
'p3': ['Person=3'],
'hif': ['HebBinyan=HIFIL'],
'hit': ['HebBinyan=HITPAEL'],
# 'htpo': '???', # hitpo'el
# 'hof': '???', # hof'al
'nif': ['HebBinyan=NIFAL'],
'piel': ['HebBinyan=PIEL'],
# 'poal': '???' # po'al
# 'poel': '???' # po'el
'pual': ['HebBinyan=PUAL'],
'qal': ['HebBinyan=PAAL'],
'nega': ['Polarity=Neg'],
'prn': ['PronType=Prs'],
'prps': ['PronType=Prs'],
'prde': ['PronType=Dem'],
'prin': ['PronType=Int'],
'card': ['NumType=Card'],
'ordn': ['NumType=Ord'],
'art': ['PronType=Art'],
#'uvf:H': ['Case=Loc'], # TODO
}
BOOK_ABBR = {
'Genesis': 'GEN',
'Exodus': 'EX',
'Leviticus': 'LEV',
'Numbers': 'NUM',
'Deuteronomy': 'DEUT',
'Ruth': 'RUTH',
}
def norm(s):
return unicodedata.normalize('NFC', s)
class Word:
def __init__(self):
self.wid = 0
self.seg = 0
# conllu collumns
self.pos = ''
self.surf = ''
self.lemma = ''
self.upos = ''
self.xpos = ''
self.feats = []
self.head = ''
self.rel = ''
self.misc = []
# helper data, filled by self.get_info()
self.text = ''
self.tail = ''
self.is_end = True
def get_info(self):
last_wid = self.wid
if isinstance(self.wid, list):
last_wid = self.wid[-1]
if last_wid != 0:
self.text = norm(T.text(self.wid))
self.tail = norm(F.trailer_utf8.v(last_wid))
if last_wid == 15747 and T.bookName(last_wid) == 'Genesis':
self.tail = ' '
if self.tail:
self.text = self.text.rstrip(self.tail)
if (' ' in self.surf or '־' in self.surf) and self.seg != 0:
if self.seg == 1:
self.tail = ' ' if ' ' in self.text else '־'
self.wid = 0
self.text = self.text.replace('־', ' ').split()[self.seg-1]
self.surf = self.text
if (F.prs.v(last_wid) and F.prs.v(last_wid) not in ['absent', 'n/a']):
self.is_end = False
bk = BOOK_ABBR.get(T.bookName(last_wid))
ch = F.chapter.v(L.u(last_wid, otype='verse')[0])
vs = F.verse.v(L.u(last_wid, otype='verse')[0])
if bk:
self.misc.append(f'Ref={bk}_{ch}.{vs}')
lex = L.u(last_wid, otype='lex')
if lex:
gloss = F.gloss.v(lex[0])
if gloss:
self.misc.append(f'Gloss={gloss}')
if self.xpos not in ['punct', 'prn'] and not self.tail:
self.is_end = False
def from_cg(self, inp):
self.lemma = inp.split('"')[1]
parts = inp.split('"')[-1].split()
self.xpos = parts[0]
if self.xpos in POS_MAP:
self.upos = POS_MAP[self.xpos]
if self.upos == 'VERB' and self.lemma == 'היה':
self.upos = 'AUX'
elif self.lemma == 'עד' and self.xpos == 'conj':
self.upos = 'ADP'
for tg in parts:
if tg in MORPH:
self.feats += MORPH[tg]
elif tg.startswith('ExtPos='):
self.feats.append(tg)
elif tg.startswith('wp'):
self.seg = int(tg[2:])
elif tg[0] == 'w' and tg[-1] != 'p':
self.wid = int(tg[1:])
#self.misc.append('BHSA=' + str(self.wid))
if self.xpos == 'SCONJ':
self.wid = [self.wid-1, self.wid]
self.xpos = 'verb'
elif tg[0] == '#':
self.pos, self.head = tg[1:].split('->')
if self.head == self.pos:
self.head = ''
elif tg[0] == '@':
self.rel = tg[1:]
if self.rel == 'advmod' and self.upos == 'VERB':
self.upos = 'ADV'
elif self.rel == 'mark' and self.xpos == 'prep' and 'ExtPos=SCONJ' not in self.feats:
self.upos = 'SCONJ'
elif self.rel == 'fixed' and self.xpos == 'conj':
self.upos = 'SCONJ'
elif tg.startswith('retag:'):
if self.xpos == 'conj' and tg in ['retag:art', 'retag:subs']:
self.upos = 'SCONJ'
self.xpos = tg[6:]
if self.xpos == 'nmpr' and 'subs' in parts:
self.upos = 'NOUN'
elif tg == 'has_prn':
self.is_end = False
if tg in ['card', 'ordn']:
self.upos = 'NUM'
if self.xpos == 'conj':
if self.lemma == 'ו' or self.lemma == 'או':
self.upos = 'CCONJ'
elif self.rel == 'mark':
self.upos = 'SCONJ'
if self.upos in ['ADP', 'SCONJ']:
self.feats = [x for x in self.feats if 'ExtPos' in x]
elif self.upos == 'ADV':
self.feats = [x for x in self.feats if 'Polarity' in x]
elif self.upos == 'VERB' and self.lemma in ['ישׁ', 'אין']:
self.feats = ['VerbForm=Fin', 'Mood=Ind']
if self.rel == 'cop':
self.upos = 'AUX'
if self.lemma == 'הוה':
self.lemma = 'היה'
self.get_info()
def to_conllu(self):
ls = [
self.pos,
self.surf or '_',
self.lemma or '_',
self.upos or '_',
self.xpos or '_',
'|'.join(sorted(set(self.feats), key=lambda x: x.lower())) or '_',
self.head or '_',
self.rel or '_',
'_',
'|'.join(sorted(set(self.misc), key=lambda x: x.lower())) or '_'
]
return '\t'.join(ls)
class Sentence:
def __init__(self):
self.text = ''
self.sid = ''
self.real_words = []
self.all_words = []
def from_cg(self, stream):
surf = ''
for line in stream:
if not line.strip():
break
elif line[0] == '"':
surf = line.strip()[2:-2]
if surf == 'blah':
surf = '_'
elif line[0] == '\t':
if line[1] == '\t':
# no idea what happened here
# but see psalms 1896
continue
w = Word()
w.surf = surf
w.from_cg(line.strip())
self.real_words.append(w)
def get_ids(self):
ret = []
for w in self.real_words:
if isinstance(w.wid, list):
ret += w.wid
elif w.wid != 0:
ret.append(w.wid)
return sorted(set(ret))
def add_compounds(self):
gps = []
cur = []
for w in self.real_words:
cur.append(w)
if w.is_end:
gps.append(cur)
cur = []
for i, g in enumerate(gps):
if len(g) > 1:
surf = ''.join(w.text for w in g)
tail = ''.join(w.tail for w in g)
nw = Word()
nw.surf = surf
nw.pos = g[0].pos + '-' + g[-1].pos
if not tail or tail[0] != ' ':
nw.misc.append('SpaceAfter=No')
self.all_words.append(nw)
self.all_words += g
#for w in g:
# w.surf = '_'
else:
w = g[0]
w.surf = w.lemma if w.xpos == 'punct' else w.text
if w.xpos == 'punct' and w.lemma == '־':
w.misc.append('SpaceAfter=No')
elif w.tail and w.tail[0] != ' ':
w.misc.append('SpaceAfter=No')
self.all_words.append(w)
def process(self):
last_misc = ''
for w in self.real_words:
for m in w.misc:
if m.startswith('Ref='):
last_misc = m
break
else:
if last_misc:
w.misc.append(last_misc)
self.add_compounds()
ids = self.get_ids()
self.text = norm(T.text(ids).strip())
ws = min(ids)
we = max(ids)
book = T.bookName(ws)
chs = F.chapter.v(L.u(ws, otype="chapter")[0])
che = F.chapter.v(L.u(we, otype="chapter")[0])
vs = F.verse.v(L.u(ws, otype="verse")[0])
ve = F.verse.v(L.u(we, otype="verse")[0])
verse = ''
if chs != che:
verse = f'{chs}:{vs}-{che}:{ve}'
elif vs != ve:
verse = f'{chs}:{vs}-{ve}'
else:
verse = f'{chs}:{vs}'
self.sid = f'Masoretic-{book}-{verse}-hbo'
def to_conllu(self):
for w in self.all_words:
if w.xpos != 'punct':
tr = utils.transliterate_loc(w.surf, False)
w.misc.append('Translit=' + tr)
ret = f'# sent_id = {self.sid}\n# text = {self.text}\n'
ret += '# translit = ' + utils.transliterate_loc(self.text, False) + '\n'
return ret + '\n'.join(w.to_conllu() for w in self.all_words) + '\n'
if __name__ == '__main__':
while True:
s = Sentence()
s.from_cg(sys.stdin)
if not s.real_words:
break
s.process()
print(s.to_conllu())