-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_data.py
207 lines (163 loc) · 7.35 KB
/
test_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
import os
import json
import random
class HumanJudgementDatasetContest:
DATA_MAP = {
'dailydialog_EVAL': ['transformer_ranker', 'transformer_generator'],
'empatheticdialogues': ['transformer_ranker', 'transformer_generator'],
'convai2':['transformer_ranker', 'transformer_generator',
'bert_ranker', 'dialogGPT'],
}
def __init__(self,
dataset_name, score_whole_dialog=True):
self.dataset_name = dataset_name+'_eval.json'
contest_data_dir = './human_evaluation_data'
self.data_dir_path = os.path.join(contest_data_dir, self.dataset_name)
if dataset_name in ['persona-see', 'fed-dial', 'dstc9']:
#self.data_list = self._load_multi_turn_data()
if score_whole_dialog:
self.data_list = self._raw_multi_turn_data()
else:
self.data_list = self._load_multi_turn_data()
else:
self.data_list = self._load_data()
def _raw_multi_turn_data(self):
'''
Score a whole dialog directly, the difference with load_multi_turn_data is it basically split a dialog with turns in a list.
'''
with open(self.data_dir_path, 'r') as f:
data = json.load(f)
data_list = []
for num, i in enumerate(data):
single_dialog = {}
single_dialog['dialog'] = []
for j in range(len(i['dialog'])):
if j%2 == 0:
single_dialog['dialog'].append('A: '+i['dialog'][j]['text'])
else:
single_dialog['dialog'].append('B: '+i['dialog'][j]['text'])
human_score = {}
for quality_name, score_list in i["annotations"].items():
if len(score_list) != 0:
score = sum(score_list)/len(score_list)
human_score[quality_name] = round(score, 2)
else: human_score[quality_name] = 'NaN'
#human_score[quality_name] = round(score, 2)
single_dialog['human_score'] = human_score
data_list.append(single_dialog)
return data_list
def _load_multi_turn_data(self):
'''
reshape multi turn data to several single turn data to score the whole dialog
also list of dictionaries, in the 'turns' key, are results of spliting.
{'turns':[{'context': , 'hyp_response': }, ... etc], 'human_score': {'quality1': s1, 'quality2': s2, etc}}
'''
with open(self.data_dir_path, 'r') as f:
data = json.load(f)
data_list = []
for num, i in enumerate(data):
single_dialog = {}
single_dialog['turns'] = []
human_score = {}
if i['dialog'][0]['speaker'] == 'model':
for j in range(2, len(i['dialog']), 2):
turn = {}
turn['context'] = [i['dialog'][k]['text'] for k in range(j)]
turn['hyp_response'] = i['dialog'][j]['text']
single_dialog['turns'].append(turn)
else:
for j in range(0, len(i['dialog']), 2):
turn = {}
turn['context'] = [i['dialog'][k]['text'] for k in range(j+1)]
turn['hyp_response'] = i['dialog'][j+1]['text']
single_dialog['turns'].append(turn)
human_score = {}
for quality_name, score_list in i["annotations"].items():
if len(score_list) != 0:
score = sum(score_list)/len(score_list)
human_score[quality_name] = round(score, 2)
else: human_score[quality_name] = 'NaN'
#human_score[quality_name] = round(score, 2)
single_dialog['human_score'] = human_score
data_list.append(single_dialog)
return data_list
def _load_data(self):
'''
single turn data structure is the list of dictionaries
{'context': context of the turn, 'hyp_response': model's response. 'human_score':{'quality1': s1, 'quality2': s2, etc}}
when there is more than one quality, or
{'context': context of the turn, 'hyp_response': model's response. 'human_score': s}
'''
with open(self.data_dir_path, 'r') as f:
data = json.load(f)
data_list = []
if self.dataset_name == 'dstc10-task5.1_eval.json':
for i in data:
single_turn = {}
single_turn['context'] = i['context'].strip().split('\n')
single_turn['hyp_response'] = i['response']
single_turn['human_score'] = round(random.random(), 2)
#single_turn['quality_name'] = qualities
data_list.append(single_turn)
return data_list
if self.dataset_name == 'fed-turn_eval.json':
for i in data:
cont = i['context'].strip().split('\n')
raw_str = []
for k in cont:
without_speaker = k.split(' ')
raw_str.append(' '.join(without_speaker[1:]))
i['context'] = '\n'.join(raw_str)
'''
res = i['response'].strip().split(' ')
i['response'] = ' '.join(res[1:])
'''
for i in data:
single_turn = {}
cont = i['context'].strip().split('\n')
single_turn['context'] = []
for k in range(len(cont)):
if k%2 == 0:
single_turn['context'].append('A: '+cont[k])
else:
single_turn['context'].append('B: '+cont[k])
if len(cont) % 2 == 0:
single_turn['hyp_response'] = 'A: '+i['response']
else:
single_turn['hyp_response'] = 'B: '+i['response']
human_score = {}
for quality_name, score_list in i['annotations'].items():
#qualities.append(quality_name)
score = sum(score_list)/len(score_list)
human_score[quality_name] = round(score, 2)
single_turn['human_score'] = list(human_score.items())[0][1] if len(human_score) == 1 else human_score
#single_turn['quality_name'] = qualities
data_list.append(single_turn)
return data_list
def __iter__(self):
return self.data_list.__iter__()
def __len__(self):
return len(self.data_list)
if __name__ == '__main__':
dataset_name = 'fed-dial'
eval_data = HumanJudgementDatasetContest(dataset_name)
'''
see where is the empty indices.
'''
multi_human_scores = {}
empty_score_indices = []
for i, sample in enumerate(eval_data):
#if(i == 1): break
#print(data['turns'])
#print(data['hyp_response'])
#print(data['human_score'])
for qualitiy, score in sample['human_score'].items():
if qualitiy not in multi_human_scores:
multi_human_scores[qualitiy] = []
if type(score)==float:
multi_human_scores[qualitiy].append(score)
else:
empty_score_indices.append(i)
for i,j in multi_human_scores.items():
print(i,':',len(j))
print(empty_score_indices)