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
|
from bisect import bisect_right
from copy import copy
import numpy as np
import random
import logging
import re
import torch
from .vocab import Vocab
logger = logging.getLogger('stanza')
def filter_consecutive_whitespaces(para):
filtered = []
for i, (char, label) in enumerate(para):
if i > 0:
if char == ' ' and para[i-1][0] == ' ':
continue
filtered.append((char, label))
return filtered
NEWLINE_WHITESPACE_RE = re.compile(r'\n\s*\n')
NUMERIC_RE = re.compile(r'^([\d]+[,\.]*)+$')
WHITESPACE_RE = re.compile(r'\s')
class DataLoader:
def __init__(self, args, input_files={'txt': None, 'label': None}, input_text=None, input_data=None, vocab=None, evaluation=False, dictionary=None):
self.args = args
self.eval = evaluation
self.dictionary = dictionary
# get input files
txt_file = input_files['txt']
label_file = input_files['label']
# Load data and process it
if input_data is not None:
self.data = input_data
else:
# set up text from file or input string
assert txt_file is not None or input_text is not None
if input_text is None:
with open(txt_file) as f:
text = ''.join(f.readlines()).rstrip()
else:
text = input_text
if label_file is not None:
with open(label_file) as f:
labels = ''.join(f.readlines()).rstrip()
else:
labels = '\n\n'.join(['0' * len(pt.rstrip()) for pt in NEWLINE_WHITESPACE_RE.split(text)])
skip_newline = args.get('skip_newline', False)
self.data = [[(WHITESPACE_RE.sub(' ', char), int(label)) # substitute special whitespaces
for char, label in zip(pt.rstrip(), pc) if not (skip_newline and char == '\n')] # check if newline needs to be eaten
for pt, pc in zip(NEWLINE_WHITESPACE_RE.split(text), NEWLINE_WHITESPACE_RE.split(labels)) if len(pt.rstrip()) > 0]
# remove consecutive whitespaces
self.data = [filter_consecutive_whitespaces(x) for x in self.data]
self.vocab = vocab if vocab is not None else self.init_vocab()
# data comes in a list of paragraphs, where each paragraph is a list of units with unit-level labels.
# At evaluation time, each paragraph is treated as single "sentence" as we don't know a priori where
# sentence breaks occur. We make prediction from left to right for each paragraph and move forward to
# the last predicted sentence break to start afresh.
self.sentences = [self.para_to_sentences(para) for para in self.data]
self.init_sent_ids()
logger.debug(f"{len(self.sentence_ids)} sentences loaded.")
def has_mwt(self):
# presumably this only needs to be called either 0 or 1 times,
# 1 when training and 0 any other time, so no effort is put
# into caching the result
for sentence in self.data:
for word in sentence:
if word[1] > 2:
return True
return False
def init_vocab(self):
vocab = Vocab(self.data, self.args['lang'])
return vocab
def init_sent_ids(self):
self.sentence_ids = []
self.cumlen = [0]
for i, para in enumerate(self.sentences):
for j in range(len(para)):
self.sentence_ids += [(i, j)]
self.cumlen += [self.cumlen[-1] + len(self.sentences[i][j][0])]
def para_to_sentences(self, para):
""" Convert a paragraph to a list of processed sentences. """
res = []
funcs = []
for feat_func in self.args['feat_funcs']:
if feat_func == 'end_of_para' or feat_func == 'start_of_para':
# skip for position-dependent features
continue
if feat_func == 'space_before':
func = lambda x: 1 if x.startswith(' ') else 0
elif feat_func == 'capitalized':
func = lambda x: 1 if x[0].isupper() else 0
elif feat_func == 'numeric':
func = lambda x: 1 if (NUMERIC_RE.match(x) is not None) else 0
else:
raise Exception('Feature function "{}" is undefined.'.format(feat_func))
funcs.append(func)
# stacking all featurize functions
composite_func = lambda x: [f(x) for f in funcs]
length = len(para)
#This function is to extract dictionary features for each character
def extract_dict_feat(idx):
dict_forward_feats = [0 for i in range(self.args['num_dict_feat'])]
dict_backward_feats = [0 for i in range(self.args['num_dict_feat'])]
forward_word = para[idx][0]
backward_word = para[idx][0]
prefix = True
suffix = True
for window in range(1,self.args['num_dict_feat']+1):
# concatenate each character and check if words found in dict not, stop if prefix not found
#check if idx+t is out of bound and if the prefix is already not found
if (idx + window) <= length-1 and prefix:
forward_word += para[idx+window][0].lower()
#check in json file if the word is present as prefix or word or None.
feat = 1 if forward_word in self.dictionary["words"] else 0
#if the return value is not 2 or 3 then the checking word is not a valid word in dict.
dict_forward_feats[window-1] = feat
#if the dict return 0 means no prefixes found, thus, stop looking for forward.
if forward_word not in self.dictionary["prefixes"]:
prefix = False
#backward check: similar to forward
if (idx - window) >= 0 and suffix:
backward_word = para[idx-window][0].lower() + backward_word
feat = 1 if backward_word in self.dictionary["words"] else 0
dict_backward_feats[window-1] = feat
if backward_word not in self.dictionary["suffixes"]:
suffix = False
#if cannot find both prefix and suffix, then exit the loop
if not prefix and not suffix:
break
return dict_forward_feats + dict_backward_feats
def process_sentence(sent):
return [self.vocab.unit2id(y[0]) for y in sent], [y[1] for y in sent], [y[2] for y in sent], [y[0] for y in sent]
use_end_of_para = 'end_of_para' in self.args['feat_funcs']
use_start_of_para = 'start_of_para' in self.args['feat_funcs']
current = []
for i, (unit, label) in enumerate(para):
label1 = label if not self.eval else 0
feats = composite_func(unit)
# position-dependent features
if use_end_of_para:
f = 1 if i == len(para)-1 else 0
feats.append(f)
if use_start_of_para:
f = 1 if i == 0 else 0
feats.append(f)
#if dictionary feature is selected
if self.args['use_dictionary']:
dict_feats = extract_dict_feat(i)
feats = feats + dict_feats
current += [(unit, label, feats)]
if label1 == 2 or label1 == 4: # end of sentence
if len(current) <= self.args['max_seqlen']:
# get rid of sentences that are too long during training of the tokenizer
res.append(process_sentence(current))
current = []
if len(current) > 0:
if self.eval or len(current) <= self.args['max_seqlen']:
res.append(process_sentence(current))
return res
def __len__(self):
return len(self.sentence_ids)
def shuffle(self):
for para in self.sentences:
random.shuffle(para)
self.init_sent_ids()
def next(self, eval_offsets=None, unit_dropout=0.0, old_batch=None, feat_unit_dropout=0.0):
''' Get a batch of converted and padded PyTorch data from preprocessed raw text for training/prediction. '''
feat_size = len(self.sentences[0][0][2][0])
unkid = self.vocab.unit2id('<UNK>')
padid = self.vocab.unit2id('<PAD>')
if old_batch is not None:
# If we have previously built a batch of data and made predictions on them, then when we are trying to make
# prediction on later characters in those paragraphs, we can avoid rebuilding the converted data from scratch
# and just (essentially) advance the indices/offsets from where we read converted data in this old batch.
# In this case, eval_offsets index within the old_batch to advance the strings to process.
ounits, olabels, ofeatures, oraw = old_batch
lens = (ounits != padid).sum(1).tolist()
pad_len = max(l-i for i, l in zip(eval_offsets, lens))
units = np.full((len(ounits), pad_len), padid, dtype=np.int64)
labels = np.full((len(ounits), pad_len), -1, dtype=np.int64)
features = np.zeros((len(ounits), pad_len, feat_size), dtype=np.float32)
raw_units = []
for i in range(len(ounits)):
eval_offsets[i] = min(eval_offsets[i], lens[i])
units[i, :(lens[i] - eval_offsets[i])] = ounits[i, eval_offsets[i]:lens[i]]
labels[i, :(lens[i] - eval_offsets[i])] = olabels[i, eval_offsets[i]:lens[i]]
features[i, :(lens[i] - eval_offsets[i])] = ofeatures[i, eval_offsets[i]:lens[i]]
raw_units.append(oraw[i][eval_offsets[i]:lens[i]] + ['<PAD>'] * (pad_len - lens[i] + eval_offsets[i]))
units = torch.from_numpy(units)
labels = torch.from_numpy(labels)
features = torch.from_numpy(features)
return units, labels, features, raw_units
def strings_starting(id_pair, offset=0, pad_len=self.args['max_seqlen']):
# At eval time, this combines sentences in paragraph (indexed by id_pair[0]) starting sentence (indexed
# by id_pair[1]) into a long string for evaluation. At training time, we just select random sentences
# from the entire dataset until we reach max_seqlen.
pid, sid = id_pair if self.eval else random.choice(self.sentence_ids)
sentences = [copy([x[offset:] for x in self.sentences[pid][sid]])]
drop_sents = False if self.eval or (self.args.get('sent_drop_prob', 0) == 0) else (random.random() < self.args.get('sent_drop_prob', 0))
total_len = len(sentences[0][0])
assert self.eval or total_len <= self.args['max_seqlen'], 'The maximum sequence length {} is less than that of the longest sentence length ({}) in the data, consider increasing it! {}'.format(self.args['max_seqlen'], total_len, ' '.join(["{}/{}".format(*x) for x in zip(self.sentences[pid][sid])]))
if self.eval:
for sid1 in range(sid+1, len(self.sentences[pid])):
total_len += len(self.sentences[pid][sid1][0])
sentences.append(self.sentences[pid][sid1])
if total_len >= self.args['max_seqlen']:
break
else:
while True:
pid1, sid1 = random.choice(self.sentence_ids)
total_len += len(self.sentences[pid1][sid1][0])
sentences.append(self.sentences[pid1][sid1])
if total_len >= self.args['max_seqlen']:
break
if drop_sents and len(sentences) > 1:
if total_len > self.args['max_seqlen']:
sentences = sentences[:-1]
if len(sentences) > 1:
p = [.5 ** i for i in range(1, len(sentences) + 1)] # drop a large number of sentences with smaller probability
cutoff = random.choices(list(range(len(sentences))), weights=list(reversed(p)))[0]
sentences = sentences[:cutoff+1]
units = [val for s in sentences for val in s[0]]
labels = [val for s in sentences for val in s[1]]
feats = [val for s in sentences for val in s[2]]
raw_units = [val for s in sentences for val in s[3]]
if not self.eval:
cutoff = self.args['max_seqlen']
units, labels, feats, raw_units = units[:cutoff], labels[:cutoff], feats[:cutoff], raw_units[:cutoff]
return units, labels, feats, raw_units
if eval_offsets is not None:
# find max padding length
pad_len = 0
for eval_offset in eval_offsets:
if eval_offset < self.cumlen[-1]:
pair_id = bisect_right(self.cumlen, eval_offset) - 1
pair = self.sentence_ids[pair_id]
pad_len = max(pad_len, len(strings_starting(pair, offset=eval_offset-self.cumlen[pair_id])[0]))
pad_len += 1
id_pairs = [bisect_right(self.cumlen, eval_offset) - 1 for eval_offset in eval_offsets]
pairs = [self.sentence_ids[pair_id] for pair_id in id_pairs]
offsets = [eval_offset - self.cumlen[pair_id] for eval_offset, pair_id in zip(eval_offsets, id_pairs)]
offsets_pairs = list(zip(offsets, pairs))
else:
id_pairs = random.sample(self.sentence_ids, min(len(self.sentence_ids), self.args['batch_size']))
offsets_pairs = [(0, x) for x in id_pairs]
pad_len = self.args['max_seqlen']
# put everything into padded and nicely shaped NumPy arrays and eventually convert to PyTorch tensors
units = np.full((len(id_pairs), pad_len), padid, dtype=np.int64)
labels = np.full((len(id_pairs), pad_len), -1, dtype=np.int64)
features = np.zeros((len(id_pairs), pad_len, feat_size), dtype=np.float32)
raw_units = []
for i, (offset, pair) in enumerate(offsets_pairs):
u_, l_, f_, r_ = strings_starting(pair, offset=offset, pad_len=pad_len)
units[i, :len(u_)] = u_
labels[i, :len(l_)] = l_
features[i, :len(f_)] = f_
raw_units.append(r_ + ['<PAD>'] * (pad_len - len(r_)))
if unit_dropout > 0 and not self.eval:
# dropout characters/units at training time and replace them with UNKs
mask = np.random.random_sample(units.shape) < unit_dropout
mask[units == padid] = 0
units[mask] = unkid
for i in range(len(raw_units)):
for j in range(len(raw_units[i])):
if mask[i, j]:
raw_units[i][j] = '<UNK>'
# dropout unit feature vector in addition to only torch.dropout in the model.
# experiments showed that only torch.dropout hurts the model
# we believe it is because the dict feature vector is mostly scarse so it makes
# more sense to drop out the whole vector instead of only single element.
if self.args['use_dictionary'] and feat_unit_dropout > 0 and not self.eval:
mask_feat = np.random.random_sample(units.shape) < feat_unit_dropout
mask_feat[units == padid] = 0
for i in range(len(raw_units)):
for j in range(len(raw_units[i])):
if mask_feat[i,j]:
features[i,j,:] = 0
units = torch.from_numpy(units)
labels = torch.from_numpy(labels)
features = torch.from_numpy(features)
return units, labels, features, raw_units
|