-
Notifications
You must be signed in to change notification settings - Fork 5
/
matxin-to-conllu.py
170 lines (151 loc) · 3.94 KB
/
matxin-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
#!/usr/bin/env python3
import sys;
import xml.etree.ElementTree as ET
typ = "ud";
if len(sys.argv) > 1: #{
typ = "gt"
#}
buf = ''
for line in sys.stdin.readlines(): #{
buf = buf + line;
#}
root = ET.fromstring(buf);
class Globals: #{
NODES = {};
#}
def proc_node(depth, node, lastnode): #{
depth = depth + 1;
# print(Globals.NODES, file=sys.stderr)
# curnode = str(int(node.attrib['ord']) + 1);
if 'ord' not in node.attrib: #{
print(Globals.NODES, file=sys.stderr);
print('ERROR: lastnode=', lastnode, file=sys.stderr);
#}
curnode = int(node.attrib['ord']) + 1;
# print('! %d || cur: %s || last: %s' % (depth, curnode, lastnode), node.tag, file=sys.stderr);
if node.tag == 'NODE': #{
form = '-';
lem = '-';
mi = '-';
si = '-';
ord = '-';
if 'form' in node.attrib: #{
form = node.attrib['form'];
#}
if 'ord' in node.attrib: #{
ord = int(node.attrib['ord']);
#}
if 'lem' in node.attrib: #{
lem = node.attrib['lem'];
#}
if 'mi' in node.attrib: #{
mi = node.attrib['mi'];
#}
if 'si' in node.attrib: #{
si = node.attrib['si'];
#}
pos = mi;
if '|' in mi: #{
pos = mi.split('|')[0]
#}
# 1 2 3 4 5 6 7 8 9 10 # 1 2 3 4 5 6 7 8
Globals.NODES[ord] = (ord, form, lem, '_',pos, '_',lastnode, si, '_', mi);
#}
nodes = node.findall('NODE');
for d in nodes: #{
proc_node(depth, d, ord);
#}
#}
def proc_tekst(blokk): #{
o = '' ;
first = True;
for tok in blokk.split(' '): #{
if tok in [',', '.', ':', ';', '!', '?', '...'] or first: #{
o = o + tok;
else: #{
o = o + ' ' + tok;
#}
first = False;
#}
# Hacks
o = o.replace('( ', '(');
o = o.replace(' )', ')');
o = o.replace('« ', '«');
o = o.replace(' »', '»');
o = o.replace('“ ', '“');
o = o.replace(' ”', '”');
o = o.replace(' - ', '-');
o = o.replace('!-', '! - ');
o = o.replace(',-', ', - ');
o = o.replace(':-', ': - ');
o = o.replace('?-', '? - ');
# " Осылай тұр! " дегендей екі иығынан басып қалды, бес-алты адымдай жерге барып тұра қалды.
new_o = '';
qc = 0;
lastc = '';
for c in o: #{
if c == '"': #{
qc = qc + 1;
#}
if c == ' ' and qc % 2 == 1 and lastc == '"': #{
continue;
#}
new_o += c;
lastc = c;
#}
o = new_o;
o = o.replace(' " ', '" ');
o = o.replace(' ",', '",');
return o ;
#}
insent = 0;
validsent = 0;
for sent in root.findall(".//SENTENCE"): #{
insent = insent + 1;
heads = sent.findall("./NODE");
if len(heads) == 0: #{
print('[',insent,']','SENTENCE', sent.attrib['ord'], 'has no head.', file=sys.stderr)
Globals.NODES = {};
continue;
elif len(heads) > 1: #{
print('[',insent,']','SENTENCE', sent.attrib['ord'], 'has multiple heads.', file=sys.stderr)
Globals.NODES = {};
continue;
#}
head = heads[0]
# print(head, head.attrib['lem'], file=sys.stderr);
proc_node(0, head, '0');
kk = list(Globals.NODES.keys());
kk.sort();
last = kk[-1];
if len(kk) != last: #{
print('[',insent,']','SENTENCE', sent.attrib['ord'], 'is missing nodes.', kk, '|||',Globals.NODES[kk[0]], file=sys.stderr)
Globals.NODES = {};
continue
#}
tekst = '';
for node in kk: #{
tekst = tekst + Globals.NODES[node][1] + ' ';
#}
tekst = proc_tekst(tekst.strip());
# print('# sent_id = %s' % (sent.attrib['ord']));
print('# sent_id = %s' % (insent));
print('# text = %s' % (tekst));
for node in kk: #{
#1 2 3 4 5 6 7 8 9 10
(id, form, lem, upos, xpos, mi, head, dep, deprels, misc) = Globals.NODES[node];
if typ == "ud":
print('%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s' % (id, form, lem, '_',xpos, '_',head, dep, '_', misc))
else:
misc = misc.replace(xpos+'|', '', 1);
if misc == xpos: #{
misc = '_';
#}
print('%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s' % (id, form, lem, '_',xpos, misc,head, dep, '_', '_'))
#}
#}
Globals.NODES = {};
validsent = validsent + 1;
print('');
#}
print(validsent,'/',insent, file=sys.stderr);