-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.py
344 lines (286 loc) · 12.9 KB
/
data.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
import os
from io import open
import torch
import json
from torch.utils.data import DataLoader, Dataset
from glob import glob
import numpy as np
import utils
import pandas as pd
from tqdm import tqdm
class Dictionary(object):
def __init__(self, path):
self.word2idx = {}
self.idx2word = []
self.bos_token = '<bos>'
self.eos_token = '<eos>'
self.pad_token = '<pad>'
self.unk_token = '<unk>'
self.newline_token = '\n'
self.build_dict(path)
def add_word(self, word):
if word not in self.word2idx:
self.idx2word.append(word)
self.word2idx[word] = len(self.idx2word) - 1
return self.word2idx[word]
def build_dict(self, path):
for fle in glob(os.path.join(path, "*", "*")):
with open(fle, 'r', encoding="utf8") as f:
for line in f:
line = line.rstrip('\n')
words = line.split()
for word in words:
self.dictionary.add_word(word)
vocabs = list(self.dictionary.items())
# shuffle vocabs
import numpy.random as random
random.seed(0)
random.shuffle(vocabs)
# reset dict
self.reset_dict()
# add special tokens to the beginning
self.dictionary.add_word(self.pad_token)
self.dictionary.add_word(self.bos_token)
self.dictionary.add_word(self.eos_token)
self.dictionary.add_word(self.unk_token)
self.dictionary.add_word(self.newline_token)
for word in vocabs:
self.dictionary.add_word(word)
assert self.encode(self.pad_token) == [0]
def encode(self, text):
words = text.split()
if text.endswith('\n'):
words += [self.word2idx['\n']]
idxs = []
for w in words:
idxs.append(self.word2idx[w])
return idxs
def decode(self, idxs):
words = []
for idx in idxs:
words.append(self.idx2word[idx])
return " ".join(words)
def reset_dict(self):
self.word2idx = {}
self.idx2word = []
def __len__(self):
return len(self.idx2word)
class Corpus(object):
def __init__(self, path, tokenizer=None):
if tokenizer:
self.tokenizer = tokenizer
else:
self.tokenizer = None
self.dictionary = Dictionary()
self.train = self.tokenize(os.path.join(path, 'train/train.txt'))
self.valid = self.tokenize(os.path.join(path, 'valid/valid.txt'))
self.test = self.tokenize(os.path.join(path, 'test/test.txt'))
def tokenize(self, path, insert=None):
"""Tokenizes a text file."""
assert os.path.exists(path)
if not self.tokenizer:
# Add words to the dictionary
with open(path, 'r', encoding="utf8") as f:
for line in f:
words = line.split() + ['<eos>']
for word in words:
self.dictionary.add_word(word)
# Tokenize file content
with open(path, 'r', encoding="utf8") as f:
idss = []
for line in f:
words = line.split() + ['<eos>']
ids = []
for word in words:
ids.append(self.dictionary.word2idx[word])
idss.append(torch.tensor(ids).type(torch.int64))
ids = torch.cat(idss)
else:
end_token_id = self.tokenizer.encode(self.tokenizer.eos_token)
# Tokenize file content
with open(path, 'r', encoding="utf8") as f:
idss = []
for line in f:
ids = self.tokenizer(line)['input_ids'] + end_token_id
idss.append(torch.tensor(ids).type(torch.int64))
ids = torch.cat(idss)
return ids
class CorpusDataset(Dataset):
def __init__(self, path, tokenizer, bsz, bptt):
self.path = path
self.tokenizer = tokenizer
self.bsz = bsz
self.bptt = bptt
self.data = self.build_data(path)
def build_data( self, path):
assert self.tokenizer.bos_token == self.tokenizer.eos_token
# start_token_id = self.tokenizer.encode(self.tokenizer.bos_token)
end_token_id = self.tokenizer.encode(self.tokenizer.eos_token)
token_ids = []
for fle in glob(os.path.join(path, '*')):
with open(fle, 'r') as fh:
for line in fh:
line = line.rstrip('\n')
line_token_ids = self.tokenizer.encode(line) + end_token_id
token_ids.extend(line_token_ids)
nbatch = len(token_ids) // self.bsz
# Trim off any extra elements that wouldn't cleanly fit (remainders).
token_ids = token_ids[:(nbatch * self.bsz)]
# Evenly divide the data across the bsz batches.
token_ids = np.array(token_ids).reshape((self.bsz, -1)).transpose() # [-1, bsz]
sequences = []
for i in range(0, len(token_ids) - 1, self.bptt):
# data, targets = get_batch(data_source, i)
seq_len = min(self.bptt, len(token_ids) - 1 - i)
sequence = token_ids[i:i+seq_len+1].transpose()
sequences += sequence.tolist()
return sequences
def __len__(self):
return len(self.data)
def __getitem__(self, index):
sequences = self.data[index]
return sequences
# return torch.tensor(sequences).type(torch.int64)
def collate(self, unpacked_data):
return unpacked_data
class CorpusPartialDPDataset(CorpusDataset):
def __init__(self, path, tokenizer, bsz, bptt, is_private_func, missing_digits=False):
self.is_private_func = is_private_func
self.missing_digits = missing_digits
self.num_canary = 0
self.total_tokens = 0
self.private_tokens = 0
super().__init__(path, tokenizer, bsz, bptt)
print(pd.Series([len(d[-1]) for d in self.data]).value_counts())
print(f"# tokens: {self.total_tokens}")
print(f"# private tokens: {self.private_tokens}")
print(f"private ratio: {self.private_tokens/self.total_tokens}")
def build_data(self, path):
assert self.tokenizer.bos_token == self.tokenizer.eos_token # only if bos = eos, can we add eos only without adding bos below in line_token_ids = self.tokenizer.encode(line) + end_token_id
# start_token_id = self.tokenizer.encode(self.tokenizer.bos_token)
end_token_id = self.tokenizer.encode(self.tokenizer.eos_token)
if self.missing_digits:
canary_digits_token_ids = self.tokenizer.encode(utils.CANARY_DIGITS)
token_ids = []
for fle in glob(os.path.join(path, '*')):
with open(fle, 'r') as fh:
for line in fh:
line = line.rstrip('\n')
line_token_ids = self.tokenizer.encode(line) + end_token_id # because bos = eos
token_ids.extend(line_token_ids)
nbatch = len(token_ids) // self.bsz
# Trim off any extra elements that wouldn't cleanly fit (remainders).
token_ids = token_ids[:(nbatch * self.bsz)]
# Evenly divide the data across the bsz batches.
token_ids = np.array(token_ids).reshape((self.bsz, -1)).transpose() # [-1, bsz]
sequences = []
texts = []
is_privates = []
split_sequences = []
for i in range(0, len(token_ids) - 1, self.bptt):
# data, targets = get_batch(data_source, i)
seq_len = min(self.bptt, len(token_ids) - 1 - i)
sequence = token_ids[i:i+seq_len+1].transpose()
cur_sequences = sequence.tolist()
sequences += cur_sequences
cur_texts = []
cur_is_privates = []
cur_split_sequences = []
for seq in cur_sequences:
split_text = [self.tokenizer.decode(tok) for tok in seq]
cur_texts.append(split_text)
is_private = self.is_private_func(split_text)
if self.missing_digits:
# we need to miss the inserted canary digits
is_sub = utils.is_sub(canary_digits_token_ids, seq)
if is_sub:
print("missing digits")
self.num_canary += 1
assert all(is_private[is_sub[0]:is_sub[1]])
for _i in range(is_sub[0], is_sub[1]):
is_private[_i] = 0
assert all([_p ==0 for _p in is_private[is_sub[0]:is_sub[1]]])
cur_is_privates.append(is_private)
self.total_tokens += len(is_private)
self.private_tokens += sum(is_private)
split_seq = utils.split_is_private(is_private, seq)
cur_split_sequences.append(split_seq)
texts += cur_texts
is_privates += cur_is_privates
split_sequences += cur_split_sequences
return list(zip(sequences, texts, is_privates, split_sequences))
def __getitem__(self, index):
tok_ids, texts, is_privates, split_sequence = self.data[index]
return split_sequence
class CustomerDataset(Dataset):
def __init__(self, path, tokenizer):
self.path = path
self.tokenizer = tokenizer
self.data = self.build_data(path)
def build_data(self, path):
# bos_id = self.tokenizer.encode(self.tokenizer.bos_token)
eos_id = self.tokenizer.encode(self.tokenizer.eos_token)
dials = []
tokens = []
for fle in glob(os.path.join(path, '*')):
with open(fle, 'r') as fh:
lines = fh.read()
dial = lines.strip().split("\n")
dials.append(dial)
dial_tokens = [self.tokenizer.encode(turn) for turn in dial]
flat_dial_tokens = [turn_tokens for turn in dial_tokens for turn_tokens in turn]
flat_dial_tokens = flat_dial_tokens + eos_id
tokens.append(flat_dial_tokens)
return list(zip(dials, tokens))
def __len__(self):
return len(self.data)
def __getitem__(self, index):
dial, tokens = self.data[index]
return tokens
# return torch.tensor(flat_dial_tokens).type(torch.int64)
def collate(self, unpacked_data):
return unpacked_data
class CustomerPartialDPDataset(CustomerDataset):
def __init__(self, path, tokenizer, is_private_func):
self.is_private_func = is_private_func
self.total_tokens = 0
self.private_tokens = 0
super().__init__(path, tokenizer)
print(pd.Series([len(d[-1]) for d in self.data]).value_counts())
print(f"# tokens: {self.total_tokens}")
print(f"# private tokens: {self.private_tokens}")
print(f"private ratio: {self.private_tokens/self.total_tokens}")
def build_data(self, path):
dials = []
texts = []
is_privates = []
split_sequences = []
eos_id = self.tokenizer.encode(self.tokenizer.eos_token)
for fle in tqdm(glob(os.path.join(path, '*'))):
with open(fle, 'r') as fh:
lines = fh.read()
dial = lines.strip().split("\n")
dials.append(dial)
dial_tokens = [self.tokenizer.encode(turn) for turn in dial]
flat_dial_tokens = [turn_tokens for turn in dial_tokens for turn_tokens in turn]
flat_dial_tokens = flat_dial_tokens + eos_id
split_text = [self.tokenizer.decode(tok) for tok in flat_dial_tokens]
flat_split_text = [self.tokenizer.decode(tok) for tok in flat_dial_tokens]
texts.append(flat_split_text)
if utils.CANARY_CONTENT in lines:
# for the inserted canary My ID is 341752. utils.private_token_classifier is not able to extract it, so use utils.is_digit
is_private = utils.is_digit(split_text)
else:
is_private = self.is_private_func(dialog=lines, domain="track_package", tokenizer=self.tokenizer, dial_tokens=dial_tokens, verbose=False) + [0] # the last 0 for the eos_id
is_privates.append(is_private)
self.total_tokens += len(is_private)
self.private_tokens += sum(is_private)
assert len(is_private) == len(flat_dial_tokens)
split_seq = utils.split_is_private(is_private, flat_dial_tokens)
split_sequences.append(split_seq)
# if "3826" in fle:
# import pdb; pdb.set_trace()
return list(zip(dials, texts, is_privates, split_sequences))
def __getitem__(self, index):
dial, texts, is_private, split_sequence = self.data[index]
return split_sequence