-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_utils.py
436 lines (330 loc) · 12.5 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
from collections import defaultdict
import copy
from glob import glob
import os
import pickle as pkl
import xml.etree.ElementTree
import numpy as np
import torch
from vocab import Vocab, VocabEntry
np.set_printoptions(precision=3)
use_cuda = torch.cuda.is_available()
def to_tensor(doc):
X, R, E, L = doc[0]
tX = []
tR = []
tE = []
tL = []
for sent_idx in range(len(X)):
tx = torch.from_numpy(np.array(X[sent_idx]))
tr = torch.from_numpy(np.array(R[sent_idx]))
te = torch.from_numpy(np.array(E[sent_idx]))
tl = torch.from_numpy(np.array(L[sent_idx]))
if use_cuda:
tx = tx.cuda()
tr = tr.cuda()
te = te.cuda()
tl = tl.cuda()
tX.append(tx)
tR.append(tr)
tE.append(te)
tL.append(tl)
return [(tX, tR, tE, tL)]
"""
DataLoader for InScript corpus
"""
class InScriptDataLoader(object):
def __init__(self, xml_dir, dict_pickle):
self.name = os.path.basename(xml_dir)
self.use_cuda = use_cuda
# Use Predefined Dictionary
with open(dict_pickle,'rb') as fin:
self.dictionary = pkl.load(fin)
self.documents = []
for xml_file in sorted(glob(os.path.join(xml_dir,"*.xml"))):
doc_name = os.path.basename(xml_file)
doc = self.parse_document(xml_file, self.dictionary)
tensor_doc = to_tensor(doc)
self.documents.append((doc_name,tensor_doc))
def parse_document(self, xml_file, dictionary, debug=False):
root = xml.etree.ElementTree.parse(xml_file).getroot()
content = root[0][0].text
sentences = content.split('\n')
R = []
E = []
L = []
participants = root[1][0]
entity_table = {}
for sent in sentences:
R.append([0] * len(sent.split()))
E.append([0] * len(sent.split()))
L.append([1] * len(sent.split()))
for label in participants:
sentence_id, word_id = map(int, label.attrib['from'].split('-'))
entity = label.attrib["name"]
text = label.attrib["text"]
tokens = text.split()
start = word_id-1
end = start+1
if 'to' in label.attrib:
_, end = map(int,label.attrib['to'].split('-'))
if any(x == 0 for x in E[sentence_id-1][start:end]):
if entity not in entity_table:
entity_table[entity] = len(entity_table)+1
entity_id = entity_table[entity]
# R : is entity?
# E : entity index
for idx in range(start,end,1):
E[sentence_id-1][idx] = entity_id
R[sentence_id-1][word_id-1] = 1
# L : entity remaining length
for l in range(len(tokens),0,-1):
idx = word_id-1 + len(tokens)-l
L[sentence_id-1][idx] = l
X = []
for sent in sentences:
x = []
for word in sent.split():
xidx = self.dictionary.get(word,0)
x.append(xidx)
X.append(x)
doc = []
doc.append((X,R,E,L))
return doc
class LetsGoCorpus(object):
def __init__(self, data_path):
train, valid, test = pkl.load(open(data_path, "rb"), encoding='bytes')
self.train = self.load_data(train)
self.valid = self.load_data(valid)
self.test = self.load_data(test)
print("Loaded %d train %d valid and %d test" % (len(self.train), len(self.valid), len(self.test)))
def load_data(self, data):
ret_dial = []
for dial in data:
ret_turn = []
for turn in dial:
sys = turn[0]
usr = turn[1]
try:
sys = sys.decode()
except:
pass
try:
usr = usr.decode()
except:
pass
ret_turn.append((sys, usr, turn[2], turn[3]))
ret_dial.append(ret_turn)
return ret_dial
def get_train_sents(self):
sys_sents = []
usr_sents = []
for dial in self.train:
for turn in dial:
sys_sents.append(turn[0])
usr_sents.append(turn[1])
return sys_sents + usr_sents, sys_sents
def read_corpus_vocab(corp, source):
data = []
for line in corp:
sent = line.strip().split(' ')
# only append <s> and </s> to the target sentence
if source == 'tgt':
sent = ['<s>'] + sent + ['</s>']
data.append(sent)
return data
def data_iter(data, batch_size, shuffle=True):
"""
randomly permute data, then sort by source length, and partition into batches
ensure that the length of source sentences in each batch is decreasing
"""
buckets = defaultdict(list)
for pair in data:
src_sent = pair[0]
buckets[len(src_sent)].append(pair)
batched_data = []
for src_len in buckets:
tuples = buckets[src_len]
if shuffle: np.random.shuffle(tuples)
batched_data.extend(list(batch_slice(tuples, batch_size)))
if shuffle:
np.random.shuffle(batched_data)
for batch in batched_data:
yield batch
def batch_slice(data, batch_size, sort=True):
batch_num = int(np.ceil(len(data) / float(batch_size)))
for i in range(batch_num):
cur_batch_size = batch_size if i < batch_num - 1 else len(data) - batch_size * i
src_sents = [data[i * batch_size + b][0] for b in range(cur_batch_size)]
tgt_sents = [data[i * batch_size + b][1] for b in range(cur_batch_size)]
if sort:
src_ids = sorted(range(cur_batch_size), key=lambda src_id: len(src_sents[src_id]), reverse=True)
src_sents = [src_sents[src_id] for src_id in src_ids]
tgt_sents = [tgt_sents[src_id] for src_id in src_ids]
yield src_sents, tgt_sents
"""
Data Loader for Lets Go Corpus
For EntityNLM,
I: Whole Dialogue as document => 4k
II: Previous context and target sentence => 55k
"""
class LetsGoDataLoader():
def __init__(self, data, vocab=None, build_documents=False):
self.data = data
self.vocab = vocab
# For encoder decoder
self.src = []
self.tgt = []
self.max_len = 30
self.process()
# For Entity NLM
self.documents = []
self.entities = []
if build_documents:
self.build_documents()
def process(self):
for dial in self.data:
for i, turn in enumerate(dial):
if i == 0: continue
src_ctx = []
entity_dict = {}
X_s = []
R_s = []
E_s = []
L_s = []
last_tgt = ['<s>'] + turn[0].strip().split(' ') + ['</s>']
for prev in dial[:i]:
sys = prev[0].strip().split(' ')
usr = prev[1].strip().split(' ')
sys = ['<s>'] + sys + ['</s>']
usr = ['<s>'] + usr + ['</s>']
sysR = self.get_R(sys)
usrR = self.get_R(usr)
sysL = self.get_L(sys)
usrL = self.get_L(usr)
sysE = self.get_E(sys, entity_dict)
usrE = self.get_E(usr, entity_dict)
sys = [ self.vocab[w] for w in sys ]
usr = [ self.vocab[w] for w in usr ]
X_s.append(sys)
X_s.append(usr)
R_s.append(sysR)
R_s.append(usrR)
E_s.append(sysE)
E_s.append(usrE)
L_s.append(sysL)
L_s.append(usrL)
assert len(X_s) % 2 == 0
doc = [ (X_s, R_s, E_s, L_s) ]
tensor_doc = to_tensor(doc)
self.src.append(tensor_doc)
self.tgt.append(last_tgt)
def build_documents(self):
for dial_idx, dial in enumerate(self.data,1):
# One Document
entity_dict = {}
X_s = []
R_s = []
E_s = []
L_s = []
for i, turn in enumerate(dial):
sys = turn[0].strip().split(' ')
usr = turn[1].strip().split(' ')
sys = ['<s>'] + sys + ['</s>']
usr = ['<s>'] + usr + ['</s>']
sysR = self.get_R(sys)
usrR = self.get_R(usr)
sysL = self.get_L(sys)
usrL = self.get_L(usr)
sysE = self.get_E(sys, entity_dict)
usrE = self.get_E(usr, entity_dict)
sys = [ self.vocab[w] for w in sys ]
usr = [ self.vocab[w] for w in usr ]
X_s.append(sys)
X_s.append(usr)
R_s.append(sysR)
R_s.append(usrR)
E_s.append(sysE)
E_s.append(usrE)
L_s.append(sysL)
L_s.append(usrL)
assert len(X_s) % 2 == 0
doc = [ (X_s, R_s, E_s, L_s) ]
# Convert to Tensor
tensor_doc = to_tensor(doc)
# Appent to self
self.documents.append((str(dial_idx), tensor_doc))
self.entities.append(entity_dict)
def get_src(self):
return self.src
def get_tgt(self):
return self.tgt
def get_R(self, sent):
ret = []
for word in sent:
if word.startswith('<') and word not in ['<s>','</s>']:
ret.append(1)
else:
ret.append(0)
return ret
def get_E(self, sent, entity_dict):
ret = []
for word in sent:
# Check if is entity
if word.startswith('<') and word not in ['<s>','</s>']:
if word not in entity_dict:
entity_dict[ word ] = len(entity_dict)+1
entity_idx = entity_dict.get(word)
else:
entity_idx = 0
ret.append(entity_idx)
return ret
def get_L(self, sent):
ret = [1 for _ in sent]
return ret
def to_tensor(self, doc):
X, R, E, L = doc[0]
tX = []
tR = []
tE = []
tL = []
for sent_idx in range(len(X)):
tx = torch.from_numpy(np.array(X[sent_idx]))
tr = torch.from_numpy(np.array(R[sent_idx]))
te = torch.from_numpy(np.array(E[sent_idx]))
tl = torch.from_numpy(np.array(L[sent_idx]))
if use_cuda:
tx = tx.cuda()
tr = tr.cuda()
te = te.cuda()
tl = tl.cuda()
tX.append(tx)
tR.append(tr)
tE.append(te)
tL.append(tl)
assert len(tX) % 2 == 0
return [(tX, tR, tE, tL)]
def load_corpus(args):
dataset_name = args.dataset
print("Loading dataset",dataset_name)
if dataset_name == "inscript":
data_dir = './data/modi'
dict_pickle = os.path.join(data_dir,'train','dict.pickle')
train_corpus = InScriptDataLoader(os.path.join(data_dir,'train'), dict_pickle)
valid_corpus = InScriptDataLoader(os.path.join(data_dir,'valid'), dict_pickle)
test_corpus = InScriptDataLoader(os.path.join(data_dir,'test'), dict_pickle)
dictionary = train_corpus.dictionary
elif dataset_name == "letsgo":
vocab = torch.load('./data/vocab.bin')
corpus = LetsGoCorpus('./data/union_data-1ab.p')
train_corpus = LetsGoDataLoader(corpus.train, vocab.src, build_documents=True)
valid_corpus = LetsGoDataLoader(corpus.valid, vocab.src, build_documents=True)
test_corpus = LetsGoDataLoader(corpus.test, vocab.src, build_documents=True)
dictionary = train_corpus.vocab.word2id
else:
raise ValueError("Invalid dataset:",dataset_name)
return train_corpus, valid_corpus, test_corpus, dictionary
if __name__ == "__main__":
vocab = torch.load('./data/vocab.bin')
corpus = LetsGoCorpus('./data/union_data-1ab.p')
test_loader = LetsGoDataLoader(corpus.test, vocab.src)