-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_features.py
268 lines (247 loc) · 9.36 KB
/
create_features.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
import csv
import os
import json
from collections import OrderedDict
import subprocess as sp
from conllu.parser import parse, parse_tree
UDPIPE = "/home/lizaku/Programs/udpipe/src/udpipe"
PATH = "/home/lizaku/Programs/udpipe/UD_Russian/ru.udpipe"
def map_roles(role):
if role:
role = role.strip()
if role in roles:
return roles[role]
else:
return role
def cleansed_text(fname):
with open(fname) as f_in:
contents = f_in.read()
text = re.sub('<[^>]*>', '', contents)
with open(TEXT_PATH, 'a') as f_out:
f_out.write(text + '\n')
def process_conllu(inp):
tree = parse_tree(inp)
root = tree[0]
data = {}
#path = 0
for const in depth_first(root):
w = const[0]['form']
deprel = const[0]['deprel']
data[w] = deprel
return data
def depth_first(node):
yield node
for child in node[1]:
for n in depth_first(child):
yield n
def breadth_first(node): # not finished
yield node
last = node
for child in node[1]:
yield child
last = child
if last == node:
return
def features(ex):
args = OrderedDict()
# lex+, pos+, gram+, prev_gr+, prev_lex+, path from predicate+,
# syntrel with parent+, lemma of a predicate+, preposition+,
# embedding for a predicate, embedding for an argument
lex, pos, gram, prev_gr, prev_lex, rel, pred_lemma = [None]*7
cl = 'noclass'
sent = ' '.join(x for x in ex)
p1 = sp.Popen(["echo", sent], stdout=sp.PIPE)
p2 = sp.Popen([UDPIPE, "--tokenize", "--tag", "--parse",
PATH], stdin=p1.stdout, stdout=sp.PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0].decode('utf-8')
synt_data = process_conllu(output)
n_word = 0
for w in ex:
try:
rel = synt_data[w]
except KeyError:
pass
if n_word == 0:
prev_word, prev_gr, prev_lex = None, None, None
instance = ex[w]
try:
lex, gr, sem, sem2, role, rank = instance
except:
instance = instance[:7] + instance[8:]
try:
pos, gram = gr.split(' ', maxsplit=1)
except:
try:
pos, gram = gr.split(',', maxsplit=1)
except:
pos, gram = None, None
if prev_word:
prev_lex, prev_gr = prev_word[0:2]
#if 'PR' in prev_gr:
# prep =
if instance[-1] is None and instance[-2] is None:
cl = 'noclass'
elif instance[-1] == 'Предикат':
cl = 'Предикат'
elif instance[-1] != 'Предикат' and instance[-2] != '-':
cl = 'Аргумент'
cl = map_roles(role)
d = [lex, pos, gram, prev_gr, prev_lex, rel, pred_lemma, cl]
args[w] = d
prev_word = instance
#print(w, d)
n_word += 1
return args
def predicates(in_f):
preds = {}
with open(in_f, 'r', encoding='utf-8') as f:
js = f.read()
examples = json.loads(js, object_pairs_hook=OrderedDict)
for ex in examples:
len_words = len(examples[ex])
n_word = 0
for w in examples[ex]:
if n_word == 0:
prev_word = None
instance = examples[ex][w]
try:
lex, gr, sem, sem2, role, rank = instance
except:
instance = instance[:7] + instance[8:]
try:
pos, gram = gr.split(' ', maxsplit=1)
except ValueError:
try:
pos, gram = gr.split(',', maxsplit=1)
except:
pos, gram = gr, None
prev_gr, prev_lex = None, None
if prev_word:
prev_lex, prev_gr = prev_word[0:2]
if instance[-1] is None and instance[-2] is None:
n_word += 1
preds[(ex, w)] = [lex, pos, gram, sem, sem2, n_word, prev_gr, prev_lex, 0]
prev_word = instance
continue
if instance[-1] == 'Предикат':
preds[(ex, w)] = [lex, pos, gram, sem, sem2, n_word, prev_gr, prev_lex, 1]
prev_word = instance
n_word += 1
with open('predicates.csv', 'w', encoding='utf-8') as p:
header = ('ExID', 'Wordform', 'Lex', 'POS', 'Gram', 'Sem', 'Sem2', 'WordID', 'prev_lex', 'prev_gr', 'Predicate')
p.write('\t'.join(header) + '\n')
for pr in preds:
p.write('\t'.join(list(pr) + [str(x) for x in preds[pr]]) + '\n')
def arguments(in_f):
args = OrderedDict()
with open(in_f, 'r', encoding='utf-8') as f:
js = f.read()
examples = json.loads(js, object_pairs_hook=OrderedDict)
for ex in examples:
len_words = len(examples[ex])
n_word = 0
for w in examples[ex]:
if n_word == 0:
prev_word = None
instance = examples[ex][w]
try:
lex, gr, sem, sem2, role, rank = instance
except:
instance = instance[:7] + instance[8:]
try:
pos, gram = gr.split(' ', maxsplit=1)
except ValueError:
try:
pos, gram = gr.split(',', maxsplit=1)
except:
pos, gram = gr, None
prev_gr, prev_lex = None, None
if prev_word:
prev_lex, prev_gr = prev_word[0:2]
if instance[-1] is None and instance[-2] is None:
n_word += 1
args[(ex, w)] = [lex, pos, gram, sem, sem2, n_word, prev_gr, prev_lex, '0']
prev_word = instance
continue
if instance[-1] != 'Предикат' and instance[-2] != '-' and instance[-2] is not None and instance[-2] != '?':
role = map_roles(role)
args[(ex, w)] = [lex, pos, gram, sem, sem2, n_word, prev_gr, prev_lex, '1']
prev_word = instance
n_word += 1
with open('arguments.csv', 'w', encoding='utf-8') as p:
writer = csv.writer(p, delimiter='\t')
header = ('ExID', 'Wordform', 'Lex', 'POS', 'Gram', 'Sem', 'Sem2', 'WordID', 'prev_gr', 'prev_lex', 'Class')
writer.writerow(header)
for a in args:
row = list(a) + [str(x) for x in args[a]]
writer.writerow(row)
def together(in_f):
args = {}
with open(in_f, 'r', encoding='utf-8') as f:
js = f.read()
examples = json.loads(js, object_pairs_hook=OrderedDict)
for ex in examples:
len_words = len(examples[ex])
n_word = 0
for w in examples[ex]:
if n_word == 0:
prev_word = None
instance = examples[ex][w]
try:
lex, gr, sem, sem2, role, rank = instance
except:
instance = instance[:7] + instance[8:]
try:
pos, gram = gr.split(' ', maxsplit=1)
except ValueError:
try:
pos, gram = gr.split(',', maxsplit=1)
except:
pos, gram = gr, None
prev_gr, prev_lex = None, None
if prev_word:
prev_lex, prev_gr = prev_word[0:2]
if instance[-1] is None and instance[-2] is None:
n_word += 1
args[(ex, w)] = [lex, pos, gram, sem, sem2, n_word, prev_gr, prev_lex, 'noclass']
prev_word = instance
continue
if instance[-1] == 'Предикат':
args[(ex, w)] = [lex, pos, gram, sem, sem2, n_word, prev_gr, prev_lex, rank]
prev_word = instance
continue
if instance[-1] != 'Предикат' and instance[-2] != '-':
role = map_roles(role)
args[(ex, w)] = [lex, pos, gram, sem, sem2, n_word, prev_lex, prev_gr, role]
prev_word = instance
n_word += 1
with open('arguments_predicates.csv', 'w', encoding='utf-8') as p:
header = ('ExID', 'Wordform', 'Lex', 'POS', 'Gram', 'Sem', 'Sem2', 'WordID', 'prev_lex', 'prev_gr', 'Class')
p.write('\t'.join(header) + '\n')
for a in args:
p.write('\t'.join(list(a) + [str(x) for x in args[a]]) + '\n')
def arg_pred(in_f, out_f):
args = OrderedDict()
with open(in_f, 'r', encoding='utf-8') as f:
js = f.read()
examples = json.loads(js, object_pairs_hook=OrderedDict)
with open(out_f, 'w', encoding='utf-8') as p:
header = ('word', 'lex', 'pos', 'gram', 'prev_gr', 'prev_lex', 'rel', 'pred_lemma', 'class')
p.write('\t'.join(header) + '\n')
for ex in examples:
args = features(examples[ex])
with open(out_f, 'a', encoding='utf-8') as p:
for a in args:
p.write('\t'.join([a] + [str(x) for x in args[a]]) + '\n')
if __name__ == '__main__':
roles = {}
with open('Roles.csv', 'r', encoding='utf-8') as r:
reader = csv.reader(r, delimiter=',')
header = next(reader)
for row in reader:
roles[row[0]] = row[2]
#predicates('parsed_framebank_roles_big.json')
arg_pred('parsed_framebank_roles_big.json', 'roles_syntax.csv')
#arguments('parsed_framebank_roles_big.json')
#together('parsed_framebank_roles.json')