-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_training_data.py
217 lines (168 loc) · 6.96 KB
/
extract_training_data.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
from conll_reader import DependencyStructure, conll_reader
from collections import defaultdict
import copy
import sys
import tensorflow as tf
from tensorflow import keras
import numpy as np
class State(object):
def __init__(self, sentence = []):
self.stack = []
self.buffer = []
if sentence:
self.buffer = list(reversed(sentence))
self.deps = set()
def shift(self):
self.stack.append(self.buffer.pop())
def left_arc(self, label):
self.deps.add( (self.buffer[-1], self.stack.pop(),label) )
def right_arc(self, label):
parent = self.stack.pop()
self.deps.add( (parent, self.buffer.pop(), label) )
self.buffer.append(parent)
def __repr__(self):
return "{},{},{}".format(self.stack, self.buffer, self.deps)
def apply_sequence(seq, sentence):
state = State(sentence)
for rel, label in seq:
if rel == "shift":
state.shift()
elif rel == "left_arc":
state.left_arc(label)
elif rel == "right_arc":
state.right_arc(label)
return state.deps
class RootDummy(object):
def __init__(self):
self.head = None
self.id = 0
self.deprel = None
def __repr__(self):
return "<ROOT>"
def get_training_instances(dep_structure):
deprels = dep_structure.deprels
sorted_nodes = [k for k,v in sorted(deprels.items())]
state = State(sorted_nodes)
state.stack.append(0)
childcount = defaultdict(int)
for ident,node in deprels.items():
childcount[node.head] += 1
seq = []
while state.buffer:
if not state.stack:
seq.append((copy.deepcopy(state),("shift",None)))
state.shift()
continue
if state.stack[-1] == 0:
stackword = RootDummy()
else:
stackword = deprels[state.stack[-1]]
bufferword = deprels[state.buffer[-1]]
if stackword.head == bufferword.id:
childcount[bufferword.id]-=1
seq.append((copy.deepcopy(state),("left_arc",stackword.deprel)))
state.left_arc(stackword.deprel)
elif bufferword.head == stackword.id and childcount[bufferword.id] == 0:
childcount[stackword.id]-=1
seq.append((copy.deepcopy(state),("right_arc",bufferword.deprel)))
state.right_arc(bufferword.deprel)
else:
seq.append((copy.deepcopy(state),("shift",None)))
state.shift()
return seq
dep_relations = ['tmod', 'vmod', 'csubjpass', 'rcmod', 'ccomp', 'poss', 'parataxis', 'appos', 'dep', 'iobj', 'pobj', 'mwe', 'quantmod', 'acomp', 'number', 'csubj', 'root', 'auxpass', 'prep', 'mark', 'expl', 'cc', 'npadvmod', 'prt', 'nsubj', 'advmod', 'conj', 'advcl', 'punct', 'aux', 'pcomp', 'discourse', 'nsubjpass', 'predet', 'cop', 'possessive', 'nn', 'xcomp', 'preconj', 'num', 'amod', 'dobj', 'neg','dt','det']
class FeatureExtractor(object):
def __init__(self, word_vocab_file, pos_vocab_file):
self.word_vocab = self.read_vocab(word_vocab_file)
self.pos_vocab = self.read_vocab(pos_vocab_file)
self.output_labels = self.make_output_labels()
def make_output_labels(self):
labels = []
labels.append(('shift',None))
for rel in dep_relations:
labels.append(("left_arc",rel))
labels.append(("right_arc",rel))
return dict((label, index) for (index,label) in enumerate(labels))
def read_vocab(self,vocab_file):
vocab = {}
for line in vocab_file:
word, index_s = line.strip().split()
index = int(index_s)
vocab[word] = index
return vocab
def get_input_representation(self, words, pos, state):
wordpos = {}
result = []
for index,word in enumerate(words):
wordpos.update({word: pos[index]})
for i in [-1, -2, -3]:
try:
w = words[state.stack[i]]
if wordpos[w] == 'CD':
result.append(self.word_vocab['<CD>'])
elif wordpos[w] == 'NNP':
result.append(self.word_vocab['<NNP>'])
elif wordpos[w] == None:
result.append(self.word_vocab['<ROOT>'])
else:
if w.lower() in self.word_vocab:
result.append(self.word_vocab[w.lower()])
else:
result.append(self.word_vocab['<UNK>'])
except:
result.append(self.word_vocab['<NULL>'])
for i in [-1, -2, -3]:
try:
w = words[state.buffer[i]]
if wordpos[w] == 'CD':
result.append(self.word_vocab['<CD>'])
elif wordpos[w] == 'NNP':
result.append(self.word_vocab['<NNP>'])
elif wordpos[w] == None:
result.append(self.word_vocab['<ROOT>'])
else:
if w.lower() in self.word_vocab:
result.append(self.word_vocab[w.lower()])
else:
result.append(self.word_vocab['<UNK>'])
except:
result.append(self.word_vocab['<NULL>'])
result_return = np.array(result)
return result_return
def get_output_representation(self, output_pair):
result = self.output_labels[output_pair]
#reference:https://keras.io/api/utils/python_utils/#to_categorical-function
result_return = tf.keras.utils.to_categorical(result, num_classes=91)
return result_return
def get_training_matrices(extractor, in_file):
inputs = []
outputs = []
count = 0
for dtree in conll_reader(in_file):
words = dtree.words()
pos = dtree.pos()
for state, output_pair in get_training_instances(dtree):
inputs.append(extractor.get_input_representation(words, pos, state))
outputs.append(extractor.get_output_representation(output_pair))
if count%100 == 0:
sys.stdout.write(".")
sys.stdout.flush()
count += 1
sys.stdout.write("\n")
return np.vstack(inputs),np.vstack(outputs)
if __name__ == "__main__":
WORD_VOCAB_FILE = 'data/words.vocab'
POS_VOCAB_FILE = 'data/pos.vocab'
try:
word_vocab_f = open(WORD_VOCAB_FILE,'r')
pos_vocab_f = open(POS_VOCAB_FILE,'r')
except FileNotFoundError:
print("Could not find vocabulary files {} and {}".format(WORD_VOCAB_FILE, POS_VOCAB_FILE))
sys.exit(1)
with open(sys.argv[1],'r') as in_file:
extractor = FeatureExtractor(word_vocab_f, pos_vocab_f)
print("Starting feature extraction... (each . represents 100 sentences)")
inputs, outputs = get_training_matrices(extractor,in_file)
print("Writing output...")
np.save(sys.argv[2], inputs)
np.save(sys.argv[3], outputs)