forked from WING-NUS/sequicity
-
Notifications
You must be signed in to change notification settings - Fork 15
/
reader.py
executable file
·1027 lines (924 loc) · 40 KB
/
reader.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import json
import pickle
from config import global_config as cfg
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
import logging
import random
import os
import re
import csv
import time, datetime
import pdb
def clean_replace(s, r, t, forward=True, backward=False):
def clean_replace_single(s, r, t, forward, backward, sidx=0):
idx = s[sidx:].find(r)
if idx == -1:
return s, -1
idx += sidx
idx_r = idx + len(r)
if backward:
while idx > 0 and s[idx - 1]:
idx -= 1
elif idx > 0 and s[idx - 1] != ' ':
return s, -1
if forward:
while idx_r < len(s) and (s[idx_r].isalpha() or s[idx_r].isdigit()):
idx_r += 1
elif idx_r != len(s) and (s[idx_r].isalpha() or s[idx_r].isdigit()):
return s, -1
return s[:idx] + t + s[idx_r:], idx_r
sidx = 0
while sidx != -1:
s, sidx = clean_replace_single(s, r, t, forward, backward, sidx)
return s
class _ReaderBase:
class LabelSet:
def __init__(self):
self._idx2item = {}
self._item2idx = {}
self._freq_dict = {}
def __len__(self):
return len(self._idx2item)
def _absolute_add_item(self, item):
idx = len(self)
self._idx2item[idx] = item
self._item2idx[item] = idx
def add_item(self, item):
if item not in self._freq_dict:
self._freq_dict[item] = 0
self._freq_dict[item] += 1
def construct(self, limit):
l = sorted(self._freq_dict.keys(), key=lambda x: -self._freq_dict[x])
print('Actual label size %d' % (len(l) + len(self._idx2item)))
if len(l) + len(self._idx2item) < limit:
logging.warning('actual label set smaller than that configured: {}/{}'
.format(len(l) + len(self._idx2item), limit))
for item in l:
if item not in self._item2idx:
idx = len(self._idx2item)
self._idx2item[idx] = item
self._item2idx[item] = idx
if len(self._idx2item) >= limit:
break
def encode(self, item):
return self._item2idx[item]
def decode(self, idx):
return self._idx2item[idx]
class Vocab(LabelSet):
def __init__(self, init=True):
_ReaderBase.LabelSet.__init__(self)
if init:
self._absolute_add_item('<pad>') # 0
self._absolute_add_item('<go>') # 1
self._absolute_add_item('<unk>') # 2
self._absolute_add_item('<go2>') # 3
def load_vocab(self, vocab_path):
f = open(vocab_path, 'rb')
dic = pickle.load(f)
self._idx2item = dic['idx2item']
self._item2idx = dic['item2idx']
self._freq_dict = dic['freq_dict']
f.close()
def enlarge_vocab(self, vocab_path):
# self.construct(vocab_size)
f = open(vocab_path, 'rb')
dic = pickle.load(f)
# self._idx2item = dic['idx2item']
# self._item2idx = dic['item2idx']
# self._freq_dict = dic['freq_dict']
# #######################
# pdb.set_trace()
# #######################
self._idx2item.update(dic['idx2item'])
self._item2idx.update(dic['item2idx'])
self._freq_dict.update(dic['freq_dict'])
f.close()
# #######################
# pdb.set_trace()
# #######################
def save_vocab(self, vocab_path):
f = open(vocab_path, 'wb')
dic = {
'idx2item': self._idx2item,
'item2idx': self._item2idx,
'freq_dict': self._freq_dict
}
pickle.dump(dic, f)
f.close()
def sentence_encode(self, word_list):
return [self.encode(_) for _ in word_list]
def sentence_decode(self, index_list, eos=None):
l = [self.decode(_) for _ in index_list]
if not eos or eos not in l:
return ' '.join(l)
else:
idx = l.index(eos)
return ' '.join(l[:idx])
def nl_decode(self, l, eos=None):
return [self.sentence_decode(_, eos) + '\n' for _ in l]
def encode(self, item):
if item in self._item2idx:
return self._item2idx[item]
else:
return self._item2idx['<unk>']
def decode(self, idx):
if idx < len(self):
return self._idx2item[idx]
else:
return 'ITEM_%d' % (idx - cfg.vocab_size)
def __init__(self):
self.train, self.dev, self.test = [], [], []
self.vocab = self.Vocab()
self.result_file = ''
def _construct(self, *args):
"""
load data, construct vocab and store them in self.train/dev/test
:param args:
:return:
"""
raise NotImplementedError('This is an abstract class, bro')
def _bucket_by_turn(self, encoded_data):
turn_bucket = {}
for dial in encoded_data:
turn_len = len(dial)
if turn_len not in turn_bucket:
turn_bucket[turn_len] = []
turn_bucket[turn_len].append(dial)
del_l = []
for k in turn_bucket:
if k >= 5: del_l.append(k)
# logging.debug("bucket %d instance %d" % (k, len(turn_bucket[k])))
# for k in del_l:
# turn_bucket.pop(k)
return turn_bucket
def _mark_batch_as_supervised(self, all_batches):
supervised_num = int(len(all_batches) * cfg.spv_proportion / 100)
for i, batch in enumerate(all_batches):
for dial in batch:
for turn in dial:
turn['supervised'] = i < supervised_num
if not turn['supervised']:
turn['degree'] = [0.] * cfg.degree_size # unsupervised learning. DB degree should be unknown
return all_batches
def _construct_mini_batch(self, data):
all_batches = []
batch = []
for dial in data:
batch.append(dial)
if len(batch) == cfg.batch_size:
all_batches.append(batch)
batch = []
# if remainder > 1/2 batch_size, just put them in the previous batch, otherwise form a new batch
if len(batch) > 0.5 * cfg.batch_size:
all_batches.append(batch)
elif len(all_batches):
all_batches[-1].extend(batch)
else:
all_batches.append(batch)
return all_batches
def _transpose_batch(self, batch):
dial_batch = []
turn_num = len(batch[0])
for turn in range(turn_num):
turn_l = {}
for dial in batch:
this_turn = dial[turn]
for k in this_turn:
if k not in turn_l:
turn_l[k] = []
turn_l[k].append(this_turn[k])
dial_batch.append(turn_l)
return dial_batch
def _construct_mini_batch_turn(self, turns):
all_batches = []
batch = []
for turn in turns:
batch.append(turn)
if len(batch) == cfg.batch_size:
all_batches.append(batch)
batch = []
# if size of remainder < 1/2 batch_size,
# just put them in the previous batch,
# otherwise form a new batch
if len(batch) > 0.5 * cfg.batch_size:
all_batches.append(batch)
elif len(all_batches) != 0:
all_batches[-1].extend(batch)
return all_batches
def _transpose_batch_turn(self, batch):
turn_trans = {}
for turn in batch:
for key in turn.keys():
if key not in turn_trans:
turn_trans[key] = []
turn_trans[key].append(turn[key])
return turn_trans
def mini_batch_iterator(self, set_name):
name_to_set = {'train': self.train, 'test': self.test, 'dev': self.dev}
dial = name_to_set[set_name]
turn_bucket = self._bucket_by_turn(dial)
# self._shuffle_turn_bucket(turn_bucket)
all_batches = []
for k in turn_bucket:
batches = self._construct_mini_batch(turn_bucket[k])
all_batches += batches
self._mark_batch_as_supervised(all_batches)
random.shuffle(all_batches)
for i, batch in enumerate(all_batches):
yield self._transpose_batch(batch)
def mini_batch_iterator_maml(self, set_name):
name_to_set = {'train': self.train, 'test': self.test, 'dev': self.dev}
dial = name_to_set[set_name]
all_domain_batches = []
for domain in range(len(dial)):
turn_bucket = self._bucket_by_turn(dial[domain])
all_batches = []
for k in turn_bucket:
batches = self._construct_mini_batch(turn_bucket[k])
all_batches += batches
# ##########################
# pdb.set_trace()
# ###########################
self._mark_batch_as_supervised(all_batches)
random.shuffle(all_batches)
all_domain_batches.append([self._transpose_batch(batch) for batch in all_batches])
# for i, batch in enumerate(all_batches):
# yield self._transpose_batch(batch)
return all_domain_batches
def mini_batch_iterator_maml_supervised(self, set_name):
name_to_set = {'train': self.train, 'test': self.test, 'dev': self.dev}
total_dial_domain = name_to_set[set_name]
if set_name == 'train':
min_total_turns = min([sum([len(dialog) for dialog in all_dialog]) for all_dialog in total_dial_domain])
all_domain_batches = []
for domain in range(len(total_dial_domain)):
random.shuffle(total_dial_domain[domain])
turns = []
for dialog in total_dial_domain[domain]:
if len(turns) < min_total_turns:
turns += dialog
else:
break
turns = turns[:min_total_turns]
batches = self._construct_mini_batch_turn(turns)
all_domain_batches.append([self._transpose_batch_turn(batch) for batch in batches])
output = [[all_domain_batches[domain][turn] for domain in range(len(total_dial_domain))]
for turn in range(len(all_domain_batches[0]))]
# ##########################
# pdb.set_trace()
# ###########################
for turn_num, turn_batch_domain in enumerate(output):
yield turn_batch_domain
# return output
else:
dials = []
for domain in range(len(total_dial_domain)):
dials += total_dial_domain[domain]
turn_bucket = self._bucket_by_turn(dials)
# self._shuffle_turn_bucket(turn_bucket)
all_batches = []
for k in turn_bucket:
batches = self._construct_mini_batch(turn_bucket[k])
all_batches += batches
self._mark_batch_as_supervised(all_batches)
random.shuffle(all_batches)
for i, batch in enumerate(all_batches):
yield self._transpose_batch(batch)
def wrap_result(self, turn_batch, gen_m, gen_z, eos_syntax=None, prev_z=None):
"""
wrap generated results
:param gen_z:
:param gen_m:
:param turn_batch: dict of [i_1,i_2,...,i_b] with keys
:return:
"""
results = []
if eos_syntax is None:
eos_syntax = {'response': 'EOS_M', 'user': 'EOS_U', 'bspan': 'EOS_Z2'}
batch_size = len(turn_batch['user'])
for i in range(batch_size):
entry = {}
if prev_z is not None:
src = prev_z[i] + turn_batch['user'][i]
else:
src = turn_batch['user'][i]
for key in turn_batch:
entry[key] = turn_batch[key][i]
if key in eos_syntax:
entry[key] = self.vocab.sentence_decode(entry[key], eos=eos_syntax[key])
if gen_m:
entry['generated_response'] = self.vocab.sentence_decode(gen_m[i], eos='EOS_M')
else:
entry['generated_response'] = ''
if gen_z:
entry['generated_bspan'] = self.vocab.sentence_decode(gen_z[i], eos='EOS_Z2')
else:
entry['generated_bspan'] = ''
results.append(entry)
write_header = False
if not self.result_file:
self.result_file = open(cfg.result_path, 'w')
self.result_file.write(str(cfg))
write_header = True
field = ['dial_id', 'turn_num', 'user', 'generated_bspan', 'bspan', 'generated_response', 'response', 'u_len',
'm_len', 'supervised']
for result in results:
del_k = []
for k in result:
if k not in field:
del_k.append(k)
for k in del_k:
result.pop(k)
writer = csv.DictWriter(self.result_file, fieldnames=field)
if write_header:
self.result_file.write('START_CSV_SECTION\n')
writer.writeheader()
################################
pdb.set_trace()
##################################
writer.writerows(results)
return results
def db_search(self, constraints):
raise NotImplementedError('This is an abstract method')
def db_degree_handler(self, z_samples, *args, **kwargs):
"""
returns degree of database searching and it may be used to control further decoding.
One hot vector, indicating the number of entries found: [0, 1, 2, 3, 4, >=5]
:param z_samples: nested list of B * [T]
:return: an one-hot control *numpy* control vector
"""
control_vec = []
for cons_idx_list in z_samples:
constraints = set()
for cons in cons_idx_list:
if type(cons) is not str:
cons = self.vocab.decode(cons)
if cons == 'EOS_Z1':
break
constraints.add(cons)
match_result = self.db_search(constraints)
degree = len(match_result)
# modified
# degree = 0
control_vec.append(self._degree_vec_mapping(degree))
return np.array(control_vec)
def _degree_vec_mapping(self, match_num):
l = [0.] * cfg.degree_size
l[min(cfg.degree_size - 1, match_num)] = 1.
return l
class CamRest676Reader(_ReaderBase):
def __init__(self):
super().__init__()
self.db = []
if type(cfg.data) is list:
self._construct_maml(cfg.data, cfg.db)
else:
self._construct(cfg.data, cfg.db)
self.result_file = ''
def _get_tokenized_data(self, raw_data, db_data, construct_vocab):
tokenized_data = []
vk_map = self._value_key_map(db_data)
for dial_id, dial in enumerate(raw_data):
tokenized_dial = []
for turn in dial['dial']:
turn_num = turn['turn']
constraint = []
requested = []
for slot in turn['usr']['slu']:
if slot['act'] == 'inform':
s = slot['slots'][0][1]
if s not in ['dontcare', 'none']:
constraint.extend(word_tokenize(s))
else:
requested.extend(word_tokenize(slot['slots'][0][1]))
degree = len(self.db_search(constraint, db_data))
requested = sorted(requested)
constraint.append('EOS_Z1')
requested.append('EOS_Z2')
user = word_tokenize(turn['usr']['transcript']) + ['EOS_U']
response = word_tokenize(self._replace_entity(turn['sys']['sent'], vk_map, constraint)) + ['EOS_M']
# ################################
# pdb.set_trace()
# ##################################
tokenized_dial.append({
'dial_id': dial_id,
'turn_num': turn_num,
'user': user,
'response': response,
'constraint': constraint,
'requested': requested,
'degree': degree,
})
if construct_vocab:
for word in user + response + constraint + requested:
self.vocab.add_item(word)
tokenized_data.append(tokenized_dial)
return tokenized_data
def _replace_entity(self, response, vk_map, constraint):
response = re.sub('[cC][., ]*[bB][., ]*\d[., ]*\d[., ]*\w[., ]*\w', 'postcode_SLOT', response)
response = re.sub('\d{5}\s?\d{6}', 'phone_SLOT', response)
constraint_str = ' '.join(constraint)
# ################################
# pdb.set_trace()
# ##################################
for v, k in sorted(vk_map.items(), key=lambda x: -len(x[0])):
# ################################
# pdb.set_trace()
# ##################################
start_idx = response.find(v)
if start_idx == -1 \
or (start_idx != 0 and response[start_idx - 1] != ' ') \
or (v in constraint_str):
continue
if k not in ['name', 'address']:
response = clean_replace(response, v, k + '_SLOT', forward=True, backward=False)
else:
response = clean_replace(response, v, k + '_SLOT', forward=False, backward=False)
# ################################
# pdb.set_trace()
# ##################################
return response
def _value_key_map(self, db_data):
requestable_keys = [
'address', 'name', 'phone', 'postcode', 'food', 'area', 'pricerange',
'open', 'price', 'parking',
'duration', 'arrive_in',
'temperature', 'weather_type',
'rating', 'company', 'director'
]
# requestable_keys = ['open', 'price', 'parking']
value_key = {}
for db_entry in db_data:
for k, v in db_entry.items():
if k in requestable_keys:
value_key[v] = k
return value_key
def _get_encoded_data(self, tokenized_data):
encoded_data = []
for dial in tokenized_data:
encoded_dial = []
prev_response = []
for turn in dial:
user = self.vocab.sentence_encode(turn['user'])
response = self.vocab.sentence_encode(turn['response'])
constraint = self.vocab.sentence_encode(turn['constraint'])
requested = self.vocab.sentence_encode(turn['requested'])
degree = self._degree_vec_mapping(turn['degree'])
turn_num = turn['turn_num']
dial_id = turn['dial_id']
# ################################
# pdb.set_trace()
# ##################################
# final input
encoded_dial.append({
'dial_id': dial_id,
'turn_num': turn_num,
'user': prev_response + user,
'response': response,
'bspan': constraint + requested,
'u_len': len(prev_response + user),
'm_len': len(response),
'degree': degree,
})
# modified
prev_response = response
encoded_data.append(encoded_dial)
return encoded_data
def _split_data(self, encoded_data, split):
"""
split data into train/dev/test
:param encoded_data: list
:param split: tuple / list
:return:
"""
total = sum(split)
dev_thr = len(encoded_data) * split[0] // total
test_thr = len(encoded_data) * (split[0] + split[1]) // total
train, dev, test = encoded_data[:dev_thr], encoded_data[dev_thr:test_thr], encoded_data[test_thr:]
return train, dev, test
def _construct_maml(self, data_json_path_list, db_json_path_list):
"""
construct encoded train, dev, test set.
:param data_json_path:
:param db_json_path:
:return:
"""
for i in range(len(data_json_path_list)):
construct_vocab = False
if not os.path.isfile(cfg.vocab_path) or cfg.enlarge_vocab:
construct_vocab = True
print('Constructing vocab file...')
raw_data_json = open(data_json_path_list[i])
raw_data = json.loads(raw_data_json.read().lower())
db_json = open(db_json_path_list[i])
db_data = json.loads(db_json.read().lower())
self.db.append(db_data)
tokenized_data = self._get_tokenized_data(raw_data, db_data, construct_vocab)
if construct_vocab:
self.vocab.construct(cfg.vocab_size)
self.vocab.save_vocab(cfg.vocab_path)
elif cfg.enlarge_vocab:
self.vocab.construct(cfg.vocab_size)
self.vocab.enlarge_vocab(cfg.vocab_path)
else:
self.vocab.load_vocab(cfg.vocab_path)
encoded_data = self._get_encoded_data(tokenized_data)
train_tmp, dev_tmp, test_tmp = self._split_data(encoded_data, cfg.split)
random.shuffle(train_tmp)
random.shuffle(dev_tmp)
random.shuffle(test_tmp)
self.train.append(train_tmp)
self.dev.append(dev_tmp)
self.test.append(test_tmp)
raw_data_json.close()
db_json.close()
def _construct(self, data_json_path, db_json_path):
"""
construct encoded train, dev, test set.
:param data_json_path:
:param db_json_path:
:return:
"""
construct_vocab = False
if not os.path.isfile(cfg.vocab_path):
construct_vocab = True
print('Constructing vocab file...')
raw_data_json = open(data_json_path)
raw_data = json.loads(raw_data_json.read().lower())
db_json = open(db_json_path)
db_data = json.loads(db_json.read().lower())
self.db = db_data
tokenized_data = self._get_tokenized_data(raw_data, db_data, construct_vocab)
if construct_vocab:
self.vocab.construct(cfg.vocab_size)
self.vocab.save_vocab(cfg.vocab_path)
else:
self.vocab.load_vocab(cfg.vocab_path)
encoded_data = self._get_encoded_data(tokenized_data)
# random.shuffle(encoded_data)
self.train, self.dev, self.test = self._split_data(encoded_data, cfg.split)
random.shuffle(self.train)
random.shuffle(self.dev)
random.shuffle(self.test)
raw_data_json.close()
db_json.close()
def db_search(self, constraints, db_data):
match_results = []
for entry in db_data:
entry_values = ' '.join(entry.values())
match = True
for c in constraints:
if c not in entry_values:
match = False
break
if match:
match_results.append(entry)
return match_results
class KvretReader(_ReaderBase):
def __init__(self):
super().__init__()
self.entity_dict = {}
self.abbr_dict = {}
self.wn = WordNetLemmatizer()
self.db = {}
self.tokenized_data_path = './data/kvret/'
self._construct(cfg.train, cfg.dev, cfg.test, cfg.entity)
def _construct(self, train_json_path, dev_json_path, test_json_path, entity_json_path):
construct_vocab = False
if not os.path.isfile(cfg.vocab_path):
construct_vocab = True
print('Constructing vocab file...')
train_json, dev_json, test_json = open(train_json_path), open(dev_json_path), open(test_json_path)
entity_json = open(entity_json_path)
train_data, dev_data, test_data = json.loads(train_json.read().lower()), json.loads(dev_json.read().lower()), \
json.loads(test_json.read().lower())
entity_data = json.loads(entity_json.read().lower())
self._get_entity_dict(entity_data)
tokenized_train = self._get_tokenized_data(train_data, construct_vocab, 'train')
tokenized_dev = self._get_tokenized_data(dev_data, construct_vocab, 'dev')
tokenized_test = self._get_tokenized_data(test_data, construct_vocab, 'test')
if construct_vocab:
self.vocab.construct(cfg.vocab_size)
self.vocab.save_vocab(cfg.vocab_path)
else:
self.vocab.load_vocab(cfg.vocab_path)
self.train, self.dev, self.test = map(self._get_encoded_data, [tokenized_train, tokenized_dev,
tokenized_test])
random.shuffle(self.train)
random.shuffle(self.dev)
random.shuffle(self.test)
def _save_tokenized_data(self, data, filename):
path = self.tokenized_data_path + filename + '.tokenized.json'
f = open(path,'w')
json.dump(data,f,indent=2)
f.close()
def _load_tokenized_data(self, filename):
'''
path = self.tokenized_data_path + filename + '.tokenized.json'
try:
f = open(path,'r')
except FileNotFoundError:
return None
data = json.load(f)
f.close()
return data
'''
return None
def _tokenize(self, sent):
return ' '.join(word_tokenize(sent))
def _lemmatize(self, sent):
return ' '.join([self.wn.lemmatize(_) for _ in sent.split()])
def _replace_entity(self, response, vk_map, prev_user_input, intent):
response = re.sub('\d+-?\d*fs?', 'temperature_SLOT', response)
response = re.sub('\d+\s?miles?', 'distance_SLOT', response)
response = re.sub('\d+\s\w+\s(dr)?(ct)?(rd)?(road)?(st)?(ave)?(way)?(pl)?\w*[.]?', 'address_SLOT', response)
response = self._lemmatize(self._tokenize(response))
requestable = {
'weather': ['weather_attribute'],
'navigate': ['poi', 'traffic_info', 'address', 'distance'],
'schedule': ['event', 'date', 'time', 'party', 'agenda', 'room']
}
reqs = set()
for v, k in sorted(vk_map.items(), key=lambda x: -len(x[0])):
start_idx = response.find(v)
if start_idx == -1 or k not in requestable[intent]:
continue
end_idx = start_idx + len(v)
while end_idx < len(response) and response[end_idx] != ' ':
end_idx += 1
# test whether they are indeed the same word
lm1, lm2 = v.replace('.', '').replace(' ', '').replace("'", ''), \
response[start_idx:end_idx].replace('.', '').replace(' ', '').replace("'", '')
if lm1 == lm2 and lm1 not in prev_user_input and v not in prev_user_input:
response = clean_replace(response, response[start_idx:end_idx], k + '_SLOT')
reqs.add(k)
return response,reqs
def _clean_constraint_dict(self, constraint_dict, intent, prefer='short'):
"""
clean the constraint dict so that every key is in "informable" and similar to one in provided entity dict.
:param constraint_dict:
:return:
"""
informable = {
'weather': ['date', 'location', 'weather_attribute'],
'navigate': ['poi_type', 'distance'],
'schedule': ['event', 'date', 'time', 'agenda', 'party', 'room']
}
del_key = set(constraint_dict.keys()).difference(informable[intent])
for key in del_key:
constraint_dict.pop(key)
invalid_key = []
for k in constraint_dict:
constraint_dict[k] = constraint_dict[k].strip()
v = self._lemmatize(self._tokenize(constraint_dict[k]))
v = re.sub('(\d+) ([ap]m)', lambda x: x.group(1) + x.group(2), v)
v = re.sub('(\d+)\s?(mile)s?', lambda x: x.group(1) + ' ' + x.group(2), v)
if v in self.entity_dict:
if prefer == 'short':
constraint_dict[k] = v
elif prefer == 'long':
constraint_dict[k] = self.abbr_dict.get(v, v)
elif v.split()[0] in self.entity_dict:
if prefer == 'short':
constraint_dict[k] = v.split()[0]
elif prefer == 'long':
constraint_dict[k] = self.abbr_dict.get(v.split()[0], v)
else:
invalid_key.append(k)
for key in invalid_key:
constraint_dict.pop(key)
return constraint_dict
def _get_tokenized_data(self, raw_data, add_to_vocab, data_type, is_test=False):
"""
Somerrthing to note: We define requestable and informable slots as below in further experiments
(including other baselines):
informable = {
'weather': ['date','location','weather_attribute'],
'navigate': ['poi_type','distance'],
'schedule': ['event']
}
requestable = {
'weather': ['weather_attribute'],
'navigate': ['poi','traffic','address','distance'],
'schedule': ['event','date','time','party','agenda','room']
}
:param raw_data:
:param add_to_vocab:
:param data_type:
:return:
"""
tokenized_data = self._load_tokenized_data(data_type)
if tokenized_data is not None:
logging.info('directly loading %s' % data_type)
return tokenized_data
tokenized_data = []
state_dump = {}
for dial_id, raw_dial in enumerate(raw_data):
tokenized_dial = []
prev_utter = ''
single_turn = {}
constraint_dict = {}
intent = raw_dial['scenario']['task']['intent']
if cfg.intent != 'all' and cfg.intent != intent:
if intent not in ['navigate', 'weather', 'schedule']:
raise ValueError('what is %s intent bro?' % intent)
else:
continue
prev_response = []
for turn_num, dial_turn in enumerate(raw_dial['dialogue']):
state_dump[(dial_id, turn_num)] = {}
if dial_turn['turn'] == 'driver':
u = self._lemmatize(self._tokenize(dial_turn['data']['utterance']))
u = re.sub('(\d+) ([ap]m)', lambda x: x.group(1) + x.group(2), u)
single_turn['user'] = prev_response + u.split() + ['EOS_U']
prev_utter += u
elif dial_turn['turn'] == 'assistant':
s = dial_turn['data']['utterance']
# find entities and replace them
s = re.sub('(\d+) ([ap]m)', lambda x: x.group(1) + x.group(2), s)
s, reqs = self._replace_entity(s, self.entity_dict, prev_utter, intent)
single_turn['response'] = s.split() + ['EOS_M']
# get constraints
if not constraint_dict:
constraint_dict = dial_turn['data']['slots']
else:
for k, v in dial_turn['data']['slots'].items():
constraint_dict[k] = v
constraint_dict = self._clean_constraint_dict(constraint_dict, intent)
raw_constraints = constraint_dict.values()
raw_constraints = [self._lemmatize(self._tokenize(_)) for _ in raw_constraints]
# add separator
constraints = []
for item in raw_constraints:
if constraints:
constraints.append(';')
constraints.extend(item.split())
# get requests
dataset_requested = set(
filter(lambda x: dial_turn['data']['requested'][x], dial_turn['data']['requested'].keys()))
requestable = {
'weather': ['weather_attribute'],
'navigate': ['poi', 'traffic', 'address', 'distance'],
'schedule': ['date', 'time', 'party', 'agenda', 'room']
}
requests = sorted(list(dataset_requested.intersection(reqs)))
single_turn['constraint'] = constraints + ['EOS_Z1']
single_turn['requested'] = requests + ['EOS_Z2']
single_turn['turn_num'] = len(tokenized_dial)
single_turn['dial_id'] = dial_id
single_turn['degree'] = self.db_degree(constraints, raw_dial['scenario']['kb']['items'])
self.db[dial_id] = raw_dial['scenario']['kb']['items']
if 'user' in single_turn:
state_dump[(dial_id, len(tokenized_dial))]['constraint'] = constraint_dict
state_dump[(dial_id, len(tokenized_dial))]['request'] = requests
tokenized_dial.append(single_turn)
prev_response = single_turn['response']
single_turn = {}
if add_to_vocab:
for single_turn in tokenized_dial:
for word_token in single_turn['constraint'] + single_turn['requested'] + \
single_turn['user'] + single_turn['response']:
self.vocab.add_item(word_token)
tokenized_data.append(tokenized_dial)
self._save_tokenized_data(tokenized_data, data_type)
return tokenized_data
def _get_encoded_data(self, tokenized_data):
encoded_data = []
for dial in tokenized_data:
new_dial = []
for turn in dial:
turn['constraint'] = self.vocab.sentence_encode(turn['constraint'])
turn['requested'] = self.vocab.sentence_encode(turn['requested'])
turn['bspan'] = turn['constraint'] + turn['requested']
turn['user'] = self.vocab.sentence_encode(turn['user'])
turn['response'] = self.vocab.sentence_encode(turn['response'])
turn['u_len'] = len(turn['user'])
turn['m_len'] = len(turn['response'])
turn['degree'] = self._degree_vec_mapping(turn['degree'])
new_dial.append(turn)
encoded_data.append(new_dial)
return encoded_data
def _get_entity_dict(self, entity_data):
entity_dict = {}
for k in entity_data:
if type(entity_data[k][0]) is str:
for entity in entity_data[k]:
entity = self._lemmatize(self._tokenize(entity))
entity_dict[entity] = k
if k in ['event', 'poi_type']:
entity_dict[entity.split()[0]] = k
self.abbr_dict[entity.split()[0]] = entity
elif type(entity_data[k][0]) is dict:
for entity_entry in entity_data[k]:
for entity_type, entity in entity_entry.items():
entity_type = 'poi_type' if entity_type == 'type' else entity_type
entity = self._lemmatize(self._tokenize(entity))
entity_dict[entity] = entity_type
if entity_type in ['event', 'poi_type']:
entity_dict[entity.split()[0]] = entity_type
self.abbr_dict[entity.split()[0]] = entity
self.entity_dict = entity_dict
def db_degree(self, constraints, items):
cnt = 0
if items is not None:
for item in items:
flg = True
for c in constraints:
if c not in item:
flg = False
break
if flg:
cnt += 1
return cnt
def db_degree_handler(self, z_samples, idx=None, *args, **kwargs):
control_vec = []
for i,cons_idx_list in enumerate(z_samples):
constraints = set()
for cons in cons_idx_list:
if type(cons) is not str:
cons = self.vocab.decode(cons)
if cons == 'EOS_Z1':
break
constraints.add(cons)
items = self.db[idx[i]]
degree = self.db_degree(constraints, items)
control_vec.append(self._degree_vec_mapping(degree))
return np.array(control_vec)
def pad_sequences(sequences, maxlen=None, dtype='int32',
padding='pre', truncating='pre', value=0.):
if not hasattr(sequences, '__len__'):
raise ValueError('`sequences` must be iterable.')
lengths = []
for x in sequences:
if not hasattr(x, '__len__'):
raise ValueError('`sequences` must be a list of iterables. '
'Found non-iterable: ' + str(x))
lengths.append(len(x))
num_samples = len(sequences)
seq_maxlen = np.max(lengths)
if maxlen is not None and cfg.truncated:
maxlen = min(seq_maxlen, maxlen)
else:
maxlen = seq_maxlen
# take the sample shape from the first non empty sequence
# checking for consistency in the main loop below.
sample_shape = tuple()
for s in sequences:
if len(s) > 0:
sample_shape = np.asarray(s).shape[1:]
break
x = (np.ones((num_samples, maxlen) + sample_shape) * value).astype(dtype)
for idx, s in enumerate(sequences):
if not len(s):
continue # empty list/array was found
if truncating == 'pre':
trunc = s[-maxlen:]
elif truncating == 'post':
trunc = s[:maxlen]
else:
raise ValueError('Truncating type "%s" not understood' % truncating)
# check `trunc` has expected shape
trunc = np.asarray(trunc, dtype=dtype)
if trunc.shape[1:] != sample_shape:
raise ValueError('Shape of sample %s of sequence at position %s is different from expected shape %s' %
(trunc.shape[1:], idx, sample_shape))
if padding == 'post':
x[idx, :len(trunc)] = trunc
elif padding == 'pre':
x[idx, -len(trunc):] = trunc
else:
raise ValueError('Padding type "%s" not understood' % padding)
return x
def get_glove_matrix(vocab, initial_embedding_np):
"""
return a glove embedding matrix
:param self:
:param glove_file:
:param initial_embedding_np: