-
Notifications
You must be signed in to change notification settings - Fork 4
/
Evaluate.py
166 lines (149 loc) · 5.33 KB
/
Evaluate.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
import numpy as np
def compute_r_n_m(scores, labels, count, at):
total = 0
correct = 0
for i in range(len(labels)):
if i % 10 == 0:
total = total + 1
sublist = scores[i:i + count]
pos_score = sublist[0]
sublist = sorted(sublist, key=lambda x: x, reverse=True)
if sublist[at - 1] <= pos_score:
correct += 1
return float(correct) / total
def compute_mrr(scores, labels, count=10):
total = 0
accumulate_mrr = 0
for i in range(len(labels)):
if i % 10 == 0:
total = total + 1
sublist = scores[i:i + count]
arg_sort = list(np.argsort(sublist)).index(0)
idx = len(sublist) - arg_sort
accumulate_mrr += 1 / idx
return float(accumulate_mrr) / total
def compute_acc(scores, labels):
scores = (np.asarray(scores) > 0.5).astype(np.int32)
accuracy = sum((scores == labels).astype(np.int32)) / len(labels)
return accuracy
def evaluate_all(scores, labels):
return compute_acc(scores, labels), compute_r_n_m(scores, labels, 2, 1), compute_r_n_m(scores, labels, 10, 1), compute_r_n_m(scores, labels, 10, 2), \
compute_r_n_m(scores, labels, 10, 5), compute_mrr(scores, labels)
def evaluate_all_from_file(path):
scores = []
labels = []
with open(path, "r") as f:
for line in f:
score, label = line.strip().split("\t")
scores.append(float(score))
labels.append(float(labels))
evaluate_all(scores, labels)
def recover_and_show(basic_directory):
import pickle
vocab = {}
vocab_id2word = {}
with open("./data/vocab.txt", "r", encoding="utf-8") as fr:
for idx, line in enumerate(fr):
line = line.strip().split("\t")
vocab[line[0]] = idx + 1
vocab_id2word[idx + 1] = line[0]
vocab["_PAD_"] = 0
vocab_id2word[0] = "_PAD_"
def initialize():
all_outline = []
outline_dict = {}
initial_file = basic_directory + "test.multi.0.pkl"
with open(initial_file, 'rb') as f:
_, _, outline, _ = pickle.load(f)
for o in outline:
if o not in all_outline:
all_outline.append(o)
sent_o = "".join([vocab_id2word[x] for x in o])
outline_dict[sent_o] = []
return all_outline, outline_dict
max_turn = 10
all_outline, outline_dict = initialize()
for turn in range(0, max_turn + 1):
score = []
with open(basic_directory + "test.result.multi." + str(turn) + ".txt", "r") as fr:
for idx, line in enumerate(fr):
if idx == 0:
continue
score.append(float(line.strip()))
with open(basic_directory + "test.multi." + str(turn) + ".pkl", "rb") as fr:
utterance, response, outline, labels = pickle.load(fr) # except for dl2r
for i, o in enumerate(outline):
if i % 10 == 0:
score_sub_list = score[i:i + 10]
response_sub_list = response[i:i + 10]
max_idx = score_sub_list.index(max(score_sub_list))
selected_response = response_sub_list[max_idx]
sent_o = "".join([vocab_id2word[x] for x in o])
outline_dict[sent_o] = utterance[i] + [selected_response] # for MUSwO
with open(basic_directory + "test.result.multi.txt", "w", encoding="utf-8") as fw:
for o in all_outline:
sent_o = "".join([vocab_id2word[x] for x in o])
utterance = outline_dict[sent_o]
fw.write("outline\t" + sent_o + "\n")
for u in utterance:
sent_u = "".join([vocab_id2word[x] for x in u])
fw.write("script\t" + sent_u + "\n")
fw.write("\n")
def evaluate_multi_turn_result(t_file, g_file):
test_file = t_file
gold_file = g_file
ft = open(test_file, "r", encoding="utf-8")
fg = open(gold_file, "r", encoding="utf-8")
t, g = ft.readline(), fg.readline()
c = -1
s = -1
all_s = 0
r_all = 0
tmp_r_all = 0
o = None
flag = 1
t_s = []
g_s = []
while t and g:
t = t.strip().split("\t")
g = g.strip().split("\t")
if len(t) > 1:
if t[0] == "script":
if t[1] == g[1]:
s += 1
c += 1
t_s.append(t[1])
g_s.append(g[1])
if t[0] == "outline":
o = t[1]
else:
tmp_c = -1
tmp_s = -1
for at in g_s:
tmp_c += 1
if at in t_s:
tmp_s += 1
tmp_r = tmp_s / tmp_c
tmp_r_all += tmp_r
r = s / c
r_all += r
all_s += 1
c = -1
s = -1
t_s = []
g_s = []
t, g = ft.readline(), fg.readline()
tmp_c = -1
tmp_s = -1
for at in g_s:
tmp_c += 1
if at in t_s:
tmp_s += 1
tmp_r = tmp_s / tmp_c
tmp_r_all += tmp_r
r = s / c
r_all += r
all_s += 1
r_all += r
print("p_strict", r_all / all_s)
print("p_weak", tmp_r_all / all_s)