forked from Shikib/schema_attention_model
-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_readers.py
278 lines (233 loc) · 11 KB
/
data_readers.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
import copy
import json
import numpy as np
import os
import pickle
import torch
from collections import defaultdict
from torch.utils.data import Dataset
from tqdm import tqdm
def filter_dataset(dataset,
data_type="happy", # happy, unhappy, multitask
domain=None,
task=None,
exclude=False,
percentage=1.0,
train=True):
"""
Split the dataset according to the criteria
- data_type:
- happy: Only the happy dialogs
- unhappy: Only the happy + unhappy dialogs (no multitask)
- multitask: All the dialogs
- domain:
- Requirements:
- task should be None
- data_type should be happy/unhappy
- If exclude = True
- Exclude dialog of this domain
- If exclude = False
- Include ONLY dialogs of this domain
- task:
- Requirements:
- domain should be None
- data_type should be happy/unhappy
- If exclude = True
- Exclude dialog of this domain
- If exclude = False
- Include ONLY dialogs of this domain
- percentage:
- Take only a certain percentage of the available data (after filters)
- If train = True
- Take the first [percentage]% of the data
- If train = False:
- Take the last [percentage]% of the data
"""
examples = dataset.examples
# Filter based on happy/unhappy/multitask
if data_type == "happy":
examples = [ex for ex in examples if ex.get("happy")]
elif data_type == "unhappy":
examples = [ex for ex in examples if not ex.get("multitask")]
# Filter based on domain
if domain is not None:
assert data_type in ["happy", "unhappy"], "For zero-shot experiments, can only use single-task data"
assert task is None, "Can filter by domain OR task, not both"
if exclude:
examples = [ex for ex in examples if ex["domains"][0] != domain]
else:
examples = [ex for ex in examples if ex["domains"][0] == domain]
# Filter based on task
if task is not None:
assert data_type in ["happy", "unhappy"], "For zero-shot experiments, can only use single-task data"
assert domain is None, "Can filter by domain OR task, not both"
if exclude:
examples = [ex for ex in examples if ex["tasks"][0] != task]
else:
examples = [ex for ex in examples if ex["tasks"][0] == task]
# Split based on percentage
all_dialog_ids = sorted(list(set([ex['dialog_id'] for ex in examples])))
if train:
selected_ids = all_dialog_ids[:int(len(all_dialog_ids)*percentage)]
else:
selected_ids = all_dialog_ids[-int(len(all_dialog_ids)*percentage):]
selected_ids = set(selected_ids)
examples = [ex for ex in examples if ex['dialog_id'] in selected_ids]
# Filter out only the relevant keys for each example (so that DataLoader doesn't complain)
keys = ["input_ids", "attention_mask", "token_type_ids", "action", "tasks", "history", "response"]
examples = [{k:v for k,v in ex.items() if k in keys} for ex in examples]
for ex in examples:
ex["tasks"] = ex["tasks"][0]
# Return new dataset
new_dataset = copy.deepcopy(dataset)
new_dataset.examples = examples
return new_dataset
class NextActionSchema(Dataset):
def __init__(self,
data_path,
tokenizer,
max_seq_length,
action_label_to_id,
vocab_file_name):
# Check if cached pickle file exists
data_dirname = os.path.dirname(os.path.abspath(data_path))
cached_path = os.path.join(data_dirname, "schema_action_cached")
if os.path.exists(cached_path):
with open(cached_path, "rb") as f:
self.examples, self.action_to_response = pickle.load(f)
return None
# Read all of the JSON files in the data directory
tasks = [
json.load(open(data_path + fn + "/" + fn + ".json")) for fn in os.listdir(data_path)
]
self.action_to_response = {}
# Iterate over the schemas and get (1) the prior states and (2) the
# next actions.
node_to_utt = {}
self.examples = []
for task in tqdm(tasks):
# Get the graph
graph = task['graph']
# For every edge in the graph, get examples of transfer to each action
for prev,action in graph.items():
if False and prev in task['r_graph']:
sys_utt = '[wizard] ' + task['replies'][task['r_graph'][prev]] + ' [SEP] '
utterance = '[user] ' + sys_utt + task['replies'][prev] + ' [SEP]'
else:
utterance = '[user] ' + task['replies'][prev] + ' [SEP]'
node_to_utt[prev] = utterance
# For next action prediction, we can normalize the diff query types
#if action in ['query_check', 'query_book']:
# action = 'query'
if action not in action_label_to_id:
continue
action_label = action_label_to_id[action]
self.action_to_response[action_label] = task['replies'][action]
encoded = tokenizer.encode(utterance)
self.examples.append({
"input_ids": np.array(encoded.ids)[-max_seq_length:],
"attention_mask": np.array(encoded.attention_mask)[-max_seq_length:],
"token_type_ids": np.array(encoded.type_ids)[-max_seq_length:],
"action": action_label,
"task": task['task'],
})
with open(cached_path, "wb+") as f:
pickle.dump([self.examples, self.action_to_response], f)
def __len__(self):
return len(self.examples)
def __getitem__(self, idx):
return self.examples[idx]
class NextActionDataset(Dataset):
def __init__(self,
data_path,
tokenizer,
max_seq_length,
vocab_file_name):
# Check if cached pickle file exists
data_dirname = os.path.dirname(os.path.abspath(data_path))
cached_path = os.path.join(data_dirname, "action_cached")
if os.path.exists(cached_path):
with open(cached_path, "rb") as f:
self.action_label_to_id, self.examples = pickle.load(f)
return None
# Read all of the JSON files in the data directory
conversations = [
json.load(open(data_path + fn)) for fn in os.listdir(data_path)
]
# Iterate over the conversations and get (1) the dialogs and (2) the
# actions for all wizard turns.
self.examples = []
self.action_label_to_id = {}
for conv in tqdm(conversations):
# History (so far) for this dialog
history = ""
for i,utt in enumerate(conv['Events']):
# NOTE: Ground truth action labels only exist when wizard picks suggestion.
# We skip all custom utterances for action prediction.
if utt['Agent'] == 'Wizard' and utt['Action'] in ['query', 'pick_suggestion']:
# Tokenize history
processed_history = ' '.join(history.strip().split()[:-1])
encoded_history = tokenizer.encode(processed_history)
# Convert action label to id
query_label = 'query'
if 'ActionLabel' not in utt:
query_check = 'Check' in [e['RequestType'][1:-1] for e in utt['Constraints'] if 'RequestType' in e]
query_book = 'Book' in [e['RequestType'][1:-1] for e in utt['Constraints'] if 'RequestType' in e]
# In case of a bug, if both book and check are on - we treat it as a check.
if query_check:
query_label = 'query_check'
elif query_book:
query_label = 'query_book'
action_label = utt['ActionLabel'] if 'ActionLabel' in utt else query_label
if action_label not in self.action_label_to_id:
self.action_label_to_id[action_label] = len(self.action_label_to_id)
action_label_id = self.action_label_to_id[action_label]
# Include metadata
domains = conv['Scenario']['Domains']
tasks = [e['Task'] for e in conv['Scenario']['WizardCapabilities']]
happy = conv['Scenario']['Happy']
multitask = conv['Scenario']['MultiTask']
# Add to data
self.examples.append({
"input_ids": np.array(encoded_history.ids)[-max_seq_length:],
"attention_mask": np.array(encoded_history.attention_mask)[-max_seq_length:],
"token_type_ids": np.array(encoded_history.type_ids)[-max_seq_length:],
"action": action_label_id,
"dialog_id": conv['DialogueID'],
"domains": domains,
"tasks": tasks,
"happy": happy,
"multitask": multitask,
"orig_history": processed_history,
"orig_action": action_label,
})
# Process and concatenate to history
if utt['Agent'] in ['User', 'Wizard', 'KnowledgeBase']:
utt_text = ""
# If there's text, just use it directly
if utt['Action'] in ['pick_suggestion', 'utter']:
utt_text = utt['Text']
# If it's a knowledge base query, format it as a string
if utt['Action'] == 'query':
utt_text = "[QUERY] "
for constraint in utt['Constraints']:
key = list(constraint.keys())[0]
val = constraint[key]
utt_text += "{} = {} ; ".format(key, val)
# If it's a knowledge base item, format it as a string
if utt['Action'] == 'return_item':
utt_text = "[RESULT] "
if 'Item' not in utt:
utt_text += "NO RESULT"
else:
for key,val in utt['Item'].items():
utt_text += "{} = {} ; ".format(key, val)
if utt_text != "":
history += "[{}] {} [SEP] ".format(utt['Agent'], utt_text.strip())
# Write to cache
with open(cached_path, "wb+") as f:
pickle.dump([self.action_label_to_id, self.examples], f)
def __len__(self):
return len(self.examples)
def __getitem__(self, idx):
return self.examples[idx]