-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdata_utils.py
284 lines (229 loc) · 7.91 KB
/
data_utils.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
import dataclasses
import json
import random
from collections import defaultdict
from copy import deepcopy
from dataclasses import dataclass
from typing import List, Optional, Union
import numpy as np
import torch
from torch.utils.data import Dataset
from tqdm import tqdm
@dataclass
class OntologyDSTFeature:
guid: str
input_ids: List[int]
segment_ids: List[int]
num_turn: int
target_ids: Optional[List[int]]
@dataclass
class OpenVocabDSTFeature:
guid: str
input_id: List[int]
segment_id: List[int]
gating_id: List[int]
target_ids: Optional[Union[List[int], List[List[int]]]]
slot_positions: [List[int]] = None
domain_id: int = None
@dataclass
class DSTInputExample:
guid: str
context_turns: List[str]
current_turn: List[str]
label: Optional[List[str]] = None
domains: List[str] = None
def to_dict(self):
return dataclasses.asdict(self)
def to_json_string(self):
"""Serializes this instance to a JSON string."""
return json.dumps(self.to_dict(), indent=2) + "\n"
class WOSDataset(Dataset):
def __init__(self, features):
self.features = features
self.length = len(self.features)
def __len__(self):
return self.length
def __getitem__(self, idx):
return self.features[idx]
class DSTPreprocessor:
def __init__(self, slot_meta, src_tokenizer, trg_tokenizer=None, ontology=None):
self.slot_meta = slot_meta
self.src_tokenizer = src_tokenizer
self.trg_tokenizer = trg_tokenizer if trg_tokenizer else src_tokenizer
self.ontology = ontology
def pad_ids(self, arrays, pad_idx, max_length=-1):
if max_length < 0:
max_length = max(list(map(len, arrays)))
arrays = [
array + [pad_idx] * (max_length - min(len(array), 512)) for array in arrays
]
return arrays
def pad_id_of_matrix(self, arrays, padding, max_length=-1, left=False):
if max_length < 0:
max_length = max([array.size(-1) for array in arrays])
new_arrays = []
for i, array in enumerate(arrays):
n, l = array.size()
pad = torch.zeros(n, (max_length - l))
pad[
:,
:,
] = padding
pad = pad.long()
m = torch.cat([array, pad], -1)
new_arrays.append(m.unsqueeze(0))
return torch.cat(new_arrays, 0)
def _convert_example_to_feature(self):
raise NotImplementedError
def convert_examples_to_features(self):
raise NotImplementedError
def recover_state(self):
raise NotImplementedError
def load_dataset(dataset_path, dev_split=0.1):
data = json.load(open(dataset_path))
num_data = len(data)
num_dev = int(num_data * dev_split)
if not num_dev:
return data, [] # no dev dataset
dom_mapper = defaultdict(list)
for d in data:
dom_mapper[len(d["domains"])].append(d["guid"])
num_per_domain_trainsition = int(num_dev / 3)
dev_idx = []
for v in dom_mapper.values():
if len(v) < num_per_domain_trainsition:
continue
idx = random.sample(v, num_per_domain_trainsition)
dev_idx.extend(idx)
train_data, dev_data = [], []
for d in data:
if d["guid"] in dev_idx:
dev_data.append(d)
else:
train_data.append(d)
dev_labels = {}
for dialogue in dev_data:
d_idx = 0
guid = dialogue["guid"]
for idx, turn in enumerate(dialogue["dialogue"]):
if turn["role"] != "user":
continue
state = turn.pop("state")
guid_t = f"{guid}-{d_idx}"
d_idx += 1
dev_labels[guid_t] = state
return train_data, dev_data, dev_labels
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.device_count() > 0:
torch.cuda.manual_seed_all(seed)
def split_slot(dom_slot_value, get_domain_slot=False):
try:
dom, slot, value = dom_slot_value.split("-")
except ValueError:
tempo = dom_slot_value.split("-")
if len(tempo) < 2:
return dom_slot_value, dom_slot_value, dom_slot_value
dom, slot = tempo[0], tempo[1]
value = dom_slot_value.replace(f"{dom}-{slot}-", "").strip()
if get_domain_slot:
return f"{dom}-{slot}", value
return dom, slot, value
def build_slot_meta(data):
slot_meta = []
for dialog in data:
for turn in dialog["dialogue"]:
if not turn.get("state"):
continue
for dom_slot_value in turn["state"]:
domain_slot, _ = split_slot(dom_slot_value, get_domain_slot=True)
if domain_slot not in slot_meta:
slot_meta.append(domain_slot)
return sorted(slot_meta)
def convert_state_dict(state):
"""
:param state: list
:return: dict
dic[s] = v : s = domain-slot(관광-종류) , v = value(박물관)
"""
dic = {}
for slot in state:
s, v = split_slot(slot, get_domain_slot=True) # s = domain-slot(관광-종류) , v = value(박물관)
dic[s] = v # dic[관광-종류] = 박물관
return dic
def _truncate_seq_pair(tokens_a, tokens_b, max_length):
"""Truncates a sequence pair in place to the maximum length."""
# This is a simple heuristic which will always truncate the longer sequence
# one token at a time. This makes more sense than truncating an equal percent
# of tokens from each, since if one sequence is very short then each token
# that's truncated likely contains more information than a longer sequence.
while True:
total_length = len(tokens_a) + len(tokens_b)
if total_length <= max_length:
break
if len(tokens_a) > len(tokens_b):
tokens_a.pop()
else:
tokens_b.pop()
def get_examples_from_dialogue(dialogue, user_first=False):
guid = dialogue["guid"]
examples = []
history = []
d_idx = 0
domains = dialogue["domains"]
for idx, turn in enumerate(dialogue["dialogue"]):
if turn["role"] != "user":
continue
if idx:
sys_utter = dialogue["dialogue"][idx - 1]["text"]
else:
sys_utter = ""
user_utter = turn["text"]
state = turn.get("state")
context = deepcopy(history)
if user_first:
current_turn = [user_utter, sys_utter]
else:
current_turn = [sys_utter, user_utter]
examples.append(
DSTInputExample(
guid=f"{guid}-{d_idx}",
context_turns=context,
current_turn=current_turn,
label=state,
domains=domains,
)
)
history.append(sys_utter)
history.append(user_utter)
d_idx += 1
return examples
def get_examples_from_dialogues(data, user_first=False, dialogue_level=False):
examples = []
for d in tqdm(data):
example = get_examples_from_dialogue(d, user_first=user_first)
if dialogue_level:
examples.append(example)
else:
examples.extend(example)
return examples
def tokenize_ontology(ontology, tokenizer, max_seq_length=12):
slot_types = []
slot_values = []
for k, v in ontology.items():
tokens = tokenizer.encode(k)
if len(tokens) < max_seq_length:
gap = max_seq_length - len(tokens)
tokens.extend([tokenizer.pad_token_id] * gap)
slot_types.append(tokens)
slot_value = []
for vv in v:
tokens = tokenizer.encode(vv)
if len(tokens) < max_seq_length:
gap = max_seq_length - len(tokens)
tokens.extend([tokenizer.pad_token_id] * gap)
slot_value.append(tokens)
slot_values.append(torch.LongTensor(slot_value))
return torch.LongTensor(slot_types), slot_values