-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutil.py
195 lines (141 loc) · 5.53 KB
/
util.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
import pickle
import random
import copy
from pytorch_pretrained_bert import BertTokenizer
def all_events(datasets, positive=True, single_event=True):
event_set = set()
for data in datasets:
_, token, _, e1, e2, _ = data
e1 = [token[x] for x in e1]
e2 = [token[x] for x in e2]
if not positive or data[-1] != 'NULL':
if single_event:
event_set.add(tuple(e1))
event_set.add(tuple(e2))
else:
event_set.add((tuple(e1), tuple(e2)))
return event_set
def get_topic(dataset, topics):
results = list()
for data in dataset:
t = data[0]
if t.split('/')[-2] in topics:
results.append(data)
return results
def topic_c(dataset, topic1, topic2):
data_topic1 = get_topic(dataset, topic1)
data_topic2 = get_topic(dataset, topic2)
events1 = all_events(data_topic1, positive=True, single_event=True)
events2 = all_events(data_topic2, positive=True, single_event=True)
inter_set = events1.intersection(events2)
union_set = events1.union(events2)
return len(inter_set) / len(union_set)
def select_datasets(datasets, seen_events, flag):
seen = list()
unseen = list()
if flag == 'seen':
for data in datasets:
_, token, _, e1, e2, _ = data
e1 = [token[x] for x in e1]
e2 = [token[x] for x in e2]
if tuple(e1) in seen_events and tuple(e2) in seen_events:
seen.append(data)
else:
unseen.append(data)
if flag == 'and':
for data in datasets:
_, token, _, e1, e2, _ = data
e1 = [token[x] for x in e1]
e2 = [token[x] for x in e2]
if tuple(e1) in seen_events and tuple(e2) in seen_events:
seen.append(data)
else:
unseen.append(data)
elif flag == 'or':
for data in datasets:
_, token, _, e1, e2, _ = data
e1 = [token[x] for x in e1]
e2 = [token[x] for x in e2]
if (tuple(e1) in seen_events and not tuple(e2) in seen_events) or (tuple(e2) in seen_events and not tuple(e1) in seen_events):
seen.append(data)
else:
unseen.append(data)
return seen, unseen
def split_datasets(data, flag):
l_data = len(data)
training_data = data[:int(l_data/2)]
testing_data = data[int(l_data/2):]
if flag == 'seen' or flag == 'or':
training_events = all_events(training_data, positive=True, single_event=True)
seen, unseen = select_datasets(testing_data, training_events, flag)
training_data = training_data
testing_data = seen
else:
training_events = all_events(training_data, positive=True)
seen, unseen = select_datasets(testing_data, training_events, flag)
training_data = training_data
testing_data = unseen
training_data_mask = copy.deepcopy(training_data)
testing_data_mask = copy.deepcopy(testing_data)
for _, sen1, sen2, span1, span2, _ in training_data_mask:
for i in span1 + span2:
sen1[i] = 103
for _, sen1, spen2, span1, span2, _ in testing_data_mask:
for i in span1 + span2:
sen1[i] = 103
return training_data, testing_data, training_data_mask, testing_data_mask
if __name__ == '__main__':
with open('data.pickle', 'rb') as f:
data = pickle.load(f)
data_topic1 = get_topic(data, ['18'])
data_topic2 = get_topic(data, ['33'])
events1 = all_events(data_topic1, positive=True, single_event=True)
events2 = all_events(data_topic2, positive=True, single_event=True)
inter_set = events1.intersection(events2)
model_dir = '/home/jliu/data/BertModel/bert-large-uncased' # uncased better
tokenizer = BertTokenizer.from_pretrained(model_dir, do_lower_case=True)
for elem in inter_set:
print(tokenizer.convert_ids_to_tokens(list(elem)))
print('--')
for elem in events1:
if elem not in inter_set:
print(tokenizer.convert_ids_to_tokens(list(elem)))
print('--')
for elem in events2:
if elem not in inter_set:
print(tokenizer.convert_ids_to_tokens(list(elem)))
with open('data/event_causality.pickle', 'rb') as f:
data = pickle.load(f)
print(len(data))
# 3464, 4381, 1891
# print(data[0])
# random.seed(1234)
# random.shuffle(data)
# training_data, testing_data, training_data_mask, testing_data_mask = split_datasets(data, 'or')
# print(len(training_data))
# print(len(testing_data))
# data = {
# 'train': training_data,
# 'test': testing_data
# }
# with open('data_one.pickle', 'wb') as f:
# pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
# data = {
# 'train': training_data_mask,
# 'test': testing_data_mask
# }
# with open('data_one_mask.pickle', 'wb') as f:
# pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
# all_topics = ['1', '3', '4', '5', '7', '8',
# '12', '13', '14', '16', '18', '19', '20',
# '22', '23', '24', '30', '32', '33', '35', '37', '41']
# res = []
# for i in all_topics:
# for j in all_topics:
# if int(j) != int(i):
# res.append([i, j, topic_c(data, [i], [j])])
# res = sorted(res, key=lambda x: x[-1], reverse=True)
# for st in all_topics:
# filt = list(filter(lambda x: x[0]==st, res))
# print(filt)
# print()