-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoz_to_csp.py
174 lines (126 loc) · 7.04 KB
/
woz_to_csp.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
import argparse
import os
import sys
from main import *
DATASET_MAP = {'multiwoz2.1': 'MultiWOZ_21', 'multiwoz2.4': 'MultiWOZ_24', 'multiwoz_zh': 'MultiWOZ_ZH', 'crosswoz': 'CrossWOZ', 'crosswoz_en': 'CrossWOZ_EN', 'risawoz': 'RiSAWOZ'}
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--input_folder', type=str, help='input file to read from')
# mwoz2.1: ../../ConvLab-2/convlab2/dst/trade/multiwoz/data/
# mwoz2.4: ../../MultiWOZ2.4/data/mwz2.4/
# multiwoz_zh: ../../ConvLab-2/data/multiwoz_zh/
# crosswoz: ../../ConvLab-2/data/crosswoz/
# crosswoz_en: ../../ConvLab-2/data/crosswoz_en/
# risawoz: ../../RiSAWOZ/RiSAWOZ-data/task2-data-DST/
parser.add_argument('--output_folder', type=str, default='', help='output folder')
parser.add_argument('--experiment', type=str, choices=['multiwoz2.1', 'multiwoz2.4', 'multiwoz_zh', 'crosswoz', 'crosswoz_en', 'risawoz'], help='')
parser.add_argument('--ontology_folder', type=str, help='ontology folder')
# checkout project.config for list of ontology dirs
args = parser.parse_args()
os.makedirs(args.output_folder, exist_ok=True)
exp = eval(DATASET_MAP[args.experiment])()
splits = exp.get_splits()
ontology = None
if args.ontology_folder:
ontology = exp.get_ontology(os.path.join(args.ontology_folder, "ontology.json"))
for split in splits:
if 'train' in split:
out_split = 'train'
elif 'test' in split:
out_split = 'test'
else:
out_split = 'eval'
with open(args.output_folder + out_split + '.tsv', 'w') as fout:
previous_belief = 'null'
if exp.dlg_format == 'original':
data = ujson.load(open(os.path.join(args.input_folder, split)))
for dlg_id, dlg in data.items():
content = dlg[exp.messages]
turn_idx = 0
while turn_idx < len(content):
turn = content[turn_idx]
next_turn = dlg[exp.messages][turn_idx + 1]
previous_turn = None
if turn_idx > 0:
previous_turn = dlg[exp.messages][turn_idx - 1]
user_utterance = turn[exp.content]
if previous_turn:
sys_utterance = previous_turn[exp.content]
else:
sys_utterance = ''
cur_state = next_turn[exp.state]
new_slots = exp.process_state(cur_state, ontology=ontology)
user_utterance = exp.process_user_uttr(user_utterance)
sys_utterance = exp.process_user_uttr(sys_utterance)
belief_state = exp.state_dict2text(new_slots)
out_string = f'{dlg_id}/{int(turn_idx / 2)}\t{previous_belief} ;{" " + sys_utterance if sys_utterance else ""}\t{user_utterance}\t{belief_state}\n'
fout.write(out_string)
previous_belief = belief_state
turn_idx += 2
elif exp.dlg_format == 'sgd':
data = ujson.load(open(os.path.join(args.input_folder, split)))
for dlg in data:
content = dlg['turns']
dlg_id = dlg['dialogue_id']
turn_idx = 0
while turn_idx < len(content):
turn = content[turn_idx]
previous_turn = None
if turn_idx > 0:
previous_turn = content[turn_idx - 1]
assert turn['speaker'] == 'user'
user_utterance = turn['utterance']
if previous_turn:
assert previous_turn['speaker'] == 'system'
sys_utterance = previous_turn['utterance']
else:
sys_utterance = ''
cur_state = turn['state']
new_slots = exp.process_state(cur_state, ontology=ontology)
user_utterance = exp.process_user_uttr(user_utterance)
sys_utterance = exp.process_user_uttr(sys_utterance)
belief_state = exp.state_dict2text(new_slots)
out_string = f'{dlg_id}/{int(turn_idx / 2)}\t{previous_belief} ;{" " + sys_utterance if sys_utterance else ""}\t{user_utterance}\t{belief_state}\n'
fout.write(out_string)
previous_belief = belief_state
turn_idx += 2
elif exp.dlg_format == 'processed':
data = ujson.load(open(args.input_folder + split, 'r'))
for dlg in data:
dlg_id = dlg['dialogue_idx']
content = dlg['dialogue']
turn_idx = 0
while turn_idx < len(content):
turn = content[turn_idx]
cur_state = turn['belief_state']
new_slots = exp.process_state(cur_state, ontology=ontology)
user_utterance = exp.process_user_uttr(turn["transcript"])
sys_utterance = exp.process_user_uttr(turn["system_transcript"])
belief_state = exp.state_dict2text(new_slots)
out_string = f'{dlg_id}/{turn_idx}\t{previous_belief} ;{" " + sys_utterance if sys_utterance else ""}\t{user_utterance}\t{belief_state}\n'
fout.write(out_string)
previous_belief = belief_state
turn_idx += 1
# star means tsv format (sumbt, etc.)
elif exp.dlg_format == 'star':
data = open(args.input_folder + split, 'r')
previous_last_turn = 'False'
for i, dlg in enumerate(data):
# header
if i == 0:
_, _, _, _, _, *slots_names = dlg.split('\t')
continue
dlg_id, turn_num, last_turn, sys_utterance, user_utterance, *slots_values = dlg.strip('\n').split('\t')
if previous_last_turn == 'True':
previous_belief = 'null'
new_slots = exp.process_state(list(zip(slots_names, slots_values)), ontology=ontology)
user_utterance = exp.process_user_uttr(user_utterance)
sys_utterance = exp.process_user_uttr(sys_utterance)
belief_state = exp.state_dict2text(new_slots)
out_string = f'{dlg_id}/{turn_num}\t{previous_belief} ;{" " + sys_utterance if sys_utterance else ""}\t{user_utterance}\t{belief_state}\n'
fout.write(out_string)
previous_belief = belief_state
previous_last_turn = last_turn
turn_idx += 1
if __name__ == '__main__':
main()