-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataloader.py
208 lines (175 loc) · 6.69 KB
/
dataloader.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
# Copyright 2021 Haoyu Song
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import torch
import json
'''
DATASETS
ConvAI2 PersonaChat:
language: English
format: personal facts
persona type: dense
downloading url: http://parl.ai/downloads/convai2/convai2_fix_723.tgz
data to use: train_self_original_no_cands & valid_self_original_no_cands
ECDT2019 PersonalDialog:
language: Chinese
format: profiles
persona type: sparse
data to use: dialogues_train.json & test_data_random.json
'''
class ConvAI2Dataset(torch.utils.data.Dataset):
def __init__(self, persona, queries, labels, device):
self.persona = persona
self.queries = queries
self.labels = labels
self.device = device
def __getitem__(self, idx):
persona = {
key: torch.tensor(val[idx]).to(self.device)
for key, val in self.persona.items()
}
query = {
key: torch.tensor(val[idx]).to(self.device)
for key, val in self.queries.items()
}
response = {
key: torch.tensor(val[idx]).to(self.device)
for key, val in self.labels.items()
}
return {'persona': persona, 'query': query, 'response': response}
def __len__(self):
return len(self.labels['input_ids'])
class ECDT2019Dataset(torch.utils.data.Dataset):
def __init__(self, profiles, queries, responses, device):
self.profiles = profiles
self.queries = queries
self.responses = responses
self.device = device
def __getitem__(self, idx):
profile = {
key: torch.tensor(val[idx]).to(self.device)
for key, val in self.profiles.items()
}
query = {
key: torch.tensor(val[idx]).to(self.device)
for key, val in self.queries.items()
}
response = {
key: torch.tensor(val[idx]).to(self.device)
for key, val in self.responses.items()
}
return {'persona': profile, 'query': query, 'response': response}
def __len__(self):
return len(self.responses['input_ids'])
class NLIDataset(torch.utils.data.Dataset):
def __init__(self, pre, hyp, device):
self.pre = pre
self.hyp = hyp
self.device = device
def __getitem__(self, idx):
pre = {
key: torch.tensor(val[idx]).to(self.device)
for key, val in self.pre.items()
}
hyp = {
key: torch.tensor(val[idx]).to(self.device)
for key, val in self.hyp.items()
}
return {'pre': pre, 'hyp': hyp}
def __len__(self):
return len(self.pre['input_ids'])
def read_convai2_split(split_dir):
persona = []
query = []
response = []
try:
with open(split_dir, "r", encoding="utf-8") as src:
pre_st, st = 'dia', 'dia'
for line in src:
line = line.strip()
if 'your persona:' in line:
pre_st = st
st = 'per'
else:
pre_st = st
st = 'dia'
if pre_st == 'dia' and st == 'per':
per_group = ''
if st == 'per':
per_group+=(line[16:]+' ')
elif st == 'dia':
persona.append(per_group)
if line[0].isdigit():
line = line[line.find(' '):]
query.append(line.split('\t')[0])
response.append(line.split('\t')[1])
else:
query.append(line.split('\t')[0])
response.append(line.split('\t')[1])
else:
raise (ValueError)
except FileNotFoundError:
print(f"Sorry! The file {split_dir} can't be found.")
return persona, query, response
def read_ecdt2019_split(split_dir, split_type='train'):
profile_lst = []
query_lst = []
response_lst = []
try:
with open(split_dir, "r", encoding="utf-8") as src:
for line in src:
line = line.strip()
data_dict = json.loads(line)
if split_type == 'test':
gr = data_dict['golden_response'][0]
response_lst.append(''.join(gr.split(' ')))
dialog = data_dict['dialog']
uid = data_dict['uid']
profile = data_dict['profile']
q = dialog[-1][0]
pfl = str(profile[uid[-1]])
query_lst.append(''.join(q.split(' ')))
profile_lst.append(pfl)
elif split_type == 'train':
dialog = data_dict['dialog']
uid = data_dict['uid']
profile = data_dict['profile']
q, r = dialog[-2][0], dialog[-1][0]
pfl = str(profile[uid[-1]])
query_lst.append(''.join(q.split(' ')))
response_lst.append(''.join(r.split(' ')))
profile_lst.append(pfl)
else:
print(f'Invalid split_type {split_type}.')
raise(ValueError)
except FileNotFoundError:
print(f"Sorry! The file {split_dir} can't be found.")
return profile_lst, query_lst, response_lst
def read_nli_split(split_dir):
pre_lst = []
hyp_lst = []
try:
with open(split_dir, "r", encoding="utf-8") as src:
for line in src:
line = line.strip()
sent_1, sent_2 = line.split('\t')[0], line.split('\t')[1]
if len(sent_1.split(' ')) > len(sent_2.split(' ')):
pre, hyp = sent_1, sent_2
else:
pre, hyp = sent_2, sent_1
pre_lst.append(pre)
hyp_lst.append(hyp)
except FileNotFoundError:
print(f"Sorry! The file {split_dir} can't be found.")
return pre_lst, hyp_lst