-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
executable file
·242 lines (217 loc) · 12.5 KB
/
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
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
import tqdm
import time
import datetime
import random
random.seed(10)
from document_reader import *
from os import listdir
from os.path import isfile, join
from EventDataset import EventDataset
from torch.utils.data import Dataset, DataLoader
from util import *
import json
from transformers import RobertaTokenizer, BigBirdTokenizer
def context_getter(my_dict, x_sent_id, y_sent_id, max_sent_len = 4096):
context = []
context += my_dict["sentences"][x_sent_id]["_subword_to_ID"]
for sent_id in range(x_sent_id + 1, y_sent_id):
context += my_dict["sentences"][sent_id]["_subword_to_ID"][1:]
offset = len(context) - 1
context += my_dict["sentences"][y_sent_id]["_subword_to_ID"][1:]
return padding(context, max_sent_len = max_sent_len), offset, len(context)
rel_type = {"IC": 0, "HiEve": 0, "MATRES": 1, "TB-Dense": 1, "CaTeRS": 2, "RED": 3}
def data(dataset, debugging, downsample, batch_size, transformers, transformers_model, undersmp_ratio = 0.4, shuffle = True):
if transformers == 'BigBird':
tokenizer = BigBirdTokenizer.from_pretrained(transformers_model)
else:
tokenizer = RobertaTokenizer.from_pretrained(transformers_model)
train_set = []
valid_set = []
test_set = []
if debugging:
train_range = range(0, 1)
valid_range = range(1, 2)
test_range = range(2, 3)
else:
train_range = range(0, 60)
valid_range = range(60, 80)
test_range = range(80, 100)
_max = 0
count_rel = {0: 0, 1: 0, 2: 0, 3: 0}
print("Processing " + dataset + " dataset...")
if transformers == 'BigBird':
if dataset == "IC":
# IC Preprocessing took 0:24:38
dir_name = "./IC/IC_Processed/"
max_sent_len = 1533
elif dataset == "HiEve":
# HiEve Preprocessing took 0:13:23
dir_name = "./hievents_v2/processed/"
max_sent_len = 1313
else:
print("Not supporting this dataset yet!")
else:
if dataset == "IC":
dir_name = "./IC/IC_Processed/"
max_sent_len = 193
elif dataset == "HiEve":
dir_name = "./hievents_v2/processed/"
max_sent_len = 155
else:
print("Not supporting this dataset yet!")
onlyfiles = [f for f in listdir(dir_name) if isfile(join(dir_name, f)) and f[-4:] == "tsvx"]
onlyfiles.sort()
doc_id = -1
t0 = time.time()
for file_name in tqdm.tqdm(onlyfiles):
doc_id += 1
if doc_id in train_range:
my_dict = tsvx_reader(dataset, dir_name, file_name, transformers, transformers_model)
num_event = len(my_dict["event_dict"])
print("num_event", num_event)
# range(a, b): [a, b)
for x in range(1, num_event+1):
for y in range(x+1, num_event+1):
for z in range(y+1, num_event+1):
x_sent_id = my_dict["event_dict"][x]["sent_id"]
y_sent_id = my_dict["event_dict"][y]["sent_id"]
z_sent_id = my_dict["event_dict"][z]["sent_id"]
x_sent = my_dict["sentences"][x_sent_id]["_subword_to_ID"]
y_sent = my_dict["sentences"][y_sent_id]["_subword_to_ID"]
z_sent = my_dict["sentences"][z_sent_id]["_subword_to_ID"]
'''
concat two sentences following PairedRL
<s> sentence_1 </s> sentence_2 </s> <pad> ... <pad>
0, ..., 2, ..., 2, 1, ..., 1
'''
if transformers == 'BigBird':
xy_sent, offset_xy, len_xy = context_getter(my_dict, x_sent_id, y_sent_id, max_sent_len)
yz_sent, offset_yz, len_yz = context_getter(my_dict, y_sent_id, z_sent_id, max_sent_len)
xz_sent, offset_xz, len_xz = context_getter(my_dict, x_sent_id, z_sent_id, max_sent_len)
else:
xy_sent = padding(x_sent + y_sent[1:], max_sent_len = max_sent_len)
yz_sent = padding(y_sent + z_sent[1:], max_sent_len = max_sent_len)
xz_sent = padding(x_sent + z_sent[1:], max_sent_len = max_sent_len)
offset_xy = len(x_sent)-1
offset_yz = len(y_sent)-1
offset_xz = len(x_sent)-1
len_xy = len(xy_sent)
len_yz = len(yz_sent)
len_xz = len(xz_sent)
'''
HiEve PairedRL context max length: 155
IC PairedRL context max length: 193
'''
_max = max(_max, len_xy, len_yz, len_xz)
x_position = my_dict["event_dict"][x]["_subword_id"]
y_position = my_dict["event_dict"][y]["_subword_id"]
z_position = my_dict["event_dict"][z]["_subword_id"]
x_subword_len = len(tokenizer.encode(my_dict["event_dict"][x]['mention'])) - 2
y_subword_len = len(tokenizer.encode(my_dict["event_dict"][y]['mention'])) - 2
z_subword_len = len(tokenizer.encode(my_dict["event_dict"][z]['mention'])) - 2
#x_sent_pos = padding(my_dict["sentences"][x_sent_id]["_subword_pos"], pos = True)
#y_sent_pos = padding(my_dict["sentences"][y_sent_id]["_subword_pos"], pos = True)
#z_sent_pos = padding(my_dict["sentences"][z_sent_id]["_subword_pos"], pos = True)
xy = my_dict["relation_dict"][(x, y)]["relation"]
yz = my_dict["relation_dict"][(y, z)]["relation"]
xz = my_dict["relation_dict"][(x, z)]["relation"]
x_seg_id = my_dict["event_dict"][x]["segment_id"]
y_seg_id = my_dict["event_dict"][y]["segment_id"]
z_seg_id = my_dict["event_dict"][z]["segment_id"]
to_append = xy_sent, x_position, x_position+x_subword_len, offset_xy+y_position, offset_xy+y_position+y_subword_len, \
yz_sent, y_position, y_position+y_subword_len, offset_yz+z_position, offset_yz+z_position+z_subword_len, \
xz_sent, x_position, x_position+x_subword_len, offset_xz+z_position, offset_xz+z_position+z_subword_len, \
float(x_seg_id==y_seg_id), float(y_seg_id==z_seg_id), float(x_seg_id==z_seg_id), \
xy, yz, xz, rel_type[dataset], x, y, z, doc_id
if xy == 3 and yz == 3:
pass
elif xy == 3 or yz == 3 or xz == 3:
if random.uniform(0, 1) <= downsample:
train_set.append(to_append)
count_rel[xy] += 1
count_rel[yz] += 1
count_rel[xz] += 1
else:
train_set.append(to_append)
count_rel[xy] += 1
count_rel[yz] += 1
count_rel[xz] += 1
'''
if xy == 3:
if random.uniform(0, 1) <= downsample:
train_set.append(to_append)
count_rel[xy] += 1
count_rel[yz] += 1
count_rel[xz] += 1
else:
train_set.append(to_append)
count_rel[xy] += 1
count_rel[yz] += 1
count_rel[xz] += 1
'''
elif doc_id in valid_range or doc_id in test_range:
my_dict = tsvx_reader(dataset, dir_name, file_name, transformers, transformers_model)
num_event = len(my_dict["event_dict"])
for x in range(1, num_event+1):
for y in range(x+1, num_event+1):
x_sent_id = my_dict["event_dict"][x]["sent_id"]
y_sent_id = my_dict["event_dict"][y]["sent_id"]
x_sent = my_dict["sentences"][x_sent_id]["_subword_to_ID"]
y_sent = my_dict["sentences"][y_sent_id]["_subword_to_ID"]
'''
concat two sentences following PairedRL
<s> sentence_1 </s> sentence_2 </s> <pad> ... <pad>
0, ..., 2, ..., 2, 1, ..., 1
'''
if transformers == 'BigBird':
xy_sent, offset_xy, len_xy = context_getter(my_dict, x_sent_id, y_sent_id, max_sent_len)
else:
xy_sent = padding(x_sent + y_sent[1:], max_sent_len = max_sent_len)
offset_xy = len(x_sent)-1
len_xy = len(xy_sent)
_max = max(_max, len_xy)
x_position = my_dict["event_dict"][x]["_subword_id"]
y_position = my_dict["event_dict"][y]["_subword_id"]
x_subword_len = len(tokenizer.encode(my_dict["event_dict"][x]['mention'])) - 2
y_subword_len = len(tokenizer.encode(my_dict["event_dict"][y]['mention'])) - 2
#x_sent_pos = padding(my_dict["sentences"][x_sent_id]["_subword_pos"], pos = True)
#y_sent_pos = padding(my_dict["sentences"][y_sent_id]["_subword_pos"], pos = True)
x_seg_id = my_dict["event_dict"][x]["segment_id"]
y_seg_id = my_dict["event_dict"][y]["segment_id"]
xy = my_dict["relation_dict"][(x, y)]["relation"]
to_append = xy_sent, x_position, x_position+x_subword_len, offset_xy+y_position, offset_xy+y_position+y_subword_len, \
xy_sent, x_position, x_position+x_subword_len, offset_xy+y_position, offset_xy+y_position+y_subword_len, \
xy_sent, x_position, x_position+x_subword_len, offset_xy+y_position, offset_xy+y_position+y_subword_len, \
float(x_seg_id==y_seg_id), float(x_seg_id==y_seg_id), float(x_seg_id==y_seg_id), \
xy, xy, xy, rel_type[dataset], x, y, x, doc_id
if doc_id in valid_range:
if xy == 3:
if random.uniform(0, 1) <= undersmp_ratio:
valid_set.append(to_append)
else:
valid_set.append(to_append)
else:
'''
undersmp_ratio is set as 1.0 in PR-E_complex
0.4 in TacoLM (down-sample negative NoRel instances with a probability of 0.4)
'''
if xy == 3:
if random.uniform(0, 1) <= undersmp_ratio:
test_set.append(to_append)
else:
test_set.append(to_append)
if debugging:
train_set = train_set[0:200]
valid_set = test_set = train_set
elapsed = format_time(time.time() - t0)
print("max context length: ", _max)
print(dataset + " Preprocessing took {:}".format(elapsed))
print(dataset + f' training instance num: {len(train_set)}')
print(dataset + f' validation instance num: {len(valid_set)}')
print(dataset + f' test instance num: {len(test_set)}')
if dataset in ["HiEve", "IC"]:
num_classes = 4
train_dataloader = DataLoader(EventDataset(train_set), batch_size=batch_size, shuffle = shuffle)
valid_dataloader = DataLoader(EventDataset(valid_set), batch_size=batch_size, shuffle = False)
test_dataloader = DataLoader(EventDataset(test_set), batch_size=batch_size, shuffle = False)
return train_dataloader, None, None, valid_dataloader, test_dataloader, num_classes, count_rel