-
Notifications
You must be signed in to change notification settings - Fork 5
/
data_utils.py
309 lines (194 loc) · 10.3 KB
/
data_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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import random
import re
import torch
from vocab import Vocab
TOKEN_PATTERN = re.compile(r"[']*[^'.,?! ]+|[.,?!]")
def tokenize(sent):
if sent == '<SILENCE>':
return [sent]
return re.findall(TOKEN_PATTERN, sent)
def dialog_reader(path):
"""Reads dialogs that are given in Facebook bAbI dialog format."""
dialogs = []
with open(path) as f:
for line in f:
if line in ['\n', '\r\n']:
yield dialogs
dialogs = []
elif '\t' in line:
match = re.search("^\d+ ([^\t]+)\t(.+$)", line)
if not match:
raise ValueError("Invalid dataset format.")
if match[2] == '<SILENCE>':
raise ValueError("Invalid dataset format: Bot never keeps silence.")
dialogs.append((match[1], match[2]))
else:
dialogs.append((line.split(' ', 1)[1][:-1], None))
if len(dialogs) > 0:
yield dialogs
def build_dialog_vocab(dialog_dataset_path, candidates_path, time_features=1000):
"""
Builds two vocabularies. One contains all dialog words along with some special tokens and the second contains
candidate responses, where the word is a whole sentence.
:param dialog_dataset_path: Path to the dialog dataset (must be in Facebook bAbI dialog format)
:param candidates_path: Path to the file containing candidate responses.
:param time_features: Number of time features to add to the dialog vocabulary.
:return: tuple containing dialog and candidate response vocabularies, respectively.
"""
vocab = Vocab()
# PAD token index must be zero so we add it first.
vocab.add_special_token('<pad>')
vocab.add_special_token('<unk>')
vocab.add_special_token('<bot>')
vocab.add_special_token('<user>')
# adding time features into the vocabulary
for i in range(time_features):
vocab.add_special_token('<ts_%d>' % i)
# adding user spoken words to the vocabulary
for dialog in dialog_reader(dialog_dataset_path):
for user_utter, _ in dialog:
for word in tokenize(user_utter):
vocab.add_word(word)
candidate_vocab = Vocab()
with open(candidates_path) as f:
for line in f:
sent = line[2:-1]
candidate_vocab.add_word(sent)
for word in tokenize(sent):
vocab.add_word(word)
vocab.make()
candidate_vocab.make()
return vocab, candidate_vocab
def sent2vec(vocab, sent):
"""Returns vector representation of a sentence by substituting each word with its index from the vocabulary."""
vec = []
for word in tokenize(sent):
idx = vocab.word_to_index(word)
if idx == -1:
idx = vocab.word_to_index('<unk>')
vec.append(idx)
return vec
def vec2sent(vocab, vec):
"""Returns original sentence from its vector representation."""
return ' '.join(vocab.index_to_word(idx) for idx in vec)
class DialogReader:
"""
Represents an iterator over the mini-batches of data samples for training/evaluation.
Single data sample is a triple containing the memory (current dialog history), query (current user utterance)
and the label (ground truth bot response), respectively.
When dealing with mini-batches, data samples are sorted by memory length in advance, so that mini-batches are
approximately same size for computation efficiency.
"""
def __init__(self,
dialog_data_path,
dialog_vocab,
candidate_vocab,
max_memory_size,
batch_size,
drop_last_batch=False,
shuffle_data=False,
eval_mode=False):
"""
:param dialog_data_path: Path to the dialog dataset.
:param dialog_vocab: The dialog vocabulary (word level).
:param candidate_vocab: The vocabulary of candidate responses (sent. level).
:param max_memory_size: The maximum size of the dialog history. If exceeded, the earliest utterances are dropped.
:param batch_size: The size of mini-batch.
:param drop_last_batch: If the number of data samples isn't divisible by batch_size, the last smaller mini-batch is dropped.
:param shuffle_data: Shuffle mini-batches before returning the iterator.
:param eval_mode: If true, every mini-batch has size 1 (regardless batch_size) and comes with an unique dialog id,
so that mini-batches from the same dialog have same ids. Useful when evaluating per dialog accuracy.
"""
self._dialog_data_path = dialog_data_path
self._dialog_vocab = dialog_vocab
self._candidate_vocab = candidate_vocab
self._max_memory_size = max_memory_size
self._batch_size = batch_size if not eval_mode else 1
self._drop_last_batch = drop_last_batch
self._shuffle_data = shuffle_data
# In eval mode batch_size is automatically set to 1, dataset isn't sorted/shuffled, batch comes with dialog id.
self._eval_mode = eval_mode
self._load_data()
if not eval_mode:
self._dataset.sort(key=lambda x: len(x[0]))
self._batches = []
batch = []
for sample in self._dataset:
batch.append(sample)
if len(batch) == self._batch_size:
self._add_batch(batch)
batch = []
if len(batch) > 0 and not self._drop_last_batch:
self._add_batch(batch)
def _add_batch(self, batch):
if self._eval_mode:
self._batches.append((batch[0][0], self._batch_to_tensor([batch[0][1]])))
else:
self._batches.append(self._batch_to_tensor(batch))
def _load_data(self):
# Vectorizing candidate responses.
candidate_vec_max_len = 0
candidate_vecs = []
for i in range(len(self._candidate_vocab)):
sent = self._candidate_vocab.index_to_word(i)
candidate_vec = [self._dialog_vocab.word_to_index(w) for w in tokenize(sent)]
candidate_vec_max_len = max(candidate_vec_max_len, len(candidate_vec))
candidate_vecs.append(candidate_vec)
# Creating tensor of (num_candidates, max_candidate_len) size to store all candidate responses.
self._candidate_vecs = torch.LongTensor(len(candidate_vecs), candidate_vec_max_len).fill_(self._dialog_vocab.word_to_index('<pad>'))
for i in range(len(candidate_vecs)):
self._candidate_vecs[i,:len(candidate_vecs[i])] = torch.LongTensor(candidate_vecs[i])
# Building dialog dataset containing (current_meomry, query, label) triples.
self._dataset = []
for dialog_i, dialog in enumerate(dialog_reader(self._dialog_data_path)):
user_utters, bot_utters = zip(*dialog)
i, tm = 0, 0
memories = []
while i < len(dialog):
if bot_utters[i]:
query = sent2vec(self._dialog_vocab, user_utters[i])
label = self._candidate_vocab.word_to_index(bot_utters[i])
if self._eval_mode:
self._dataset.append((dialog_i, (memories[:], query, label)))
else:
self._dataset.append((memories[:], query, label))
self._write_memory(memories, query, tm, 0)
self._write_memory(memories, sent2vec(self._dialog_vocab, bot_utters[i]), tm + 1, 1)
i, tm = i + 1, tm + 2
# Handling 'displaying options' case.
else:
while not bot_utters[i]:
self._write_memory(memories, sent2vec(self._dialog_vocab, user_utters[i]), tm, 0)
i, tm = i + 1, tm + 1
def _write_memory(self, memories, memory, time, speaker_id):
memory = self._add_speaker_feature(memory, speaker_id)
memory = self._add_time_feature(memory, time)
if len(memories) == self._max_memory_size and self._max_memory_size > 0:
del memories[0]
if self._max_memory_size > 0:
memories.append(memory)
def _add_speaker_feature(self, vec, speaker_id):
return [self._dialog_vocab.word_to_index(['<user>', '<bot>'][speaker_id])] + vec
def _add_time_feature(self, vec, time):
return [self._dialog_vocab.word_to_index('<ts_%d>' % time)] + vec
def _batch_to_tensor(self, batch):
pad = self._dialog_vocab.word_to_index('<pad>')
memories, queries, labels = zip(*batch)
batch_size = len(batch)
max_mem_len = max(1, 1, *[len(m) for m in memories])
max_vec_len = max(1, 1, *[len(v) for m in memories for v in m])
max_query_len = max(len(q) for q in queries)
mem_tensor = torch.LongTensor(batch_size, max_mem_len, max_vec_len).fill_(pad)
query_tensor = torch.LongTensor(batch_size, max_query_len).fill_(pad)
label_tensor = torch.stack([torch.LongTensor([label]) for label in labels])
for i in range(batch_size):
for j in range(len(memories[i])):
mem_tensor[i,j,:len(memories[i][j])] = torch.LongTensor(memories[i][j])
query_tensor[i,:len(queries[i])] = torch.LongTensor(queries[i])
return mem_tensor, query_tensor, label_tensor
def __iter__(self):
if not self._eval_mode and self._shuffle_data:
random.shuffle(self._batches)
return iter(self._batches)
def __len__(self):
return len(self._batches)