-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.py
271 lines (207 loc) · 10.1 KB
/
utils.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
import os, json, spacy
import numpy as np
from nltk.tokenize import word_tokenize
from sklearn.metrics import f1_score, accuracy_score
from collections import defaultdict
from typing import List, Dict, Callable, Tuple
class ClassicalTokenizer(object):
def __init__(self):
self.glove_dir = "vocab_dir"
if not os.path.exists(self.glove_dir):
os.mkdir(self.glove_dir)
self.build_vocab("ddrel")
with open(self.glove_dir + "/token2id.json", "r") as f:
self.token2id = json.loads(f.read())
with open(self.glove_dir + "/id2token.json", "r") as f:
self.id2token = json.loads(f.read())
def build_vocab(self, data_dir: str):
""" Build vocabulary over the given train, dev and test file.
"""
self.token2id = {"[PAD]": 0, "[SOS]": 1, "[EOS]": 2, "[UNK]": 3}
self.id2token = {0: "[PAD]", 1: "[SOS]", 2: "[EOS]", 3: "[UNK]"}
token_cnt = 4
for file_name in ["train.txt", "dev.txt", "test.txt"]:
with open("{}/{}".format(data_dir, file_name), "r") as f:
for sample in f.readlines():
sample = json.loads(sample)
context = sample["context"]
for sent in context:
for token in word_tokenize(sent):
if not token in self.token2id.keys():
self.token2id[token] = token_cnt
self.id2token[token_cnt] = token
token_cnt += 1
with open("{}/token2id.json".format(self.glove_dir), "w") as f:
json.dump(self.token2id, f)
with open("{}/id2token.json".format(self.glove_dir), "w") as f:
json.dump(self.id2token, f)
print("# of token in the whole dataset: {}".format(token_cnt))
# extract glove embeddings for the dataset
token2embedding = {}
with open("glove.840B.300d.txt", "r") as f:
for line in f.readlines():
line = line.strip().split()
try:
token = line[0]
embedding = [float(v) for v in line[1:]]
token2embedding[token] = embedding
except:
pass
needed_embedding = {
0: list(np.zeros(300)),
1: list(np.random.normal(size=300)),
2: list(np.random.normal(size=300)),
3: list(np.random.normal(size=300)),
}
unk = 0
for token, id in self.token2id.items():
if token in token2embedding.keys():
needed_embedding[id] = token2embedding[token]
else:
unk += 1
with open(self.glove_dir + "/glove.conrel.txt", "w") as f:
for token_id in range(0, token_cnt):
if token_id in needed_embedding.keys():
f.write(" ".join([str(token_id)] + [str(v) for v in needed_embedding[token_id]]) + "\n")
else:
f.write(" ".join([str(token_id)] + ["0." for _ in range(300)]) + "\n")
print("# of unknown word: {}".format(unk))
def encode(self, sent: str) -> List[int]:
"""
Convert all the tokens in this sentence to their id
"""
tokens = word_tokenize(sent)
return [0] + [self.token2id[token] if token in self.token2id.keys() \
else 2 for token in tokens] + [1]
def decode(self, tokens: List[int]) -> str:
return " ".join([self.id2token[str(id)] for id in tokens])
def compute_per_session_score(output_filename: str, pairid: int = None, average_mode: str = None) -> Tuple:
y, y_ = [], []
with open(output_filename, "r") as f:
for sample in f.readlines():
sample = json.loads(sample)
y.append(sample["label"])
y_.append(sample["y_"])
print(output_filename)
print("=" * 30, " per session score ", "=" * 30)
print("label acc: {:.3f}\t f1-macro: {:.3f}".format(
accuracy_score(y, y_),
f1_score(y, y_, average="macro")))
def compute_per_pair_score_mrr(output_filename="experiment_scripts/BERT_baseline.json"):
""" MRR"""
def mmr(logits: List[List[int]]) -> int:
label_cnt = len(logits[0])
session_cnt = len(logits)
label2score = defaultdict(int)
for logit in logits:
logit = sorted([(val, idx) for idx, val in enumerate(logit)])
for rank, (val, idx) in enumerate(logit):
label2score[idx] += 1. / (label_cnt - rank)
for idx in range(label_cnt):
label2score[idx] /= session_cnt
return max(label2score.items(), key=lambda x: x[1])[0]
pair2label = {}
pair2out = defaultdict(list)
with open(output_filename, "r") as f:
for sample in f.readlines():
sample = json.loads(sample)
pair2out[sample["pair-id"]].append(sample["logits"])
pair2label[sample["pair-id"]] = sample["label"]
for pairid in pair2label.keys():
pair2out[pairid] = mmr(pair2out[pairid])
y_ = [val for key, val in pair2out.items()]
y = [val for key, val in pair2label.items()]
assert len(y) == len(y_)
print(output_filename)
print("=" * 30, " per pair score by MRR", "=" * 30)
print("label acc: {:.3f}\tf1-macro: {:.3f}".format(
accuracy_score(y, y_),
f1_score(y, y_, average="macro")
))
def per_pair_score():
compute_per_pair_score_mrr("experiment_scripts/BERT_4_baseline-42.json")
compute_per_pair_score_mrr("experiment_scripts/BERT_4_baseline-52.json")
compute_per_pair_score_mrr("experiment_scripts/BERT_4_baseline-62.json")
print("\n")
compute_per_pair_score_mrr("experiment_scripts/BERT_6_baseline-42.json")
compute_per_pair_score_mrr("experiment_scripts/BERT_6_baseline-52.json")
compute_per_pair_score_mrr("experiment_scripts/BERT_6_baseline-62.json")
print("\n")
compute_per_pair_score_mrr("experiment_scripts/BERT_13_baseline-42.json")
compute_per_pair_score_mrr("experiment_scripts/BERT_13_baseline-52.json")
compute_per_pair_score_mrr("experiment_scripts/BERT_13_baseline-62.json")
print("\n")
# compute_per_pair_score_mrr("experiment_scripts/TextCNN_4-42.json")
# compute_per_pair_score_mrr("experiment_scripts/TextCNN_4-52.json")
# compute_per_pair_score_mrr("experiment_scripts/TextCNN_4-62.json")
# print("\n")
# compute_per_pair_score_mrr("experiment_scripts/TextCNN_6-42.json")
# compute_per_pair_score_mrr("experiment_scripts/TextCNN_6-52.json")
# compute_per_pair_score_mrr("experiment_scripts/TextCNN_6-62.json")
# print("\n")
# compute_per_pair_score_mrr("experiment_scripts/TextCNN_13-42.json")
# compute_per_pair_score_mrr("experiment_scripts/TextCNN_13-52.json")
# compute_per_pair_score_mrr("experiment_scripts/TextCNN_13-62.json")
# print("\n")
# compute_per_pair_score_mrr("experiment_scripts/LSTM_4-42.json")
# compute_per_pair_score_mrr("experiment_scripts/LSTM_4-52.json")
# compute_per_pair_score_mrr("experiment_scripts/LSTM_4-62.json")
# print("\n")
# compute_per_pair_score_mrr("experiment_scripts/LSTM_6-42.json")
# compute_per_pair_score_mrr("experiment_scripts/LSTM_6-52.json")
# compute_per_pair_score_mrr("experiment_scripts/LSTM_6-62.json")
# print("\n")
# compute_per_pair_score_mrr("experiment_scripts/LSTM_13-42.json")
# compute_per_pair_score_mrr("experiment_scripts/LSTM_13-52.json")
# compute_per_pair_score_mrr("experiment_scripts/LSTM_13-62.json")
# print("\n")
def per_session_score():
compute_per_session_score("experiment_scripts/BERT_4_baseline-42.json")
compute_per_session_score("experiment_scripts/BERT_4_baseline-52.json")
compute_per_session_score("experiment_scripts/BERT_4_baseline-62.json")
print("\n")
compute_per_session_score("experiment_scripts/BERT_6_baseline-42.json")
compute_per_session_score("experiment_scripts/BERT_6_baseline-52.json")
compute_per_session_score("experiment_scripts/BERT_6_baseline-62.json")
print("\n")
compute_per_session_score("experiment_scripts/BERT_13_baseline-42.json")
compute_per_session_score("experiment_scripts/BERT_13_baseline-52.json")
compute_per_session_score("experiment_scripts/BERT_13_baseline-62.json")
print("\n")
# compute_per_session_score("experiment_scripts/TextCNN_4-42.json")
# compute_per_session_score("experiment_scripts/TextCNN_4-52.json")
# compute_per_session_score("experiment_scripts/TextCNN_4-62.json")
# print("\n")
# compute_per_session_score("experiment_scripts/TextCNN_6-42.json")
# compute_per_session_score("experiment_scripts/TextCNN_6-52.json")
# compute_per_session_score("experiment_scripts/TextCNN_6-62.json")
# print("\n")
# compute_per_session_score("experiment_scripts/TextCNN_13-42.json")
# compute_per_session_score("experiment_scripts/TextCNN_13-52.json")
# compute_per_session_score("experiment_scripts/TextCNN_13-62.json")
# print("\n")
# compute_per_session_score("experiment_scripts/LSTM_4-42.json")
# compute_per_session_score("experiment_scripts/LSTM_4-52.json")
# compute_per_session_score("experiment_scripts/LSTM_4-62.json")
# print("\n")
# compute_per_session_score("experiment_scripts/LSTM_6-42.json")
# compute_per_session_score("experiment_scripts/LSTM_6-52.json")
# compute_per_session_score("experiment_scripts/LSTM_6-62.json")
# print("\n")
# compute_per_session_score("experiment_scripts/LSTM_13-42.json")
# compute_per_session_score("experiment_scripts/LSTM_13-52.json")
# compute_per_session_score("experiment_scripts/LSTM_13-62.json")
# print("\n")
def test_tokenizer():
t = ClassicalTokenizer()
return t
def show(pairidx):
pair2label = {}
pair2out = defaultdict(list)
with open("ddrel/test.txt", "r") as f:
for sample in f.readlines():
sample = json.loads(sample)
if sample["pair-id"] == str(pairidx): # and sample["y6"] == sample["6-label"]:
for s in sample["context"]:
print(s)
print("\n")