tei_writer.py
8.78 KB
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
# -*- coding: utf-8 -*-
import os, sys
import codecs
from xml_utils import xml_escape
import settings
SEGMENTATION = u"ann_segmentation.xml"
MORPHOSYNTAX = u"ann_morphosyntax.xml"
class LogicError(Exception):
pass
def write_header(f):
f.write('''<?xml version="1.0" encoding="UTF-8"?>
<teiCorpus xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:nkjp="http://www.nkjp.pl/ns/1.0" xmlns="http://www.tei-c.org/ns/1.0">
<xi:include href="KORBA_header.xml"/>
<TEI>
<xi:include href="header.xml"/>
<text>
<body>
''')
def write_footer(f):
f.write(''' </body>
</text>
</TEI>
</teiCorpus>
''')
def start_tag(f, tg, xmlid, corresp, indent=u""):
line = indent+u" <{} xml:id=\"{}\"".format(tg, xmlid)
if corresp is not None:
line += u" corresp=\"{}\"".format(corresp)
line += u">\n"
f.write(line)
def end_tag(f, tg, indent=u""):
f.write(indent+u" </{}>\n".format(tg))
def incscntr(seg, cntrptr, origchoice = False):
if isinstance(seg, dict):
cntrptr[0] += 1
elif isinstance(seg, list):
if origchoice:
if len(seg) != 2:
print(repr(seg))
raise LogicError("niepoprawny choice")
for i, c in enumerate(seg):
for s in c:
incscntr(s, cntrptr, False)
else:
for c in seg:
for s in c:
incscntr(s, cntrptr, False)
else:
raise LogicError("niespodziewany smiec zamiast segmentu "+repr(seg))
def write_seg(fs, fm, seg, cntrptr, pcnt, xmlid, typ, oxmlid = None, ind = u""):
indent = ind+u" "
if isinstance(seg, dict):
seg["segid"] = "segm_{}.{}-seg".format(pcnt, cntrptr[0])
seg["morphid"] = "morph_{}.{}-seg".format(pcnt, cntrptr[0])
# zapis segmentu w segmentacji
if xmlid is not None:
lines = indent+u"<seg xml:id=\"{}\" corresp=\"{}#string-range({},{},{})\"".format(seg["segid"], source_filename, xmlid, seg["source_offset"], len(seg["source_orth"]))
else:
lines = indent+u"<seg xml:id=\"{}\"".format(seg["segid"])
if typ:
lines += u" type=\"{}\"".format(typ)
if "nps" in seg:
lines += u" nkjp:nps=\"true\""
lines += ">\n"
lines += indent+u" <w>{}</w>\n".format(xml_escape(seg["source_orth"]))
lines += indent+u"</seg>\n"
fs.write(lines)
# zapis segmentu w morfoskladni
lines = indent+u"<seg xml:id=\"{}\" corresp=\"{}#{}\">\n".format(seg["morphid"], SEGMENTATION, seg["segid"])
lines += indent+u" <fs type=\"morph\">\n"
lines += indent+u" <f name=\"orth\">\n"
lines += indent+u" <string>{}</string>\n".format(xml_escape(seg["orth"]))
lines += indent+u" </f>\n"
lines += indent+u" <f name=\"translit\">\n"
lines += indent+u" <string>{}</string>\n".format(xml_escape(seg["source_orth"]))
lines += indent+u" </f>\n"
lines += indent+u" <f name=\"interps\">\n"
bctgmsd = {}
for i in seg["interps"]:
k = (i["base"], i["ctag"])
if k not in bctgmsd:
bctgmsd[k] = []
bctgmsd[k].append(i["msd"])
lid = 1
msdid = 1
for k, v in bctgmsd.items():
lines += indent+u" <fs type=\"lex\" xml:id=\"morph_{}.{}.{}-lex\">\n".format(pcnt, cntrptr[0], lid)
lines += indent+u" <f name=\"base\">\n"
lines += indent+u" <string>{}</string>\n".format(xml_escape(k[0]))
lines += indent+u" </f>\n"
lines += indent+u" <f name=\"ctag\">\n"
lines += indent+u" <symbol value=\"{}\"/>\n".format(k[1])
lines += indent+u" </f>\n"
lines += indent+u" <f name=\"msd\">\n"
if len(v) > 1:
lines += indent+u" <vAlt>\n"
for m in v:
lines += indent+u" <symbol value=\"{}\" xml:id=\"morph_{}.{}.{}.{}-msd\"/>\n".format(m, pcnt, cntrptr[0], lid, msdid)
msdid += 1
lines += indent+u" </vAlt>\n"
else:
lines += indent+u" <symbol value=\"{}\" xml:id=\"morph_{}.{}.{}.{}-msd\"/>\n".format(v[0], pcnt, cntrptr[0], lid, msdid)
msdid += 1
lines += indent+u" </f>\n"
lines += indent+u" </fs>\n"
lid += 1
msdid = 1
lines += indent+u" </f>\n"
lines += indent+u" </fs>\n"
lines += indent+u"</seg>\n"
fm.write(lines)
cntrptr[0] += 1
elif isinstance(seg, list):
fs.write(indent+u"<choice>\n")
if oxmlid:
if len(seg) != 2:
print(repr(seg))
raise LogicError("niepoprawny choice")
xids = [xmlid, oxmlid]
for i, c in enumerate(seg):
xind = ind+u" "
if len(c) > 1:
fs.write(indent+u" <nkjp:paren>\n")
xind += u" "
for s in c:
write_seg(fs, fm, s, cntrptr, pcnt, xids[i], typ, None, xind)
if len(c) > 1:
fs.write(indent+u" </nkjp:paren>\n")
else:
for c in seg:
xind = ind+u" "
if len(c) > 1:
fs.write(indent+u" <nkjp:paren>\n")
xind += u" "
for s in c:
write_seg(fs, fm, s, cntrptr, pcnt, xmlid, typ, None, xind)
if len(c) > 1:
fs.write(indent+u" </nkjp:paren>\n")
fs.write(indent+u"</choice>\n")
else:
raise LogicError("niespodziewany smiec zamiast segmentu "+repr(seg))
def write_special_seg(f, frag):
# zapis segmentu specjalnego tylko w segmentacji
lines = u" <seg xml:id=\"{}\" type=\"{}\"/>\n".format(frag["segid"], frag["type"])
f.write(lines)
def write_files(path, parasents, source_fn):
global source_filename
source_filename = source_fn
#segmentation_path = os.path.join(unicode(path, "utf-8"), SEGMENTATION)
#morphosyntax_path = os.path.join(unicode(path, "utf-8"), MORPHOSYNTAX)
segmentation_path = os.path.join(unicode(path, "utf-8"), settings.SUBFOL, SEGMENTATION)
morphosyntax_path = os.path.join(unicode(path, "utf-8"), settings.SUBFOL, MORPHOSYNTAX)
segm = codecs.open(segmentation_path, mode='w', encoding='utf-8')
morpho = codecs.open(morphosyntax_path, mode='w', encoding='utf-8')
write_header(segm)
write_header(morpho)
for pci, p in enumerate(parasents):
segcntr = [1]
pcnt = pci + 1
start_tag(segm, "p", "segm_{}-p".format(pcnt), None)
start_tag(morpho, "p", "morph_{}-p".format(pcnt), SEGMENTATION+"#segm_{}-p".format(pcnt))
for s in p:
scnt = [segcntr[0]-1]
# oblicz scnt tak, jak to jest w NKJP (czyli idiotycznie - nr ostatniego segmentu)
for frag in s:
if "orig_segs" in frag:
if "segs" in frag:
incscntr([frag["segs"], frag["orig_segs"]], scnt, True)
else:
for seg in frag["orig_segs"]:
incscntr(seg, scnt)
elif "segs" in frag:
for seg in frag["segs"]:
incscntr(seg, scnt)
else:
scnt[0] += 1
scnt = scnt[0]
start_tag(segm, "s", "segm_{}.{}-s".format(pcnt, scnt), None, u" ")
start_tag(morpho, "s", "morph_{}.{}-s".format(pcnt, scnt), SEGMENTATION+"#segm_{}.{}-s".format(pcnt, scnt), u" ")
for frag in s:
if "orig_segs" in frag:
if "segs" in frag:
write_seg(segm, morpho, [frag["segs"], frag["orig_segs"]], segcntr, pcnt, frag["id"], frag["type"] if "type" in frag else None, frag["orig_id"])
else:
for seg in frag["orig_segs"]:
write_seg(segm, morpho, seg, segcntr, pcnt, frag["orig_id"], frag["type"] if "type" in frag else None)
elif "segs" in frag:
for seg in frag["segs"]:
write_seg(segm, morpho, seg, segcntr, pcnt, frag["id"], frag["type"] if "type" in frag else None)
else:
frag["segid"] = "segm_{}.{}-seg".format(pcnt, segcntr[0])
segcntr[0] += 1
write_special_seg(segm, frag)
end_tag(segm, "s", u" ")
end_tag(morpho, "s", u" ")
end_tag(segm, "p")
end_tag(morpho, "p")
write_footer(segm)
write_footer(morpho)
segm.close()
morpho.close()