-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_datasets.py
229 lines (194 loc) · 8.72 KB
/
text_datasets.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
import datasets
from datasets import load_dataset
import torch
from torch.utils.data import DataLoader,Dataset
import re,json,os
from tqdm import tqdm
class Dictionary:
"""dictionary:
1. load dataset: create a map, from word to index; process the dataset into a list of indices
2. encode line: convert a line to indices
"""
def __init__(self,max_size=50000):
self.vocab = {
'<pad>':float('inf'),
'<bos>':float('inf'),
'<eos>':float('inf'),
'<unk>':float('inf')
}
self.max_size = max_size
self.init_finished = False
def tokenize(self,sentence):
raise NotImplementedError()
def add(self,sentence):
tokens = self.tokenize(sentence)
for token in tokens:
if token in self.vocab:
self.vocab[token] += 1
else:
self.vocab[token] = 1
def encode_ln(self,line):
assert self.init_finished
main = [self.vocab.get(word,self.unk) for word in self.tokenize(line)]
return [self.bos]+main+[self.eos]
def finish_init(self):
print('init finished with {} words'.format(len(self.vocab)))
self.init_finished = True
self.reverse_vocab = {idx:word for word,idx in self.vocab.items()}
def save(self,path=None):
if path is None:
if not hasattr(self,'save_path'):
raise ValueError('save path not specified')
path = self.save_path
if not self.init_finished:
l = [(times,word) for word,times in self.vocab.items()]
l = sorted(l[:self.max_size],reverse=True)
self.vocab = {word:idx for idx,(_,word) in enumerate(l)}
self.finish_init()
json.dump(self.vocab, open(path, 'w'),indent=4)
def load(self,path=None):
self.vocab = json.load(open(path, 'r'))
if len(self.vocab)>self.max_size:
self.vocab = {word:ind for word,ind in self.vocab.items() if ind<self.max_size}
assert all([x in self.vocab for x in ['<pad>','<bos>','<eos>','<unk>']]),'Invalid dictionary file'
self.finish_init()
def decode_ln(self,indices):
assert self.init_finished
if isinstance(indices,torch.Tensor):
assert len(indices.shape)==1,'Does not accept batch input.'
indices = indices.long().detach().cpu().tolist()
return ' '.join(self.reverse_vocab.get(idx,self.unk) for idx in indices)
def __len__(self):
return len(self.vocab)
@property
def bos(self): assert self.init_finished; return self.vocab['<bos>']
@property
def eos(self): assert self.init_finished; return self.vocab['<eos>']
@property
def pad(self): assert self.init_finished; return self.vocab['<pad>']
@property
def unk(self): assert self.init_finished; return self.vocab['<unk>']
class ZH_Dictionary(Dictionary):
def __init__(self, max_size=50000):
super().__init__(max_size)
self.save_path = f'../data/zh_dict_max{max_size}.json'
def tokenize(self,sentence):
return re.findall(r'([^\da-z]|[\da-z]+)', sentence.lower())
def save(self,path=None):
if path is None:
if not hasattr(self,'save_path'):
raise ValueError('save path not specified')
path = self.save_path
if not self.init_finished:
l = [(times,word) for word,times in self.vocab.items()]
l = sorted(l[:self.max_size],reverse=True)
self.vocab = {word:idx for idx,(_,word) in enumerate(l)}
self.finish_init()
json.dump(self.vocab, open(path, 'w',encoding='utf-8'),ensure_ascii=False,indent=4)
def load(self,path=None):
with open(path, 'r', encoding='utf-8') as f:
self.vocab = json.load(f)
if len(self.vocab)>self.max_size:
self.vocab = {word:ind for word,ind in self.vocab.items() if ind<self.max_size}
assert all([x in self.vocab for x in ['<pad>','<bos>','<eos>','<unk>']]),'Invalid dictionary file'
self.finish_init()
def decode_ln(self,indices):
assert self.init_finished
if isinstance(indices,torch.Tensor):
assert len(indices.shape)==1,'Does not accept batch input.'
indices = indices.long().detach().cpu().tolist()
return ''.join(self.reverse_vocab.get(idx,self.unk) for idx in indices)
class EN_Dictionary(Dictionary):
def __init__(self, max_size=50000):
super().__init__(max_size)
self.save_path = f'../data/en_dict_max{max_size}.json'
def tokenize(self,sentence):
return re.findall(r'\b\w+\b|[^\w\s]', sentence.lower())
class WMT19Dataset(Dataset):
def __init__(self,dataset:datasets.arrow_dataset.Dataset,load=False,en_dict = None,zh_dict = None,vocab_size=20000,pick_num=None):
if pick_num is not None:
dataset = dataset.select(range(pick_num))
self.dataset = dataset
if load:
print('Using preloaded dictionary.')
en_dict = EN_Dictionary(max_size=vocab_size)
en_dict.load(en_dict.save_path)
self.en_dict = en_dict
zh_dict = ZH_Dictionary(max_size=vocab_size)
zh_dict.load(zh_dict.save_path)
self.zh_dict = zh_dict
return
if en_dict is not None and zh_dict is not None:
self.en_dict = en_dict
self.zh_dict = zh_dict
return
self.en_dict = EN_Dictionary()
self.zh_dict = ZH_Dictionary()
def pre_process(one):
self.en_dict.add(' '.join(x['en'] for x in one['translation']))
self.zh_dict.add(''.join(x['zh'] for x in one['translation']))
dataset.map(pre_process, batched=True)
self.en_dict.save()
self.zh_dict.save()
def __len__(self):
return len(self.dataset)
def __getitem__(self, idx):
raw = self.dataset[idx]
# print(raw)
en = self.en_dict.encode_ln(raw['translation']['en'])
zh = self.zh_dict.encode_ln(raw['translation']['zh'])
return en,zh
class WMT19DataLoader(DataLoader):
def __init__(self,dataset:Dataset,batch_size=32,shuffle=True):
self.dataset = dataset
super().__init__(self.dataset,batch_size=batch_size,shuffle=shuffle,collate_fn=self.collate_fn)
def collate_fn(self,batch):
en,zh = zip(*batch)
en_len = [len(x) for x in en]
zh_len = [len(x) for x in zh]
en = [x+[self.dataset.en_dict.pad]*(max(en_len)-len(x)) for x in en]
zh = [x+[self.dataset.zh_dict.pad]*(max(zh_len)-len(x)) for x in zh]
en = torch.tensor(en)
zh = torch.tensor(zh)
if en.shape[1]>100:
en = en[:,:100]
if zh.shape[1]>100:
zh = zh[:,:100]
return en,zh
class WMT19:
def __init__(self,batch_size=32,vocab_size=20000) -> None:
print('Initializing WMT dataset with batch size',batch_size)
assert os.path.exists('../data/wmt19'),'Please download the dataset at https://huggingface.co/datasets/wmt/wmt19/tree/main/zh-en'
data_files = {
"train": ["../data/wmt19/train-000{:02d}-of-00013.parquet".format(i) for i in range(13)],
"validation": "../data/wmt19/validation-00000-of-00001.parquet"
}
wmt19 = load_dataset('parquet', data_files=data_files)
print('WMT19 loaded. train dataset len:', len(wmt19['train']))
# raise NotImplementedError()
self.train_dataset = WMT19Dataset(wmt19['train'],load=True,vocab_size=vocab_size)
self.valid_dataset = WMT19Dataset(wmt19['validation'],en_dict=self.train_dataset.en_dict,zh_dict=self.train_dataset.zh_dict,vocab_size=vocab_size,pick_num=1000)
self.train_dataloader = WMT19DataLoader(self.train_dataset,batch_size=batch_size)
self.valid_dataloader = WMT19DataLoader(self.valid_dataset,batch_size=batch_size)
self.src_dict = self.train_dataset.en_dict
self.tgt_dict = self.train_dataset.zh_dict
print('example:',self.valid_dataset.dataset[-1]['translation'])
def decode(self,indices,lang):
if lang == 'en':
return self.train_dataset.en_dict.decode_ln(indices)
else:
return self.train_dataset.zh_dict.decode_ln(indices)
if __name__ == '__main__':
# wmt19 = WMT19()
# train_dl = wmt19.train_dataloader
# valid_dl = wmt19.valid_dataloader
# en,zh = next(iter(train_dl))
# zh_dict = ZH_Dictionary(max_size=20000)
# zh_dict.load('../data/zh_dict_max50000.json')
# zh_dict.save('../data/zh_dict_max20000.json')
# en_dict = EN_Dictionary(max_size=20000)
# en_dict.load('../data/en_dict_max50000.json')
# en_dict.save('../data/en_dict_max20000.json')
wmt19 = WMT19()
for en,zh in wmt19.train_dataloader:
pass