Datasets:
Languages:
English
Size:
10K<n<100K
Upload preprocess.py
Browse files- preprocess.py +97 -0
preprocess.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
from zipfile import ZipFile, ZIP_DEFLATED
|
4 |
+
import random
|
5 |
+
import json_lines
|
6 |
+
from collections import Counter
|
7 |
+
from shutil import rmtree
|
8 |
+
|
9 |
+
|
10 |
+
def preprocess():
|
11 |
+
random.seed(42)
|
12 |
+
|
13 |
+
ontology = {
|
14 |
+
'domains': {},
|
15 |
+
'intents': {},
|
16 |
+
'state': {},
|
17 |
+
"dialogue_acts": {
|
18 |
+
"categorical": {},
|
19 |
+
"non-categorical": {},
|
20 |
+
"binary": {}
|
21 |
+
}
|
22 |
+
}
|
23 |
+
|
24 |
+
dataset = 'metalwoz'
|
25 |
+
splits = ['train', 'validation', 'test']
|
26 |
+
dialogues_by_split = {split: [] for split in splits}
|
27 |
+
ZipFile('metalwoz-test-v1.zip').extract('dstc8_metalwoz_heldout.zip')
|
28 |
+
cnt = Counter()
|
29 |
+
for filename in ['metalwoz-v1.zip', 'dstc8_metalwoz_heldout.zip']:
|
30 |
+
with ZipFile(filename) as zipfile:
|
31 |
+
task_id2description = {x['task_id']: x for x in json_lines.reader(zipfile.open('tasks.txt'))}
|
32 |
+
for path in zipfile.namelist():
|
33 |
+
if path.startswith('dialogues'):
|
34 |
+
if filename == 'metalwoz-v1.zip':
|
35 |
+
split = random.choice(['train']*9+['validation'])
|
36 |
+
else:
|
37 |
+
split = 'test'
|
38 |
+
if split == 'validation':
|
39 |
+
print(path, split)
|
40 |
+
for ori_dialog in json_lines.reader(zipfile.open(path)):
|
41 |
+
dialogue_id = f'{dataset}-{split}-{len(dialogues_by_split[split])}'
|
42 |
+
domain = ori_dialog['domain']
|
43 |
+
|
44 |
+
task_des = task_id2description[ori_dialog['task_id']]
|
45 |
+
|
46 |
+
goal = {
|
47 |
+
'description': "user role: {}. user prompt: {}. system role: {}. system prompt: {}.".format(
|
48 |
+
task_des['user_role'], task_des['user_prompt'], task_des['bot_role'], task_des['bot_prompt']),
|
49 |
+
'inform': {},
|
50 |
+
'request': {}
|
51 |
+
}
|
52 |
+
|
53 |
+
dialogue = {
|
54 |
+
'dataset': dataset,
|
55 |
+
'data_split': split,
|
56 |
+
'dialogue_id': dialogue_id,
|
57 |
+
'original_id': ori_dialog['id'],
|
58 |
+
'domains': [domain], # will be updated by dialog_acts and state
|
59 |
+
'goal': goal,
|
60 |
+
'turns': []
|
61 |
+
}
|
62 |
+
|
63 |
+
ontology['domains'][domain] = {
|
64 |
+
'description': task_des['bot_role'],
|
65 |
+
'slots': {}
|
66 |
+
}
|
67 |
+
cnt[ori_dialog['turns'][0]] += 1
|
68 |
+
# assert ori_dialog['turns'][0] == "how may I help you?", print(ori_dialog['turns'])
|
69 |
+
for utt_idx, utt in enumerate(ori_dialog['turns'][1:]):
|
70 |
+
speaker = 'user' if utt_idx % 2 == 0 else 'system'
|
71 |
+
turn = {
|
72 |
+
'speaker': speaker,
|
73 |
+
'utterance': utt,
|
74 |
+
'utt_idx': utt_idx,
|
75 |
+
}
|
76 |
+
dialogue['turns'].append(turn)
|
77 |
+
|
78 |
+
dialogues_by_split[split].append(dialogue)
|
79 |
+
|
80 |
+
os.remove('dstc8_metalwoz_heldout.zip')
|
81 |
+
new_data_dir = 'data'
|
82 |
+
os.makedirs(new_data_dir, exist_ok=True)
|
83 |
+
dialogues = []
|
84 |
+
for split in splits:
|
85 |
+
dialogues += dialogues_by_split[split]
|
86 |
+
json.dump(dialogues[:10], open(f'dummy_data.json', 'w', encoding='utf-8'), indent=2, ensure_ascii=False)
|
87 |
+
json.dump(ontology, open(f'{new_data_dir}/ontology.json', 'w', encoding='utf-8'), indent=2, ensure_ascii=False)
|
88 |
+
json.dump(dialogues, open(f'{new_data_dir}/dialogues.json', 'w', encoding='utf-8'), indent=2, ensure_ascii=False)
|
89 |
+
with ZipFile('data.zip', 'w', ZIP_DEFLATED) as zf:
|
90 |
+
for filename in os.listdir(new_data_dir):
|
91 |
+
zf.write(f'{new_data_dir}/{filename}')
|
92 |
+
rmtree(new_data_dir)
|
93 |
+
return dialogues, ontology
|
94 |
+
|
95 |
+
|
96 |
+
if __name__ == '__main__':
|
97 |
+
preprocess()
|