-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvua_data_helper.py
343 lines (322 loc) · 18.4 KB
/
vua_data_helper.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
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
"""
vua_data_helper.py
- process VUA data
"""
import logging
import os
import sys
import numpy as np
import pickle
from data_utils import InputExample, InputFeatures, _parse_str_vector
def read_vua_examples_from_file(data_folder, mode):
"""
@param sent_file: one sentnece per line and words are separated by space
@param label_file: word labels separated by space, 1: metaphor, 0: non-metaphor
"""
examples = []
prefix = data_folder + mode + "_"
if mode == "train":
sent_file = open(prefix+"tokens.txt", "r")
pos_file = open(prefix+"pos.txt", "r")
#mask_file = open(prefix+"mask.txt", "r")
biasdown_file = open(prefix+"biasdown.txt", "r")
biasup_file = open(prefix+"biasup.txt", "r")
biasupdown_file = open(prefix+"biasupdown.txt", "r")
corp_file = open(prefix+"corp.txt", "r")
topic_file = open(prefix+"topic.txt", "r")
verbnet_file = open(prefix+"verbnet.txt", "r")
wordnet_file = open(prefix+"wordnet.txt", "r")
label_file = open(prefix+"metaphor.txt", "r")
example_id = 0
for (sent_line, pos_line, biasdown_line, biasup_line, biasupdown_line,
corp_line, topic_line, verbnet_line, wordnet_line, label_line) in \
zip(sent_file, pos_file, biasdown_file, biasup_file, biasupdown_file,
corp_file, topic_file, verbnet_file, wordnet_file, label_file):
words = sent_line.strip().split()
pos_list = pos_line.strip().split()
#content_mask_list = [int(val) for val in mask_line.strip().split()]
biasdown_vector_list = _parse_str_vector(biasdown_line)
biasup_vector_list = _parse_str_vector(biasup_line)
biasupdown_vector_list = _parse_str_vector(biasupdown_line)
corp_vector_list = _parse_str_vector(corp_line)
topic_vector_list = _parse_str_vector(topic_line)
verbnet_vector_list = _parse_str_vector(verbnet_line)
wordnet_vector_list = _parse_str_vector(wordnet_line)
labels = [int(label) for label in label_line.strip().split()]
examples.append(InputExample(example_id="{}-{}".format(mode, str(example_id)),
words=words, pos_list=pos_list,
#content_mask_list=content_mask_list,
biasdown_vectors=biasdown_vector_list,
biasup_vectors=biasup_vector_list,
biasupdown_vectors=biasupdown_vector_list,
corp_vectors=corp_vector_list,
topic_vectors=topic_vector_list,
verbnet_vectors=verbnet_vector_list,
wordnet_vectors=wordnet_vector_list,
labels=labels))
example_id += 1
sent_file.close()
pos_file.close()
#mask_file.close()
biasdown_file.close()
biasup_file.close()
biasupdown_file.close()
corp_file.close()
topic_file.close()
verbnet_file.close()
wordnet_file.close()
label_file.close()
# test data does not have labels
elif mode == "test":
sent_file = open(prefix+"tokens.txt", "r")
pos_file = open(prefix+"pos.txt", "r")
#mask_file = open(prefix+"mask.txt", "r")
biasdown_file = open(prefix+"biasdown.txt", "r")
biasup_file = open(prefix+"biasup.txt", "r")
biasupdown_file = open(prefix+"biasupdown.txt", "r")
corp_file = open(prefix+"corp.txt", "r")
topic_file = open(prefix+"topic.txt", "r")
verbnet_file = open(prefix+"verbnet.txt", "r")
wordnet_file = open(prefix+"wordnet.txt", "r")
example_id = 0
for (sent_line, pos_line, biasdown_line, biasup_line, biasupdown_line,
corp_line, topic_line, verbnet_line, wordnet_line) in \
zip(sent_file, pos_file, biasdown_file, biasup_file, biasupdown_file,
corp_file, topic_file, verbnet_file, wordnet_file):
words = sent_line.strip().split()
pos_list = pos_line.strip().split()
#content_mask_list = [int(val) for val in mask_line.strip().split()]
biasdown_vector_list = _parse_str_vector(biasdown_line)
biasup_vector_list = _parse_str_vector(biasup_line)
biasupdown_vector_list = _parse_str_vector(biasupdown_line)
corp_vector_list = _parse_str_vector(corp_line)
topic_vector_list = _parse_str_vector(topic_line)
verbnet_vector_list = _parse_str_vector(verbnet_line)
wordnet_vector_list = _parse_str_vector(wordnet_line)
pseudo_labels = [0] * len(words)
examples.append(InputExample(example_id="{}-{}".format(mode, str(example_id)),
words=words, pos_list=pos_list,
#content_mask_list=content_mask_list,
biasdown_vectors=biasdown_vector_list,
biasup_vectors=biasup_vector_list,
biasupdown_vectors=biasupdown_vector_list,
corp_vectors=corp_vector_list,
topic_vectors=topic_vector_list,
verbnet_vectors=verbnet_vector_list,
wordnet_vectors=wordnet_vector_list,
labels=pseudo_labels))
sent_file.close()
pos_file.close()
#mask_file.close()
biasdown_file.close()
biasup_file.close()
biasupdown_file.close()
corp_file.close()
topic_file.close()
verbnet_file.close()
wordnet_file.close()
return examples
def convert_vua_examples_to_features(
examples,
max_seq_length,
tokenizer,
cls_token_at_end=False,
cls_token="[CLS]",
cls_token_segment_id=1,
sep_token="[SEP]",
sep_token_extra=True,
pad_on_left=False,
pad_token=0,
pad_token_segment_id=0,
pos_vocab={},
pad_pos_id=0,
pad_token_label_id=-100,
pad_feature_val=0,
sequence_a_segment_id=0,
mask_padding_with_zero=True,
mode="train"):
features = []
sent_lens = []
# track dimension for each feature vector
feature_dim_dict = {'biasdown': 17, 'biasup': 17, 'biasupdown': 66, 'corp': 200, \
'topic': 100, 'verbnet': 270, 'wordnet': 26}
for (eid, example) in enumerate(examples):
if eid % 10000 == 0:
print("Writing example %d of %d", eid, len(examples))
tokens = []
pos_ids = []
#content_mask = []
biasdown_vectors = []
biasup_vectors = []
biasupdown_vectors = []
corp_vectors = []
topic_vectors = []
verbnet_vectors = []
wordnet_vectors = []
label_ids = []
for (word, pos, biasdown_vector, biasup_vector, biasupdown_vector,
corp_vector, topic_vector, verbnet_vector, wordnet_vector, label) in \
zip(example.words, example.pos_list, example.biasdown_vectors,
example.biasup_vectors, example.biasupdown_vectors,
example.corp_vectors, example.topic_vectors,
example.verbnet_vectors, example.wordnet_vectors, example.labels):
# a word might be split into multiple tokens
word_tokens = tokenizer.tokenize(word)
tokens += list(word_tokens)
pos_ids += [pos_vocab[pos]] + [pad_pos_id] * (len(word_tokens)-1)
biasdown_vectors += [biasdown_vector] + \
[[pad_feature_val]*len(biasdown_vector) for i in range(len(word_tokens)-1)]
biasup_vectors += [biasup_vector] + \
[[pad_feature_val]*len(biasup_vector) for i in range(len(word_tokens)-1)]
biasupdown_vectors += [biasupdown_vector] + \
[[pad_feature_val]*len(biasupdown_vector) for i in range(len(word_tokens)-1)]
corp_vectors += [corp_vector] + \
[[pad_feature_val]*len(corp_vector) for i in range(len(word_tokens)-1)]
topic_vectors += [topic_vector] + \
[[pad_feature_val]*len(topic_vector) for i in range(len(word_tokens)-1)]
verbnet_vectors += [verbnet_vector] + \
[[pad_feature_val]*len(verbnet_vector) for i in range(len(word_tokens)-1)]
wordnet_vectors += [wordnet_vector] + \
[[pad_feature_val]*len(wordnet_vector) for i in range(len(word_tokens)-1)]
# Use the real label id for the first token of the word,
# and padding ids for the remaining tokens
label_ids += [label] + [pad_token_label_id] * (len(word_tokens)-1)
sent_lens.append(len(tokens))
# Account for [CLS] and [SEP] with "- 2" and with "- 3" for RoBERTa.
special_tokens_count = 3 if sep_token_extra else 2
if len(tokens) > max_seq_length - special_tokens_count:
tokens = tokens[: (max_seq_length - special_tokens_count)]
pos_ids = pos_ids[: (max_seq_length - special_tokens_count)]
biasdown_vectors = biasdown_vectors[: (max_seq_length - special_tokens_count)]
biasup_vectors = biasup_vectors[: (max_seq_length - special_tokens_count)]
biasupdown_vectors = biasupdown_vectors[: (max_seq_length - special_tokens_count)]
corp_vectors = corp_vectors[: (max_seq_length - special_tokens_count)]
topic_vectors = topic_vectors[: (max_seq_length - special_tokens_count)]
verbnet_vectors = verbnet_vectors[: (max_seq_length - special_tokens_count)]
wordnet_vectors = wordnet_vectors[: (max_seq_length - special_tokens_count)]
label_ids = label_ids[: (max_seq_length - special_tokens_count)]
tokens += [sep_token]
pos_ids += [pad_pos_id]
biasdown_vectors += [[pad_feature_val] * feature_dim_dict["biasdown"]]
biasup_vectors += [[pad_feature_val] * feature_dim_dict['biasup']]
biasupdown_vectors += [[pad_feature_val] * feature_dim_dict['biasupdown']]
corp_vectors += [[pad_feature_val] * feature_dim_dict['corp']]
topic_vectors += [[pad_feature_val] * feature_dim_dict['topic']]
verbnet_vectors += [[pad_feature_val] * feature_dim_dict['verbnet']]
wordnet_vectors += [[pad_feature_val] * feature_dim_dict['wordnet']]
label_ids += [pad_token_label_id]
if sep_token_extra:
# RoBERTa uses an extra separator b/w pairs of sentences
tokens += [sep_token]
pos_ids += [pad_pos_id]
biasdown_vectors += [[pad_feature_val] * feature_dim_dict["biasdown"]]
biasup_vectors += [[pad_feature_val] * feature_dim_dict['biasup']]
biasupdown_vectors += [[pad_feature_val] * feature_dim_dict['biasupdown']]
corp_vectors += [[pad_feature_val] * feature_dim_dict['corp']]
topic_vectors += [[pad_feature_val] * feature_dim_dict['topic']]
verbnet_vectors += [[pad_feature_val] * feature_dim_dict['verbnet']]
wordnet_vectors += [[pad_feature_val] * feature_dim_dict['wordnet']]
label_ids += [pad_token_label_id]
segment_ids = [sequence_a_segment_id] * len(tokens)
if cls_token_at_end:
tokens += [cls_token]
pos_ids += [pad_pos_id]
biasdown_vectors += [[pad_feature_val] * feature_dim_dict["biasdown"]]
biasup_vectors += [[pad_feature_val] * feature_dim_dict['biasup']]
biasupdown_vectors += [[pad_feature_val] * feature_dim_dict['biasupdown']]
corp_vectors += [[pad_feature_val] * feature_dim_dict['corp']]
topic_vectors += [[pad_feature_val] * feature_dim_dict['topic']]
verbnet_vectors += [[pad_feature_val] * feature_dim_dict['verbnet']]
wordnet_vectors += [[pad_feature_val] * feature_dim_dict['wordnet']]
label_ids += [pad_token_label_id]
segment_ids += [cls_token_segment_id]
else:
tokens = [cls_token] + tokens
pos_ids = [pad_pos_id] + pos_ids
biasdown_vectors = [[pad_feature_val] * feature_dim_dict["biasdown"]] + biasdown_vectors
biasup_vectors = [[pad_feature_val] * feature_dim_dict['biasup']] + biasup_vectors
biasupdown_vectors = [[pad_feature_val] * feature_dim_dict['biasupdown']] + biasupdown_vectors
corp_vectors = [[pad_feature_val] * feature_dim_dict['corp']] + corp_vectors
topic_vectors = [[pad_feature_val] * feature_dim_dict['topic']] + topic_vectors
verbnet_vectors = [[pad_feature_val] * feature_dim_dict['verbnet']] + verbnet_vectors
wordnet_vectors = [[pad_feature_val] * feature_dim_dict['wordnet']] + wordnet_vectors
label_ids = [pad_token_label_id] + label_ids
segment_ids = [cls_token_segment_id] + segment_ids
input_ids = tokenizer.convert_tokens_to_ids(tokens)
# The mask has 1 for real tokens and 0 for padding tokens. Only real
# tokens are attended to.
input_mask = [1 if mask_padding_with_zero else 0] * len(input_ids)
# Zero-pad up to the sequence length.
padding_length = max_seq_length - len(input_ids)
if pad_on_left:
input_ids = ([pad_token] * padding_length) + input_ids
input_mask = ([0 if mask_padding_with_zero else 1] * padding_length) + input_mask
segment_ids = ([pad_token_segment_id] * padding_length) + segment_ids
pos_ids = ([pad_pos_id] * padding_length) + pos_ids
biasdown_vectors = [[pad_feature_val] * feature_dim_dict["biasdown"] for i in range(padding_length)] \
+ biasdown_vectors
biasup_vectors = [[pad_feature_val] * feature_dim_dict['biasup'] for i in range(padding_length)] \
+ biasup_vectors
biasupdown_vectors = [[pad_feature_val] * feature_dim_dict['biasupdown'] for i in range(padding_length)] \
+ biasupdown_vectors
corp_vectors = [[pad_feature_val] * feature_dim_dict['corp'] for i in range(padding_length)] \
+ corp_vectors
topic_vectors = [[pad_feature_val] * feature_dim_dict['topic'] for i in range(padding_length)] \
+ topic_vectors
verbnet_vectors = [[pad_feature_val] * feature_dim_dict['verbnet'] for i in range(padding_length)] \
+ verbnet_vectors
wordnet_vectors = [[pad_feature_val] * feature_dim_dict['wordnet'] for i in range(padding_length)] \
+ wordnet_vectors
label_ids = ([pad_token_label_id] * padding_length) + label_ids
else:
input_ids += [pad_token] * padding_length
input_mask += [0 if mask_padding_with_zero else 1] * padding_length
segment_ids += [pad_token_segment_id] * padding_length
pos_ids += [pad_pos_id] * padding_length
biasdown_vectors += [[pad_feature_val] * feature_dim_dict["biasdown"] for i in range(padding_length)]
biasup_vectors += [[pad_feature_val] * feature_dim_dict['biasup'] for i in range(padding_length)]
biasupdown_vectors += [[pad_feature_val] * feature_dim_dict['biasupdown'] for i in range(padding_length)]
corp_vectors += [[pad_feature_val] * feature_dim_dict['corp'] for i in range(padding_length)]
topic_vectors += [[pad_feature_val] * feature_dim_dict['topic'] for i in range(padding_length)]
verbnet_vectors += [[pad_feature_val] * feature_dim_dict['verbnet'] for i in range(padding_length)]
wordnet_vectors += [[pad_feature_val] * feature_dim_dict['wordnet'] for i in range(padding_length)]
label_ids += [pad_token_label_id] * padding_length
assert len(input_ids) == max_seq_length
assert len(input_mask) == max_seq_length
assert len(segment_ids) == max_seq_length
assert len(pos_ids) == max_seq_length
assert len(biasdown_vectors) == max_seq_length
assert len(biasup_vectors) == max_seq_length
assert len(biasupdown_vectors) == max_seq_length
assert len(corp_vectors) == max_seq_length
assert len(topic_vectors) == max_seq_length
assert len(verbnet_vectors) == max_seq_length
assert len(wordnet_vectors) == max_seq_length
assert len(label_ids) == max_seq_length
# peek data
if eid < 4:
print("*** Example ***")
print("example_id: %s", example.example_id)
print("tokens: %s", " ".join([str(x) for x in tokens]))
print("input_ids: %s", " ".join([str(x) for x in input_ids]))
print("input_mask: %s", " ".join([str(x) for x in input_mask]))
print("segment_ids: %s", " ".join([str(x) for x in segment_ids]))
print("pos_ids: %s", " ".join([str(x) for x in pos_ids]))
print("label_ids: %s", " ".join([str(x) for x in label_ids]))
features.append(InputFeatures(input_ids=input_ids,
input_mask=input_mask,
segment_ids=segment_ids,
pos_ids=pos_ids,
biasdown_vectors=biasdown_vectors,
biasup_vectors=biasup_vectors,
biasupdown_vectors=biasupdown_vectors,
corp_vectors=corp_vectors,
topic_vectors=topic_vectors,
verbnet_vectors=verbnet_vectors,
wordnet_vectors=wordnet_vectors,
label_ids=label_ids))
print("# of examples: {}, avg sent_len: {}".format(len(sent_lens), np.mean(sent_lens)))
print("min sent len: {}, max_sent_len: {}".format(min(sent_lens), max(sent_lens)))
#print("feature_dim_dict: {}".format(feature_dim_dict))
print("feature_dim: {}".format(sum([feature_dim_dict[f] for f in feature_dim_dict])))
return features