-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdependency_parser.py
More file actions
361 lines (281 loc) · 11.3 KB
/
dependency_parser.py
File metadata and controls
361 lines (281 loc) · 11.3 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import conllu
import copy
import transition
import numpy as np
from sklearn import datasets, linear_model
# from sklearn.cross_validation import train_test_split
# from sklearn.metrics import mean_squared_error, r2_score
# from sklearn.feature_extraction import DictVectorizer
from keras.layers import Dense, Embedding, Dropout, Activation
from keras.layers import SimpleRNN
# from keras.layers import LSTM
from keras.layers import GRU
from keras.models import Sequential
import sys
from collections import namedtuple
#from Daniel De Kok's implementation "train.py" in the second Deep Learning Assignment from his SS 2017 Class
class Numberer:
def __init__(self):
self.v2n = dict()
self.n2v = list()
self.start_idx = 1
def number(self, value, add_if_absent=True):
n = self.v2n.get(value)
if n is None:
if add_if_absent:
n = len(self.n2v) + self.start_idx
self.v2n[value] = n
self.n2v.append(value)
else:
return 0
return n
def value(self, number):
return self.n2v[number - 1]
def max_number(self):
return len(self.n2v) + 1
#straight from the transition.py class
def test_parser(parsed, gold_standard):
Token = namedtuple(
'Token', "tid, form lemma pos xpos feats head deprel deps misc children")
def read_conllu(fname=None, fp=sys.stdin, mark_children=False):
if fname is not None:
fp = open(fname, 'r', encoding='utf-8')
treebank = []
sent_start = True
for line in fp:
if line.startswith('#'):
continue
line = line.strip()
if len(line) == 0 and not sent_start:
if mark_children:
for tok in sent:
if tok.head is not None:
hd = sent[tok.head]
hd.children.append(tok.tid)
treebank.append(sent)
sent_start = True
continue
if mark_children: chi = []
else: chi = None
if sent_start:
sent = [Token(
0, "_", "root", "_", "_", "_", None, "_", "_", "_", chi)]
sent_start = False
(tid, form, lemma, pos, xpos, feats, head, deprel, deps, misc) = \
line.strip().split('\t')
if "-" in tid:
continue
sent.append(Token(int(tid),
form,
lemma,
pos,
xpos,
feats,
int(head),
deprel.split(":")[0],
deps,
misc,
chi))
return treebank
conllu.save(parsed, "..\\parsed")
conllu.save(gold_standard, "..\\gold_standard")
out = read_conllu("..\\parsed")
gs = read_conllu("..\\gold_standard")
# out = read_conllu(parsed)
# gs = read_conllu(gold_standard)
if len(out) != len(gs):
print("The number of sentences differ!")
sys.exit(-1)
arcs_lmatch_w = 0
arcs_umatch_w = 0
arcs_total = 0
for i in range(len(out)):
sent_out = out[i]
sent_gs = gs[i]
if len(sent_out) != len(sent_gs):
print("The number of words differ in sentence {}".format(i))
sys.exit(-1)
arcs_lmatch_sent = 0
arcs_umatch_sent = 0
ntokens = len(sent_out) - 1
for j in range(1,len(sent_out)):
if sent_out[j].head == sent_gs[j].head:
arcs_umatch_sent += 1
if sent_out[j].deprel == sent_gs[j].deprel:
arcs_lmatch_sent += 1
arcs_total += ntokens
arcs_lmatch_w += arcs_lmatch_sent
arcs_umatch_w += arcs_umatch_sent
print("UAS: {:.2f}\tLAS: {:.2f}".format(
100 * arcs_umatch_w / arcs_total,
100 * arcs_lmatch_w / arcs_total)
)
def format_as_sents(conllu_path):
sents = list(conllu.load(conllu_path))
# conllu.save(sents, temp)
# assert sents == list(conllu.load(temp))
return sents
# is it more efficient to write them tos a file or save all as a list?
# pickle the list?
# return sents and use it directly?
def get_things(sents,proj=False, lazy=True, verbose=True):
everything = []
for s in sents:
o = transition.Oracle(s, proj, lazy)
c = transition.Config(s)
feat_list = []
tup_list = []
while not c.is_terminal():
act, arg = o.predict(c)
# print(o.predict(c))
# print(c.stack_nth(1))
# print(c.stack)
# print(c.input)
feat_list = features_to_list(s,c)
tup_list.append("{}\t{}".format(act, arg))
tup_list.append(feat_list)
#create two-tuples as specified by assignment
two_tup = tuple(tup_list)
everything.append(two_tup)
feat_list = []
tup_list = []
if verbose: print("{}\t{}".format(act, arg))
assert c.doable(act)
getattr(c, act)(arg)
return everything
def features_to_list(s,c):
feat_list = []
# add features and pos tags from stack
for x in range(3, 0, -1):
try:
if c.stack_nth(x)== 0:
feat_list.append("ROOT")
feat_list.append("root")
else:
#do we need stacknth
feat_list.append(s.upostag[c.stack_nth(x)])
feat_list.append(s.form[c.stack_nth(x)])
except IndexError:
feat_list.append("")
feat_list.append("")
# add features and pos tags from buffer
for y in range(1, 4, 1):
try:
feat_list.append(s.upostag[c.input_nth(y)])
feat_list.append(s.form[c.input_nth(y)])
except IndexError:
feat_list.append("")
feat_list.append("")
return feat_list
def train_classifier(formpos_num, labels_num, train_list,val_list):
#ADD VALIDATION ST
labels_train_numbered = []
feat_train_numbered = []
all_feats_numbered_tr =[]
labels_test_numbered = []
feat_test_numbered = []
all_feats_numbered_tst =[]
for two_tup in train_list:
label = two_tup[0]
numbed_label = labels_num.number(label, True)
labels_train_numbered.append(numbed_label)
for form_or_pos in two_tup[1]:
numbed_feature = formpos_num.number(form_or_pos, True)
feat_train_numbered.append(numbed_feature)
all_feats_numbered_tr.append(feat_train_numbered)
feat_train_numbered = []
# assert len(labels_train_numbered) == len(all_feats_numbered_tr)
for two_tup in val_list:
label = two_tup[0]
numbed_label = labels_num.number(label, False)
labels_test_numbered.append(numbed_label)
for form_or_pos in two_tup[1]:
numbed_feature = formpos_num.number(form_or_pos, False)
feat_test_numbered.append(numbed_feature)
all_feats_numbered_tst.append(feat_test_numbered)
feat_test_numbered = []
all_feats_numbered_tst = np.asarray(all_feats_numbered_tst)
labels_test_numbered = np.asarray(labels_test_numbered)
all_feats_numbered_tr = np.asarray(all_feats_numbered_tr)
labels_train_numbered = np.asarray(labels_train_numbered)
#I started out by using dictVectorizer, numbering arrays and putting into linear model, but got very poor results
#I learned more about Keras from: https://machinelearningmastery.com/sequence-classification-lstm-recurrent-neural-networks-python-keras/
#I asked Ryan Callihan and Madeesh Kannan for advice on using Keras embeddings ~Sam
model = Sequential()
embedding_size = 100
model.add(Embedding(input_dim = formpos_num.max_number(),output_dim = embedding_size,
input_length = all_feats_numbered_tr.shape[1]))
# model.add(LSTM(embedding_size))
# model.add(SimpleRNN(embedding_size, dropout = 0.2, recurrent_dropout = 0.1))
model.add(GRU(embedding_size, recurrent_dropout = 0.1, dropout = 0.2))
model.add(Dense(labels_num.max_number(), activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(all_feats_numbered_tr, labels_train_numbered, epochs= 10, batch_size = 64, verbose=1)
score = model.evaluate(all_feats_numbered_tst, labels_test_numbered)
print("Accuracy: %.2f%%" % (score[1]*100))
return model
def parse_sent(sent, classifier, features_num,labels_num):
#needs to predict labels for a list of features that have been numbered
#during EVERY tansition step
c = transition.Config(sent)
feat_list = []
bigger_list = []
feat_list_numd = []
feat_list = features_to_list(sent, c)
for feature in feat_list:
feat_list_numd.append(features_num.number(feature, False))
#I need to do this in order to get the right shape
bigger_list.append(feat_list_numd)
feat_list_numd = np.asarray(bigger_list)
choices_sorted = []
predictions = classifier.predict(feat_list_numd)
#predict probabilities of act/arg pairs for every step of the parse
for preds in predictions:
preds_list = np.argsort(preds)[::-1]
poss_acts = []
for pred_step in preds_list:
act_arg_pair = labels_num.value(pred_step)
poss_acts.append(act_arg_pair)
choices_sorted.append(poss_acts)
while not c.is_terminal():
for step in choices_sorted:
for poss_choice in step:
act,arg = poss_choice.split("\t")
if not c.doable(act):
continue
else:
getattr(c, act)(arg)
#found a working choice, get out of this step
break
#is this what we want to return? I guess
return c.finish()
if '__main__' == __name__:
import sys
# do we need the temp path?
try:
train_file, val_file = sys.argv[1:]
except ValueError:
sys.exit("usage: {} train_file val_file".format(sys.argv[0]))
labels_num = Numberer()
features_num = Numberer()
train_sents = format_as_sents(train_file)
val_sents = format_as_sents(val_file)
labels_and_features_tr = get_things(train_sents, verbose=False)
labels_and_features_val = get_things(val_sents, verbose=False)
model = train_classifier(features_num,labels_num,labels_and_features_tr,labels_and_features_val)
#parse every val sent, compare with gold standard (output from oracle) for ALL the sents
parses = []
for sent in val_sents:
parses.append(parse_sent(sent,model,features_num, labels_num))
test_parser(parses, val_sents)
print("yes!")
'''
a GRU was found to work better than the LSTM or SimpleRNN
The laptop was getting very hot when training on the full set for a long time,
so results were recorded after 1 epoch:
Accuracy: 84.90% UAS: 6.78 LAS: 0.20
On smaller training and validation sets(about 1/10 of each file), the results for 10 epochs were:
Accuracy: 87.10% UAS: 6.93 LAS: 0.43
'''