-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
213 lines (162 loc) · 6.17 KB
/
dataset.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
import torch
from torch.utils.data import Dataset
import re
from nltk.tokenize import TweetTokenizer
class DialogLMDataset(Dataset):
def __init__(
self,
df,
src_col,
src_eos_token,
tokenizer,
tgt_col = None,
):
self.tokenizer = tokenizer
self.train_mode = False
self.tgts = None
if tgt_col is not None:
self.train_mode = True
len_df = len(df)
srcs = []
tgts = []
num_proc = 0
for i, row in df.iterrows():
if num_proc % 1000 == 0:
print(f'\rProcessing row: {num_proc} of {len_df}', end='', flush=True)
num_proc += 1
src = DialogLMDataset.preprocess_text_minimal(
txt = str(row[src_col]),
dataset_eos_token = src_eos_token,
tokenizer_eos_token = self.tokenizer.eos_token
)
src_enc = tokenizer.encode(src)
srcs.append(src_enc)
# preprocess and tokenize tgt col if available (usually for training)
if tgt_col is not None:
tgt = DialogLMDataset.preprocess_text_minimal(row[tgt_col])
tgt_enc = tokenizer.encode(tgt)
tgts.append(tgt_enc)
# save
self.srcs = srcs
self.tgts = tgts
@staticmethod
def preprocess_text_minimal(
txt,
dataset_eos_token = 'EOS',
tokenizer_eos_token = '<|endoftext|>'
):
# remove "title : " prefixes
if txt[:8] == 'title : ':
txt = txt[8:]
# replace 'EOS' with tokenizer's EOS
if dataset_eos_token != tokenizer_eos_token:
txt_utterance_split = txt.split(dataset_eos_token)
txt = tokenizer_eos_token.join([s.strip() for s in txt_utterance_split])
return txt
@staticmethod
def preprocess_text(
txt,
dataset_eos_token = 'EOS',
tokenizer_eos_token = '<|endoftext|>'
):
# remove "title : " prefixes
if txt[:8] == 'title : ':
txt = txt[8:]
txt = str(txt).lower()
# url and tag
words = []
for word in txt.split():
if word[0] == '#': # don't allow tag
continue
i = word.lower().find('http')
if i >= 0:
word = word[:i] + ' ' + '__url__'
words.append(word.strip())
txt = ' '.join(words)
# remove illegal char
txt = txt.replace(chr(92),'') # chr(92) = '\'. as twitter has 'b\/c' rather than 'b/c'
txt = txt.replace("b/c","because").replace('j/k','just kidding').replace('w/o','without').replace('w/','with')
txt = re.sub('__mention__','MENTION',txt)
txt = re.sub('__url__','URL',txt)
txt = re.sub(r"[^A-Za-z0-9()\[\]:,.!?'“” ]", " ", txt)
txt = re.sub('MENTION','__mention__',txt)
txt = re.sub('URL','__url__',txt)
tokenizer = TweetTokenizer(preserve_case=True)
txt = ' ' + ' '.join(tokenizer.tokenize(txt)) + ' '
# remove un-necessary space
txt = ' '.join(txt.split())
# replace 'EOS' with tokenizer's EOS
if dataset_eos_token != tokenizer_eos_token:
txt_utterance_split = txt.split(dataset_eos_token.lower())
txt = tokenizer_eos_token.join([s.strip() for s in txt_utterance_split])
return txt
def __len__(self):
return len(self.srcs)
def __getitem__(self, index):
'''
__getitem__ runs on 1 example at a time. Here, we get an example at index and return its numericalized source and
target values using the vocabulary objects we created in __init__
'''
if self.train_mode:
return {'src': self.srcs[index], 'tgt': self.tgts[index]}
else:
return {'src': self.srcs[index], 'tgt': None}
class CausalLMDataset(Dataset):
def __init__(
self,
df,
text_col,
bos_token = '<|startoftext|>',
eos_token = '<|endoftext|>'
):
"""
A simple dataset for self-supervised Causal Language Modeling.
"""
self.train_mode = True
self.eos_token = eos_token
self.bos_token = bos_token
len_df = len(df)
texts = []
num_proc = 0
for i, row in df.iterrows():
if num_proc % 1000 == 0:
print(f'\rProcessing row: {num_proc} of {len_df}', end='', flush=True)
num_proc += 1
text = CausalLMDataset.preprocess_text(row[text_col])
text = self.bos_token + text + self.eos_token
texts.append(text)
# save
self.texts = texts
@staticmethod
def preprocess_text(txt):
txt = str(txt).lower()
# url and tag
words = []
for word in txt.split():
if word[0] == '#': # don't allow tag
continue
i = word.lower().find('http')
if i >= 0:
word = word[:i] + ' ' + '__url__'
words.append(word.strip())
txt = ' '.join(words)
# remove illegal char
txt = txt.replace(chr(92),'') # chr(92) = '\'. as twitter has 'b\/c' rather than 'b/c'
txt = txt.replace("b/c","because").replace('j/k','just kidding').replace('w/o','without').replace('w/','with')
txt = re.sub('__mention__','MENTION',txt)
txt = re.sub('__url__','URL',txt)
txt = re.sub(r"[^A-Za-z0-9()\[\]:,.!?'“” ]", " ", txt)
txt = re.sub('MENTION','__mention__',txt)
txt = re.sub('URL','__url__',txt)
tokenizer = TweetTokenizer(preserve_case=True)
txt = ' ' + ' '.join(tokenizer.tokenize(txt)) + ' '
# remove un-necessary space
return ' '.join(txt.split())
def __len__(self):
return len(self.texts)
def __getitem__(self, index):
'''
__getitem__ runs on 1 example at a time. Here, we get an example at index and return its numericalized source and
target values using the vocabulary objects we created in __init__
'''
return {'text': self.texts[index]}