-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevent_dataset.py
343 lines (298 loc) · 15.1 KB
/
event_dataset.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
343
import os
import random
from collections import Counter
import numpy as np
import torch
import copy
import corenlp
import stanfordnlp
class EventReader:
def __init__(self):
pass
def read_events(self, root_dir, files):
'''
:param root_dir: Directory containing .tsv files with token-level event annotations
:param files: List of files to be included in the dataset (used to pass train/dev/test splits)
'''
reader = open(os.path.join(root_dir, files), "r")
file_list = []
for line in reader:
file_list.append(line.strip()+".tsv")
reader.close()
sentences = []
events = []
cur_sentence = []
cur_events = []
for file in file_list:
reader = open(os.path.join(root_dir, file), "r")
for line in reader:
if len(line) > 0:
if line == '\n':
if cur_sentence:
sentences.append(cur_sentence)
events.append(cur_events)
cur_sentence = []
cur_events = []
else:
if len(line.strip().split('\t')) == 1:
continue
word, event = line.strip().split('\t')
cur_sentence.append(word.lower())
cur_events.append(event)
if cur_sentence:
sentences.append(cur_sentence)
events.append(cur_events)
return sentences, events
def create_padded_batches(self, sentences, events, batch_size, use_cuda, shuffle, is_bert=False, inst_weights=None):
combined = list(zip(sentences, events))
if inst_weights is not None:
combined = list(zip(sentences, events, inst_weights))
if shuffle:
random.shuffle(combined)
shuffled_sents, shuffled_events = [], []
if inst_weights is not None:
shuffled_sents, shuffled_events, shuffled_weights = zip(*combined)
shuffled_weights = list(shuffled_weights)
else:
shuffled_sents, shuffled_events = zip(*combined)
shuffled_events = list(shuffled_events)
shuffled_sents = list(shuffled_sents)
batches = []
for i in range(0, len(shuffled_sents), batch_size):
start = i
end = min(len(shuffled_sents), start+batch_size)
cur_sents = shuffled_sents[start:end]
cur_events = shuffled_events[start:end]
cur_weights = None
if inst_weights is not None:
cur_weights = shuffled_weights[start:end]
if cur_weights is not None:
combined = list(zip(cur_sents, cur_events, cur_weights))
else:
combined = list(zip(cur_sents, cur_events))
combined = list(reversed(sorted(combined, key=lambda x: len(x[0]))))
if cur_weights is not None:
cur_sents, cur_events, cur_weights = zip(*combined)
cur_weights = list(cur_weights)
else:
cur_sents, cur_events = zip(*combined)
cur_sents = list(cur_sents)
cur_events = list(cur_events)
cur_lengths = [len(x) for x in cur_sents]
cur_masks = []
max_seq_len = cur_lengths[0]
for i in range(len(cur_sents)):
if not is_bert:
cur_sents[i] = cur_sents[i] + [0] * (max_seq_len - cur_lengths[i])
else:
cur_sents[i] = cur_sents[i] + [[0] * len(cur_sents[i][0]) for j in range(max_seq_len - cur_lengths[i])]
cur_events[i] = cur_events[i] + [0] * (max_seq_len - cur_lengths[i])
cur_masks.append([1] * cur_lengths[i] + [0] * (max_seq_len - cur_lengths[i]))
if not is_bert:
if not use_cuda:
if cur_weights is not None:
batches.append([torch.LongTensor(cur_sents), torch.FloatTensor(cur_events), torch.LongTensor(cur_lengths), torch.FloatTensor(cur_masks), torch.FloatTensor(cur_weights)])
else:
batches.append([torch.LongTensor(cur_sents), torch.FloatTensor(cur_events), torch.LongTensor(cur_lengths), torch.FloatTensor(cur_masks)])
else:
if cur_weights is not None:
batches.append([torch.cuda.LongTensor(cur_sents), torch.cuda.FloatTensor(cur_events), torch.cuda.LongTensor(cur_lengths), torch.cuda.FloatTensor(cur_masks), torch.cuda.FloatTensor(cur_weights)])
else:
batches.append([torch.cuda.LongTensor(cur_sents), torch.cuda.FloatTensor(cur_events), torch.cuda.LongTensor(cur_lengths), torch.cuda.FloatTensor(cur_masks)])
else:
if not use_cuda:
if cur_weights is not None:
batches.append([torch.FloatTensor(cur_sents), torch.FloatTensor(cur_events), torch.LongTensor(cur_lengths), torch.FloatTensor(cur_masks), torch.FloatTensor(cur_weights)])
else:
batches.append([torch.FloatTensor(cur_sents), torch.FloatTensor(cur_events), torch.LongTensor(cur_lengths), torch.FloatTensor(cur_masks)])
else:
#for i in range(len(cur_masks)):
# print('{} {} {}'.format(len(cur_sents[i]), len(cur_events[i]), len(cur_masks[i])))
if cur_weights is not None:
batches.append([torch.cuda.FloatTensor(cur_sents), torch.cuda.FloatTensor(cur_events), torch.cuda.LongTensor(cur_lengths), torch.cuda.FloatTensor(cur_masks), torch.cuda.FloatTensor(cur_weights)])
else:
batches.append([torch.cuda.FloatTensor(cur_sents), torch.cuda.FloatTensor(cur_events), torch.cuda.LongTensor(cur_lengths), torch.cuda.FloatTensor(cur_masks)])
return batches
def create_pos_padded_batches(self, sentences, pos, events, batch_size, use_cuda, shuffle):
combined = list(zip(sentences, pos, events))
if shuffle:
random.shuffle(combined)
shuffled_sents, shuffled_pos, shuffled_events = zip(*combined)
shuffled_events = list(shuffled_events)
shuffled_pos = list(shuffled_pos)
shuffled_sents = list(shuffled_sents)
batches = []
for i in range(0, len(shuffled_sents), batch_size):
start = i
end = min(len(shuffled_sents), start+batch_size)
cur_sents = shuffled_sents[start:end]
cur_pos = shuffled_pos[start:end]
cur_events = shuffled_events[start:end]
combined = list(zip(cur_sents, cur_pos, cur_events))
combined = list(reversed(sorted(combined, key=lambda x: len(x[0]))))
cur_sents, cur_pos, cur_events = zip(*combined)
cur_sents = list(cur_sents)
cur_pos = list(cur_pos)
cur_events = list(cur_events)
cur_lengths = [len(x) for x in cur_sents]
cur_masks = []
max_seq_len = cur_lengths[0]
for i in range(len(cur_sents)):
cur_sents[i] = cur_sents[i] + [0] * (max_seq_len - cur_lengths[i])
cur_pos[i] = cur_pos[i] + [0] * (max_seq_len - cur_lengths[i])
cur_events[i] = cur_events[i] + [0] * (max_seq_len - cur_lengths[i])
cur_masks.append([1] * cur_lengths[i] + [0] * (max_seq_len - cur_lengths[i]))
if not use_cuda:
batches.append([torch.LongTensor(cur_sents), torch.LongTensor(cur_pos), torch.FloatTensor(cur_events), torch.LongTensor(cur_lengths), torch.FloatTensor(cur_masks)])
else:
batches.append([torch.cuda.LongTensor(cur_sents), torch.cuda.LongTensor(cur_pos), torch.cuda.FloatTensor(cur_events), torch.cuda.LongTensor(cur_lengths), torch.cuda.FloatTensor(cur_masks)])
return batches
def construct_vocab(self, sequences):
counter = Counter()
for sequence in sequences:
counter.update(sequence)
vocab = ["<PAD>", "<UNK>"] + list(counter.keys())
print(len(vocab))
vocab = dict(list(zip(vocab, range(len(vocab)))))
return vocab
def construct_integer_sequences(self, sequences, vocab):
int_sequences = []
for sequence in sequences:
new_sequence = []
for word in sequence:
if word in vocab:
new_sequence.append(vocab[word])
else:
if "<UNK>" in vocab:
new_sequence.append(vocab["<UNK>"])
else:
new_sequence.append(vocab["<unk>"]) # For compatibility of this function with models where unk is lowercase
int_sequences.append(new_sequence)
return int_sequences
# Write a sentence-reader class to read sentences with their domain
class SentenceReader:
def __init__(self):
pass
def read_unlabeled_sents(self, root_dir):
sentences = []
domains = []
for file in os.listdir(root_dir):
reader = open(os.path.join(root_dir, file), "r")
for line in reader:
if line == '\n':
continue
sentences.append([x.lower() for x in line.strip().split()])
domains.append(0)
return sentences, domains
def read_unlabeled_sents_as_docs(self, root_dir):
files = []
filenames = []
for file in os.listdir(root_dir):
filenames.append(file)
cur_file = []
reader = open(os.path.join(root_dir, file), "r")
for line in reader:
if line == "\n":
continue
cur_file.append([x.lower() for x in line.strip().split()])
files.append(cur_file)
reader.close()
return filenames, files
def read_labeled_sents(self, sents):
new_sents = copy.deepcopy(sents)
domains = [1]*len(new_sents)
return new_sents, domains
def create_padded_batches(self, sentences, domains, batch_size, use_cuda, shuffle, is_bert=False):
combined = list(zip(sentences, domains))
if shuffle:
random.shuffle(combined)
shuffled_sents, shuffled_domains = zip(*combined)
shuffled_domains = list(shuffled_domains)
shuffled_sents = list(shuffled_sents)
batches = []
for i in range(0, len(shuffled_sents), batch_size):
start = i
end = min(len(shuffled_sents), start + batch_size)
cur_sents = shuffled_sents[start:end]
cur_domains = shuffled_domains[start:end]
combined = list(zip(cur_sents, cur_domains))
combined = list(reversed(sorted(combined, key=lambda x: len(x[0]))))
cur_sents, cur_domains = zip(*combined)
cur_sents = list(cur_sents)
cur_domains = list(cur_domains)
cur_lengths = [len(x) for x in cur_sents]
cur_masks = []
max_seq_len = cur_lengths[0]
for i in range(len(cur_sents)):
if not is_bert:
cur_sents[i] = cur_sents[i] + [0] * (max_seq_len - cur_lengths[i])
else:
cur_sents[i] = cur_sents[i] + [[0] * len(cur_sents[i][0]) for j in range(max_seq_len - cur_lengths[i])]
cur_masks.append([1] * cur_lengths[i] + [0] * (max_seq_len - cur_lengths[i]))
if not is_bert:
if not use_cuda:
batches.append(
[torch.LongTensor(cur_sents), torch.LongTensor(cur_domains), torch.LongTensor(cur_lengths),
torch.FloatTensor(cur_masks)])
else:
batches.append([torch.cuda.LongTensor(cur_sents), torch.cuda.LongTensor(cur_domains),
torch.cuda.LongTensor(cur_lengths), torch.cuda.FloatTensor(cur_masks)])
else:
if not use_cuda:
batches.append(
[torch.FloatTensor(cur_sents), torch.LongTensor(cur_domains), torch.LongTensor(cur_lengths),
torch.FloatTensor(cur_masks)])
else:
batches.append([torch.cuda.FloatTensor(cur_sents), torch.cuda.LongTensor(cur_domains),
torch.cuda.LongTensor(cur_lengths), torch.cuda.FloatTensor(cur_masks)])
return batches
def create_pos_padded_batches(self, sentences, pos, domains, batch_size, use_cuda, shuffle):
combined = list(zip(sentences, pos, domains))
if shuffle:
random.shuffle(combined)
shuffled_sents, shuffled_pos, shuffled_domains = zip(*combined)
shuffled_domains = list(shuffled_domains)
shuffled_pos = list(shuffled_pos)
shuffled_sents = list(shuffled_sents)
batches = []
for i in range(0, len(shuffled_sents), batch_size):
start = i
end = min(len(shuffled_sents), start + batch_size)
cur_sents = shuffled_sents[start:end]
cur_pos = shuffled_pos[start:end]
cur_domains = shuffled_domains[start:end]
combined = list(zip(cur_sents, cur_pos, cur_domains))
combined = list(reversed(sorted(combined, key=lambda x: len(x[0]))))
cur_sents, cur_pos, cur_domains = zip(*combined)
cur_sents = list(cur_sents)
cur_pos = list(cur_pos)
cur_domains = list(cur_domains)
cur_lengths = [len(x) for x in cur_sents]
cur_masks = []
max_seq_len = cur_lengths[0]
for i in range(len(cur_sents)):
cur_sents[i] = cur_sents[i] + [0] * (max_seq_len - cur_lengths[i])
cur_pos[i] = cur_pos[i] + [0] * (max_seq_len - cur_lengths[i])
cur_masks.append([1] * cur_lengths[i] + [0] * (max_seq_len - cur_lengths[i]))
if not use_cuda:
batches.append(
[torch.LongTensor(cur_sents), torch.LongTensor(cur_pos), torch.LongTensor(cur_domains), torch.LongTensor(cur_lengths),
torch.FloatTensor(cur_masks)])
else:
batches.append([torch.cuda.LongTensor(cur_sents), torch.cuda.LongTensor(cur_pos), torch.cuda.LongTensor(cur_domains),
torch.cuda.LongTensor(cur_lengths), torch.cuda.FloatTensor(cur_masks)])
return batches
# TODO: Replace models_dir with local directory containing Stanford CoreNLP English model download
class Parser:
def __init__(self):
self.pipeline = stanfordnlp.Pipeline(processors='tokenize,pos', lang='en', tokenize_pretokenized=True, models_dir="/usr0/home/anaik/installations/stanford-corenlp-full-2018-10-05/en/", treebank="en_ewt")
def parse_sequences(self, sequences):
parse_outputs = []
for sequence in sequences:
annotations = self.pipeline([sequence])
pos_tags = []
for sentence in annotations.sentences:
for token in sentence.words:
pos_tags.append(token.pos)
parse_outputs.append(pos_tags)
return parse_outputs