forked from salesforce/simpletod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_multiwoz.py
907 lines (756 loc) · 34.2 KB
/
preprocess_multiwoz.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
# -*- coding: utf-8 -*-
import copy
import json
import os
import sys
import re
import shutil
import urllib
from urllib import request
from collections import OrderedDict
from io import BytesIO
from zipfile import ZipFile
from tqdm import tqdm
import numpy as np
from utils.multiwoz import dbPointer
from utils.multiwoz import delexicalize
from utils.multiwoz.nlp import normalize, normalize_lexical, normalize_beliefstate, normalize_mine
import ipdb
np.set_printoptions(precision=3)
np.random.seed(2)
# GLOBAL VARIABLES
DICT_SIZE = 1000000
MAX_LENGTH = 600
DATA_DIR = './resources'
def is_ascii(s):
return all(ord(c) < 128 for c in s)
def fixDelex(filename, data, data2, idx, idx_acts):
"""Given system dialogue acts fix automatic delexicalization."""
try:
turn = data2[filename.strip('.json')][str(idx_acts)]
except:
return data
# if not isinstance(turn, str) and not isinstance(turn, unicode):
if not isinstance(turn, bytes) and not isinstance(turn, str):
for k, act in turn.items():
if 'Attraction' in k:
if 'restaurant_' in data['log'][idx]['text']:
data['log'][idx]['text'] = data['log'][idx]['text'].replace("restaurant", "attraction")
if 'hotel_' in data['log'][idx]['text']:
data['log'][idx]['text'] = data['log'][idx]['text'].replace("hotel", "attraction")
if 'Hotel' in k:
if 'attraction_' in data['log'][idx]['text']:
data['log'][idx]['text'] = data['log'][idx]['text'].replace("attraction", "hotel")
if 'restaurant_' in data['log'][idx]['text']:
data['log'][idx]['text'] = data['log'][idx]['text'].replace("restaurant", "hotel")
if 'Restaurant' in k:
if 'attraction_' in data['log'][idx]['text']:
data['log'][idx]['text'] = data['log'][idx]['text'].replace("attraction", "restaurant")
if 'hotel_' in data['log'][idx]['text']:
data['log'][idx]['text'] = data['log'][idx]['text'].replace("hotel", "restaurant")
return data
def delexicaliseReferenceNumber(sent, turn):
"""Based on the belief state, we can find reference number that
during data gathering was created randomly."""
domains = ['restaurant', 'hotel', 'attraction', 'train', 'taxi', 'hospital'] # , 'police']
if turn['metadata']:
for domain in domains:
if turn['metadata'][domain]['book']['booked']:
for slot in turn['metadata'][domain]['book']['booked'][0]:
if slot == 'reference':
val = '[' + domain + '_' + slot + ']'
else:
val = '[' + domain + '_' + slot + ']'
key = normalize(turn['metadata'][domain]['book']['booked'][0][slot])
sent = (' ' + sent + ' ').replace(' ' + key + ' ', ' ' + val + ' ')
# try reference with hashtag
key = normalize("#" + turn['metadata'][domain]['book']['booked'][0][slot])
sent = (' ' + sent + ' ').replace(' ' + key + ' ', ' ' + val + ' ')
# try reference with ref#
key = normalize("ref#" + turn['metadata'][domain]['book']['booked'][0][slot])
sent = (' ' + sent + ' ').replace(' ' + key + ' ', ' ' + val + ' ')
return sent
def delexicaliseReferenceNumber_mine(sent, turn):
"""Based on the belief state, we can find reference number that
during data gathering was created randomly."""
domains = ['restaurant', 'hotel', 'attraction', 'train', 'taxi', 'hospital'] # , 'police']
if turn['metadata']:
for domain in domains:
if turn['metadata'][domain]['book']['booked']:
for slot in turn['metadata'][domain]['book']['booked'][0]:
if slot == 'reference':
val = '[' + domain + '_' + slot + ']'
else:
val = '[' + domain + '_' + slot + ']'
key = normalize_mine(turn['metadata'][domain]['book']['booked'][0][slot])
sent = (' ' + sent + ' ').replace(' ' + key + ' ', ' ' + val + ' ')
# try reference with hashtag
key = normalize_mine("#" + turn['metadata'][domain]['book']['booked'][0][slot])
sent = (' ' + sent + ' ').replace(' ' + key + ' ', ' ' + val + ' ')
# try reference with ref#
key = normalize_mine("ref#" + turn['metadata'][domain]['book']['booked'][0][slot])
sent = (' ' + sent + ' ').replace(' ' + key + ' ', ' ' + val + ' ')
return sent
def addBookingPointer(task, turn, pointer_vector):
"""Add information about availability of the booking option."""
# Booking pointer
rest_vec = np.array([1, 0])
if task['goal']['restaurant']:
# if turn['metadata']['restaurant'].has_key("book"):
# if turn['metadata']['restaurant']['book'].has_key("booked"):
if "book" in turn['metadata']['restaurant']:
if "booked" in turn['metadata']['restaurant']['book']:
if turn['metadata']['restaurant']['book']["booked"]:
if "reference" in turn['metadata']['restaurant']['book']["booked"][0]:
rest_vec = np.array([0, 1])
hotel_vec = np.array([1, 0])
if task['goal']['hotel']:
# if turn['metadata']['hotel'].has_key("book"):
# if turn['metadata']['hotel']['book'].has_key("booked"):
if "book" in turn['metadata']['hotel']:
if "booked" in turn['metadata']['hotel']['book']:
if turn['metadata']['hotel']['book']["booked"]:
if "reference" in turn['metadata']['hotel']['book']["booked"][0]:
hotel_vec = np.array([0, 1])
train_vec = np.array([1, 0])
if task['goal']['train']:
# if turn['metadata']['train'].has_key("book"):
# if turn['metadata']['train']['book'].has_key("booked"):
if "book" in turn['metadata']['train']:
if "booked" in turn['metadata']['train']['book']:
if turn['metadata']['train']['book']["booked"]:
if "reference" in turn['metadata']['train']['book']["booked"][0]:
train_vec = np.array([0, 1])
pointer_vector = np.append(pointer_vector, rest_vec)
pointer_vector = np.append(pointer_vector, hotel_vec)
pointer_vector = np.append(pointer_vector, train_vec)
return pointer_vector
def addDBPointer(turn):
"""Create database pointer for all related domains."""
domains = ['restaurant', 'hotel', 'attraction', 'train']
pointer_vector = np.zeros(6 * len(domains))
for domain in domains:
num_entities = dbPointer.queryResult(domain, turn)
pointer_vector = dbPointer.oneHotVector(num_entities, domain, pointer_vector)
return pointer_vector
def get_summary_bstate(bstate):
"""Based on the mturk annotations we form multi-domain belief state"""
domains = [u'taxi', u'restaurant', u'hospital', u'hotel', u'attraction', u'train', u'police']
summary_bstate = []
for domain in domains:
domain_active = False
booking = []
# print(domain,len(bstate[domain]['book'].keys()))
for slot in sorted(bstate[domain]['book'].keys()):
if slot == 'booked':
if bstate[domain]['book']['booked']:
booking.append(1)
else:
booking.append(0)
else:
if bstate[domain]['book'][slot] != "":
booking.append(1)
else:
booking.append(0)
if domain == 'train':
if 'people' not in bstate[domain]['book'].keys():
booking.append(0)
if 'ticket' not in bstate[domain]['book'].keys():
booking.append(0)
summary_bstate += booking
for slot in bstate[domain]['semi']:
slot_enc = [0, 0, 0] # not mentioned, dontcare, filled
if bstate[domain]['semi'][slot] == 'not mentioned':
slot_enc[0] = 1
elif bstate[domain]['semi'][slot] == 'dont care' or bstate[domain]['semi'][slot] == 'dontcare' or \
bstate[domain]['semi'][slot] == "don't care":
slot_enc[1] = 1
elif bstate[domain]['semi'][slot]:
slot_enc[2] = 1
if slot_enc != [0, 0, 0]:
domain_active = True
summary_bstate += slot_enc
# quasi domain-tracker
if domain_active:
summary_bstate += [1]
else:
summary_bstate += [0]
# print(len(summary_bstate))
assert len(summary_bstate) == 94
return summary_bstate
def get_belief_state(bstate):
domains = [u'taxi', u'restaurant', u'hospital', u'hotel', u'attraction', u'train', u'police']
raw_bstate = []
for domain in domains:
for slot, value in bstate[domain]['semi'].items():
if value:
raw_bstate.append((domain, slot, normalize_beliefstate(value)))
for slot, value in bstate[domain]['book'].items():
if slot == 'booked':
continue
if value:
new_slot = '{} {}'.format('book', slot)
raw_bstate.append((domain, new_slot, normalize_beliefstate(value)))
# ipdb.set_trace()
return raw_bstate
def analyze_dialogue(dialogue, maxlen):
"""Cleaning procedure for all kinds of errors in text and annotation."""
d = dialogue
# do all the necessary postprocessing
if len(d['log']) % 2 != 0:
# print path
print('odd # of turns')
return None # odd number of turns, wrong dialogue
d_pp = {}
d_pp['goal'] = d['goal'] # for now we just copy the goal
usr_turns = []
sys_turns = []
for i in range(len(d['log'])):
if len(d['log'][i]['text'].split()) > maxlen:
print('too long')
return None # too long sentence, wrong dialogue
if i % 2 == 0: # usr turn
if 'db_pointer' not in d['log'][i]:
print('no db')
return None # no db_pointer, probably 2 usr turns in a row, wrong dialogue
text = d['log'][i]['text']
if not is_ascii(text):
print('not ascii')
return None
# d['log'][i]['tkn_text'] = self.tokenize_sentence(text, usr=True)
usr_turns.append(d['log'][i])
else: # sys turn
text = d['log'][i]['text']
if not is_ascii(text):
print('not ascii')
return None
# d['log'][i]['tkn_text'] = self.tokenize_sentence(text, usr=False)
belief_summary = get_summary_bstate(d['log'][i]['metadata'])
d['log'][i]['belief_summary'] = belief_summary
sys_turns.append(d['log'][i])
d_pp['usr_log'] = usr_turns
d_pp['sys_log'] = sys_turns
return d_pp
def analyze_dialogue_raw_beliefstate(dialogue, maxlen):
"""Cleaning procedure for all kinds of errors in text and annotation."""
d = dialogue
# do all the necessary postprocessing
if len(d['log']) % 2 != 0:
# print path
print('odd # of turns')
return None # odd number of turns, wrong dialogue
d_pp = {}
d_pp['goal'] = d['goal'] # for now we just copy the goal
usr_turns = []
sys_turns = []
for i in range(len(d['log'])):
if len(d['log'][i]['text'].split()) > maxlen:
print('too long')
return None # too long sentence, wrong dialogue
if i % 2 == 0: # usr turn
if 'db_pointer' not in d['log'][i]:
print('no db')
return None # no db_pointer, probably 2 usr turns in a row, wrong dialogue
text = d['log'][i]['text']
if not is_ascii(text):
print('not ascii')
return None
# d['log'][i]['tkn_text'] = self.tokenize_sentence(text, usr=True)
usr_turns.append(d['log'][i])
else: # sys turn
text = d['log'][i]['text']
if not is_ascii(text):
print('not ascii')
return None
# d['log'][i]['tkn_text'] = self.tokenize_sentence(text, usr=False)
belief_summary = get_summary_bstate(d['log'][i]['metadata'])
d['log'][i]['belief_summary'] = belief_summary
# get raw belief state
belief_state = get_belief_state(d['log'][i]['metadata'])
d['log'][i]['belief_state'] = belief_state
sys_turns.append(d['log'][i])
d_pp['usr_log'] = usr_turns
d_pp['sys_log'] = sys_turns
return d_pp
def analyze_dialogue_raw_beliefstate_v2(dialogue, maxlen):
"""Cleaning procedure for all kinds of errors in text and annotation."""
d = dialogue
# do all the necessary postprocessing
if len(d['log']) % 2 != 0:
# print path
print('odd # of turns')
return None # odd number of turns, wrong dialogue
d_pp = {}
d_pp['goal'] = d['goal'] # for now we just copy the goal
usr_turns = []
sys_turns = []
for i in range(len(d['log'])):
if len(d['log'][i]['text'].split()) > maxlen:
print('too long')
return None # too long sentence, wrong dialogue
if i % 2 == 0: # usr turn
if 'db_pointer' not in d['log'][i]:
print('no db')
return None # no db_pointer, probably 2 usr turns in a row, wrong dialogue
text = d['log'][i]['text']
if not is_ascii(text):
print('not ascii')
return None
# d['log'][i]['tkn_text'] = self.tokenize_sentence(text, usr=True)
usr_turns.append(d['log'][i])
else: # sys turn
text = d['log'][i]['text']
if not is_ascii(text):
print('not ascii')
return None
# d['log'][i]['tkn_text'] = self.tokenize_sentence(text, usr=False)
belief_summary = get_summary_bstate(d['log'][i]['metadata'])
d['log'][i]['belief_summary'] = belief_summary
# get raw belief state
belief_state = get_belief_state(d['log'][i]['metadata'])
d['log'][i]['belief_state'] = belief_state
sys_turns.append(d['log'][i])
d_pp['usr_log'] = usr_turns
d_pp['sys_log'] = sys_turns
return d_pp
def get_dial(dialogue):
"""Extract a dialogue from the file"""
dial = []
# d_orig = analyze_dialogue(dialogue, MAX_LENGTH) # max turn len is 50 words
d_orig = analyze_dialogue_raw_beliefstate(dialogue, MAX_LENGTH) # max turn len is 50 words
if d_orig is None:
return None
usr = [t['text'] for t in d_orig['usr_log']]
db = [t['db_pointer'] for t in d_orig['usr_log']]
bs = [t['belief_summary'] for t in d_orig['sys_log']]
sys = [t['text'] for t in d_orig['sys_log']]
for u, d, s, b in zip(usr, db, sys, bs):
dial.append((u, s, d, b))
return dial
def get_dial_raw_bstate(dialogue):
"""Extract a dialogue from the file"""
dial = []
d_orig = analyze_dialogue_raw_beliefstate(dialogue, MAX_LENGTH) # max turn len is 50 words
if d_orig is None:
return None
usr = [t['text'] for t in d_orig['usr_log']]
db = [t['db_pointer'] for t in d_orig['usr_log']]
bs = [t['belief_summary'] for t in d_orig['sys_log']]
belief_state = [t['belief_state'] for t in d_orig['sys_log']]
sys = [t['text'] for t in d_orig['sys_log']]
for u, d, s, b, bstate in zip(usr, db, sys, bs, belief_state):
dial.append((u, s, d, b, bstate))
return dial
def createDict(word_freqs):
words = [k for k in word_freqs.keys()]
freqs = [v for v in word_freqs.values()]
sorted_idx = np.argsort(freqs)
sorted_words = [words[ii] for ii in sorted_idx[::-1]]
# Extra vocabulary symbols
_GO = '_GO'
EOS = '_EOS'
UNK = '_UNK'
PAD = '_PAD'
SEP0 = '_SEP0'
SEP1 = '_SEP1'
SEP2 = '_SEP2'
SEP3 = '_SEP3'
SEP4 = '_SEP4'
SEP5 = '_SEP5'
SEP6 = '_SEP6'
SEP7 = '_SEP7'
extra_tokens = [_GO, EOS, UNK, PAD, SEP0, SEP1, SEP2, SEP3, SEP4, SEP5, SEP6, SEP7]
# extra_tokens = [_GO, EOS, UNK, PAD]
worddict = OrderedDict()
for ii, ww in enumerate(extra_tokens):
worddict[ww] = ii
for ii, ww in enumerate(sorted_words):
worddict[ww] = ii #+ len(extra_tokens)
new_worddict = worddict.copy()
for key, idx in worddict.items():
if idx >= DICT_SIZE:
del new_worddict[key]
return new_worddict
def moveFiles(src_path, dst_path):
shutil.copy(os.path.join(src_path, 'data.json'), dst_path)
shutil.copy(os.path.join(src_path, 'valListFile.json'), dst_path)
shutil.copy(os.path.join(src_path, 'testListFile.json'), dst_path)
shutil.copy(os.path.join(src_path, 'dialogue_acts.json'), dst_path)
return
def createDelexData():
"""Main function of the script - loads delexical dictionary,
goes through each dialogue and does:
1) data normalization
2) delexicalization
3) addition of database pointer
4) saves the delexicalized data
"""
# download the data
loadDataMultiWoz()
# create dictionary of delexicalied values that then we will search against, order matters here!
dic = delexicalize.prepareSlotValuesIndependent()
delex_data = {}
fin1 = open(os.path.join(DATA_DIR, 'multi-woz/data.json'))
data = json.load(fin1)
fin2 = open(os.path.join(DATA_DIR, 'multi-woz/dialogue_acts.json'))
data2 = json.load(fin2)
for dialogue_name in tqdm(data):
dialogue = data[dialogue_name]
# print dialogue_name
idx_acts = 1
for idx, turn in enumerate(dialogue['log']):
# normalization, split and delexicalization of the sentence
sent = normalize(turn['text'])
words = sent.split()
sent = delexicalize.delexicalise(' '.join(words), dic)
# parsing reference number GIVEN belief state
sent = delexicaliseReferenceNumber(sent, turn)
# changes to numbers only here
digitpat = re.compile('\d+')
sent = re.sub(digitpat, '[value_count]', sent)
# delexicalized sentence added to the dialogue
dialogue['log'][idx]['text'] = sent
if idx % 2 == 1: # if it's a system turn
# add database pointer
pointer_vector = addDBPointer(turn)
# add booking pointer
pointer_vector = addBookingPointer(dialogue, turn, pointer_vector)
# print pointer_vector
dialogue['log'][idx - 1]['db_pointer'] = pointer_vector.tolist()
# FIXING delexicalization:
dialogue = fixDelex(dialogue_name, dialogue, data2, idx, idx_acts)
idx_acts += 1
delex_data[dialogue_name] = dialogue
with open(os.path.join(DATA_DIR, 'multi-woz/delex.json'), 'w') as outfile:
json.dump(delex_data, outfile)
return delex_data
def loadDataMultiWoz():
data_url = os.path.join(DATA_DIR, 'multi-woz-2.1/data.json')
dataset_url = "https://www.repository.cam.ac.uk/bitstream/handle/1810/294507/MULTIWOZ2.1.zip?sequence=1&isAllowed=y"
download_path = os.path.join(DATA_DIR, 'multi-woz')
extract_path = os.path.join(download_path, 'MULTIWOZ2.1')
os.makedirs(download_path, exist_ok=True)
if not os.path.exists(data_url):
print("Downloading and unzipping the MultiWOZ dataset")
resp = urllib.request.urlopen(dataset_url)
zip_ref = ZipFile(BytesIO(resp.read()))
zip_ref.extractall(download_path)
zip_ref.close()
moveFiles(src_path=extract_path, dst_path=download_path)
return
def createDelexData_mine():
"""Main function of the script - loads delexical dictionary,
goes through each dialogue and does:
1) data normalization
2) delexicalization
3) addition of database pointer
4) saves the delexicalized data
"""
# download the data
loadDataMultiWoz()
# create dictionary of delexicalied values that then we will search against, order matters here!
dic = delexicalize.prepareSlotValuesIndependent_mine()
delex_data = {}
fin1 = open(os.path.join(DATA_DIR, 'multi-woz/data.json'))
data = json.load(fin1)
fin2 = open(os.path.join(DATA_DIR, 'multi-woz/dialogue_acts.json'))
data2 = json.load(fin2)
for dialogue_name in tqdm(data):
dialogue = data[dialogue_name]
# print dialogue_name
idx_acts = 1
for idx, turn in enumerate(dialogue['log']):
# normalization, split and delexicalization of the sentence
sent = normalize_mine(turn['text'])
# only delexicalize system response
if idx % 2 == 1:
words = sent.split()
sent = delexicalize.delexicalise(' '.join(words), dic)
# parsing reference number GIVEN belief state
sent = delexicaliseReferenceNumber_mine(sent, turn)
# changes to numbers only here
digitpat = re.compile('\d+')
# sent = re.sub(digitpat, '[value_count]', sent)
# delexicalized sentence added to the dialogue
dialogue['log'][idx]['text'] = sent
if idx % 2 == 1: # if it's a system turn
# add database pointer
pointer_vector = addDBPointer(turn)
# add booking pointer
pointer_vector = addBookingPointer(dialogue, turn, pointer_vector)
# print pointer_vector
dialogue['log'][idx - 1]['db_pointer'] = pointer_vector.tolist()
# FIXING delexicalization:
dialogue = fixDelex(dialogue_name, dialogue, data2, idx, idx_acts)
idx_acts += 1
delex_data[dialogue_name] = dialogue
with open(os.path.join(DATA_DIR, 'multi-woz/delex_mine.json'), 'w') as outfile:
json.dump(delex_data, outfile)
return delex_data
def createLexicalData():
"""Main function of the script - loads delexical dictionary,
goes through each dialogue and does:
1) data normalization
2) delexicalization
3) addition of database pointer
4) saves the delexicalized data
"""
# download the data
loadDataMultiWoz()
# create dictionary of delexicalied values that then we will search against, order matters here!
# dic = delexicalize.prepareSlotValuesIndependent()
delex_data = {}
fin1 = open(os.path.join(DATA_DIR, 'multi-woz/data.json'))
data = json.load(fin1)
fin2 = open(os.path.join(DATA_DIR, 'multi-woz/dialogue_acts.json'))
data2 = json.load(fin2)
for dialogue_name in tqdm(data):
dialogue = data[dialogue_name]
# print dialogue_name
idx_acts = 1
for idx, turn in enumerate(dialogue['log']):
# normalization, split and delexicalization of the sentence
sent = normalize_lexical(turn['text'])
# words = sent.split()
# sent = delexicalize.delexicalise(' '.join(words), dic)
# parsing reference number GIVEN belief state
# sent = delexicaliseReferenceNumber(sent, turn)
# changes to numbers only here
# digitpat = re.compile('\d+')
# sent = re.sub(digitpat, '[value_count]', sent)
# delexicalized sentence added to the dialogue
dialogue['log'][idx]['text'] = sent
if idx % 2 == 1: # if it's a system turn
# add database pointer
pointer_vector = addDBPointer(turn)
# add booking pointer
pointer_vector = addBookingPointer(dialogue, turn, pointer_vector)
# print pointer_vector
dialogue['log'][idx - 1]['db_pointer'] = pointer_vector.tolist()
# FIXING delexicalization:
# dialogue = fixDelex(dialogue_name, dialogue, data2, idx, idx_acts)
idx_acts += 1
# ipdb.set_trace()
delex_data[dialogue_name] = dialogue
with open(os.path.join(DATA_DIR, 'multi-woz/lex.json'), 'w') as outfile:
json.dump(delex_data, outfile)
return delex_data
def get_action(actions, dial_name, turn_id):
turn_id = str(turn_id)
if turn_id in actions[dial_name.split('.')[0]]:
turn_action = actions[dial_name.split('.')[0]][turn_id]
if isinstance(turn_action, str):
return turn_action, []
acts = {}
for k, v in turn_action.items():
domain, act = [w.lower() for w in k.split('-')]
for (slot, value) in v:
slot = ' '.join(slot.lower().strip().split('\t'))
value = ' '.join(value.lower().strip().split('\t'))
# concat.extend(['_SEP1', v1, '_SEP2', v2])
if domain in acts and act in acts[domain] and slot in acts[domain][act]: # already domain-act is considered, skip
continue
if domain not in acts:
acts[domain] = {}
acts[domain][act] = {}
acts[domain][act] = [(slot, value)]
elif act not in acts[domain]:
acts[domain][act] = {}
acts[domain][act] = [(slot, value)]
else:
acts[domain][act].append((slot, value))
concat = []
for domain in acts:
for act in acts[domain]:
for slot, value in acts[domain][act]:
concat.append((domain, act, slot))
return turn_action, concat
else:
return [], []
def divideData(data, lexicalize=False):
"""Given test and validation sets, divide
the data for three different sets"""
# ipdb.set_trace()
testListFile = []
fin = open(os.path.join(DATA_DIR, 'multi-woz/testListFile.json'))
for line in fin:
testListFile.append(line[:-1])
fin.close()
valListFile = []
fin = open(os.path.join(DATA_DIR, 'multi-woz/valListFile.json'))
for line in fin:
valListFile.append(line[:-1])
fin.close()
trainListFile = open(os.path.join(DATA_DIR, 'multi-woz/trainListFile'), 'w')
actions = json.load(open('resources/multi-woz/dialogue_acts.json', 'r'))
test_dials = {}
val_dials = {}
train_dials = {}
# dictionaries
word_freqs_usr = OrderedDict()
word_freqs_sys = OrderedDict()
word_freqs_history = OrderedDict()
word_freqs_action = OrderedDict()
word_freqs_belief = OrderedDict()
for dialogue_name in tqdm(data):
dial = get_dial_raw_bstate(data[dialogue_name])
if dial:
dialogue = {}
dialogue['usr'] = []
dialogue['sys'] = []
dialogue['db'] = []
dialogue['bs'] = []
dialogue['bstate'] = []
dialogue['sys_act_raw'] = []
dialogue['sys_act'] = []
for turn_id, turn in enumerate(dial):
dialogue['usr'].append(turn[0])
dialogue['sys'].append(turn[1])
dialogue['db'].append(turn[2])
dialogue['bs'].append(turn[3])
dialogue['bstate'].append(turn[4])
turn_act_raw, turn_act = get_action(actions, dialogue_name, turn_id+1)
# ipdb.set_trace()
dialogue['sys_act_raw'].append(turn_act_raw)
dialogue['sys_act'].append(turn_act)
if dialogue_name in testListFile:
test_dials[dialogue_name] = dialogue
elif dialogue_name in valListFile:
val_dials[dialogue_name] = dialogue
else:
trainListFile.write(dialogue_name + '\n')
train_dials[dialogue_name] = dialogue
for turn in dial:
line = turn[0]
words_in = line.strip().split(' ')
for w in words_in:
if w not in word_freqs_usr:
word_freqs_usr[w] = 0
word_freqs_usr[w] += 1
# dialogue history vocab
for w in words_in:
if w not in word_freqs_history:
word_freqs_history[w] = 0
word_freqs_history[w] += 1
line = turn[1]
words_in = line.strip().split(' ')
for w in words_in:
if w not in word_freqs_sys:
word_freqs_sys[w] = 0
word_freqs_sys[w] += 1
# dialogue history vocab
for w in words_in:
if w not in word_freqs_history:
word_freqs_history[w] = 0
word_freqs_history[w] += 1
act_words = []
for dial_act in dialogue['sys_act']:
for domain, act, slot in dial_act:
act_words.extend([domain, act, slot])
for w in act_words:
if w not in word_freqs_sys:
word_freqs_sys[w] = 0
word_freqs_sys[w] += 1
if w not in word_freqs_history:
word_freqs_history[w] = 0
word_freqs_history[w] += 1
if w not in word_freqs_action:
word_freqs_action[w] = 0
word_freqs_action[w] += 1
belief_words = []
for dial_bstate in dialogue['bstate']:
for domain, slot, value in dial_bstate:
belief_words.extend([domain, slot])
belief_words.extend(normalize_beliefstate(value).strip().split(' '))
for w in belief_words:
if w not in word_freqs_sys:
word_freqs_sys[w] = 0
word_freqs_sys[w] += 1
if w not in word_freqs_history:
word_freqs_history[w] = 0
word_freqs_history[w] += 1
if w not in word_freqs_belief:
word_freqs_belief[w] = 0
word_freqs_belief[w] += 1
# save all dialogues
if lexicalize:
val_filename = os.path.join(DATA_DIR, 'val_dials_lexicalized.json')
test_filename = os.path.join(DATA_DIR, 'test_dials_lexicalized.json')
train_filename = os.path.join(DATA_DIR, 'train_dials_lexicalized.json')
else:
val_filename = os.path.join(DATA_DIR, 'val_dials.json')
test_filename = os.path.join(DATA_DIR, 'test_dials.json')
train_filename = os.path.join(DATA_DIR, 'train_dials.json')
with open(val_filename, 'w') as f:
json.dump(val_dials, f, indent=4)
with open(test_filename, 'w') as f:
json.dump(test_dials, f, indent=4)
with open(train_filename, 'w') as f:
json.dump(train_dials, f, indent=4)
return word_freqs_usr, word_freqs_sys, word_freqs_history
def buildDictionaries(word_freqs_usr, word_freqs_sys, word_freqs_histoy, lexicalize=False):
"""Build dictionaries for both user and system sides.
You can specify the size of the dictionary through DICT_SIZE variable."""
dicts = []
worddict_usr = createDict(word_freqs_usr)
dicts.append(worddict_usr)
worddict_sys = createDict(word_freqs_sys)
dicts.append(worddict_sys)
worddict_history = createDict(word_freqs_histoy)
dicts.append(worddict_history)
# reverse dictionaries
idx2words = []
for dictionary in dicts:
dic = {}
for k, v in dictionary.items():
dic[v] = k
idx2words.append(dic)
if lexicalize:
input_index2word_filename = os.path.join(DATA_DIR, 'input_lang.index2word_lexicalized.json')
input_word2index_filename = os.path.join(DATA_DIR, 'input_lang.word2index_lexicalized.json')
output_index2word_filename = os.path.join(DATA_DIR, 'output_lang.index2word_lexicalized.json')
output_word2index_filename = os.path.join(DATA_DIR, 'output_lang.word2index_lexicalized.json')
history_index2word_filename = os.path.join(DATA_DIR, 'history_lang.index2word_lexicalized.json')
history_word2index_filename = os.path.join(DATA_DIR, 'history_lang.word2index_lexicalized.json')
else:
input_index2word_filename = os.path.join(DATA_DIR, 'input_lang.index2word.json')
input_word2index_filename = os.path.join(DATA_DIR, 'input_lang.word2index.json')
output_index2word_filename = os.path.join(DATA_DIR, 'output_lang.index2word.json')
output_word2index_filename = os.path.join(DATA_DIR, 'output_lang.word2index.json')
history_index2word_filename = os.path.join(DATA_DIR, 'history_lang.index2word.json')
history_word2index_filename = os.path.join(DATA_DIR, 'history_lang.word2index.json')
with open(input_index2word_filename, 'w') as f:
json.dump(idx2words[0], f, indent=2)
with open(input_word2index_filename, 'w') as f:
json.dump(dicts[0], f, indent=2)
with open(output_index2word_filename, 'w') as f:
json.dump(idx2words[1], f, indent=2)
with open(output_word2index_filename, 'w') as f:
json.dump(dicts[1], f, indent=2)
with open(history_index2word_filename, 'w') as f:
json.dump(idx2words[2], f, indent=2)
with open(history_word2index_filename, 'w') as f:
json.dump(dicts[2], f, indent=2)
def main():
if sys.argv[1] == 'delex':
print('MultiWoz Create delexicalized dialogues. Get yourself a coffee, this might take a while.')
if not os.path.isfile(os.path.join(DATA_DIR, 'multi-woz/delex.json')):
data = createDelexData()
else:
data = json.load(open(os.path.join(DATA_DIR, 'multi-woz/delex.json')))
elif sys.argv[1] == 'lexical':
print('MultiWoz Create lexicalized dialogues. Get yourself a coffee, this might take a while.')
if not os.path.isfile(os.path.join(DATA_DIR, 'multi-woz/lex.json')):
data = createLexicalData()
else:
data = json.load(open(os.path.join(DATA_DIR, 'multi-woz/lex.json')))
else:
raise TypeError('unknown preprocessing')
print('Divide dialogues for separate bits - usr, sys, db, bs')
word_freqs_usr, word_freqs_sys, word_freqs_history = divideData(data,
lexicalize=(str(sys.argv[1])=='lexical'))
print('Building dictionaries')
buildDictionaries(word_freqs_usr, word_freqs_sys, word_freqs_history,
lexicalize=(str(sys.argv[1])=='lexical'))
if __name__ == "__main__":
main()