-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
349 lines (238 loc) · 9.69 KB
/
preprocessing.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
345
346
347
348
349
"""inital data preprocessing pipeline
functions:
train_preprocessor
test_preprocessor
test_answer_preprocessor
"""
import numpy as np
import pickle
import yaml
import pandas as pd
import sys
from sklearn.model_selection import train_test_split
import torch
from spacy.tokenizer import Tokenizer
from spacy.lang.en import English, EnglishDefaults
from dataloader import batched_data
with open('/home/config.yaml') as f:
config=yaml.safe_load(f)
#------------------------------------------------------------------------------------------------#
def json_to_df(j): # used for train set
data=pd.DataFrame(columns=['title','context','question','answer_start','answer_text'])
o_=len(j['data'])
for h in range(o_): # title
l_=len(j['data'][h]['paragraphs'])
title=j['data'][h]['title']
for i in range(l_): # passage
context=j['data'][h]['paragraphs'][i]['context']
k_=len(j['data'][h]['paragraphs'][i]['qas'])
for j_ in range(k_): # question
que=j['data'][h]['paragraphs'][i]['qas'][j_]['question']
ans_s=j['data'][h]['paragraphs'][i]['qas'][j_]['answers'][0]['answer_start']
ans_t=j['data'][h]['paragraphs'][i]['qas'][j_]['answers'][0]['text']
data.loc[len(data.index)]=[title,context,que,ans_s,ans_t]
return data
#------------------------------------------------------------------------------------------------#
def dev_json_to_df(j): # used for test set with multiple answers.
data=pd.DataFrame(columns=['title','context','question','answer_text'])
o_=len(j['data'])
for h in range(o_): # title
l_=len(j['data'][h]['paragraphs'])
title=j['data'][h]['title']
for i in range(l_): # passage
context=j['data'][h]['paragraphs'][i]['context']
k_=len(j['data'][h]['paragraphs'][i]['qas'])
for j_ in range(k_): # question
que=j['data'][h]['paragraphs'][i]['qas'][j_]['question']
ans_l=len(j['data'][h]['paragraphs'][i]['qas'][j_]['answers'])
multiple=[]
for m_ in range(ans_l): # loop for multiple answers
ans_t=j['data'][h]['paragraphs'][i]['qas'][j_]['answers'][m_]['text']
multiple.append(ans_t)
unrepeat=list(set(multiple)) # handles repetitive answers by excluding them.
data.loc[len(data.index)]=[title,context,que,unrepeat]
return data
#------------------------------------------------------------------------------------------------#
def splitter(df,val_ratio):
"""
this is used to split dataframe to desired ratio,
shuffle is currently set to False
"""
q_train,q_val,c_train,c_val,a_train,a_val=train_test_split(df['question'],df['context'],df['answer_text'],test_size=val_ratio,random_state=0,shuffle=False)
d1=pd.DataFrame(list(zip(c_train,q_train,a_train)),columns=['context','question','answer_text'])
d2=pd.DataFrame(list(zip(c_val,q_val,a_val)),columns=['context','question','answer_text'])
return d1,d2
#------------------------------------------------------------------------------------------------#
# spacy tokenizer is good, but splitting at aprostrophe/contraction words
def tokenizer(h):
"""
tokenizer for train set - using spacy tokenizer
"""
a=[[],[],[]]
nlp = English()
# Create a blank Tokenizer with just the English vocab
x=['context','question','answer_text']
for k,i in enumerate(x):
for _ in range(h.shape[0]):
words = nlp(str(h[i][_]))# used to be lower
tokens = [word.text.lower() for word in words] # text to convert spacy tokens to words
a[k].append(tokens)
return a[0],a[1],a[2]
#------------------------------------------------------------------------------------------------#
# pipeline converting string to integers for context
def word2_int(h,vocab):
"""
converts word to integer using vocabulary dictionary index
"""
text_pipeline = lambda x: vocab(x)
q=[]
for i in h:
q.append(text_pipeline(i))
return q
#------------------------------------------------------------------------------------------------#
# pads the embeddings and truncates to a fixed value.
class pad_truncate:
"""
paddding and truncating of context , question , answer to a fixed values set.(400/30/20)
"""
def __init__(self,seq_length):
self.seq_length=seq_length
def pad_features(self,length_int):
features = np.zeros((len(length_int), self.seq_length), dtype = int)
for i, text in enumerate(length_int):
text_len = len(text)
if text_len <= self.seq_length:
zeroes = list(np.full((self.seq_length-text_len),0))
new = text+zeroes
elif text_len > self.seq_length:
new = text[0:self.seq_length]
features[i,:] = np.array(new)
return features
#------------------------------------------------------------------------------------------------#
# takes tokenized text as input
class pad_pipeline:
"""
implements word to integer , padding/truncating , numpy to torch tensor conversion
"""
def __init__(self,pad_feature_context,pad_feature_question,pad_feature_answer,vocab):
self.vocab=vocab
self.pad_feature_context=pad_feature_context
self.pad_feature_question=pad_feature_question
self.pad_feature_answer=pad_feature_answer
def question_processing(self,x):
x=word2_int(x,self.vocab)
x=self.pad_feature_question.pad_features(x)
x=torch.from_numpy(x).long()
return x
def context_processing(self,x):
x=word2_int(x,self.vocab)
x=self.pad_feature_context.pad_features(x)
x=torch.from_numpy(x).long()
return x
def answer_processing(self,x):
x=word2_int(x,self.vocab)
x=self.pad_feature_answer.pad_features(x)
x=torch.from_numpy(x).long()
return x
#------------------------------------------------------------------------------------------------#
# split the dataframe if neccesary and get the padded data
def train_preprocessor(df,vocab):
"""
takes in dataframe and returns a padded text of context/question/answer of TRAIN SET
tokenization/padding/truncation/word2_int/conversion to torch tensor.
"""
c_train,q_train,ans_train=tokenizer(df)
pad_feature_context=pad_truncate(400) # context/question/answer length
pad_feature_question=pad_truncate(30)
pad_feature_answer=pad_truncate(20)
pipe=pad_pipeline(pad_feature_context,pad_feature_question,pad_feature_answer,vocab)
c_pad,q_pad,ans_pad=pipe.context_processing(c_train),pipe.question_processing(q_train),pipe.answer_processing(ans_train)
return c_pad,q_pad,ans_pad
#------------------------------------------------------------------------------------------------#
def answer_tokenizer(h): # takes in
"""
used for tokenization of test set answers. takes in dataframe series after processed using
df.apply(eval)
"""
h=list(h)
a=[]
nlp = English()
# Create a blank Tokenizer with just the English vocab
for i in range(len(h)):
c_=[]
for j in range(len(h[i])):
words = nlp(str(h[i][j]))
tokens = [word.text.lower() for word in words]
c_.append(tokens)
a.append(c_)
return a
#------------------------------------------------------------------------------------------------#
def val_set_tokenizer(h):
"""
test set tokenization of context/question , takes in a dataframe containing context/question
and returns tokenized text as list
"""
a=[[],[]]
nlp = English()
# Create a blank Tokenizer with just the English vocab
x=['context','question']
for k,i in enumerate(x):
for _ in range(h.shape[0]):
words = nlp(str(h[i][_]))
tokens = [word.text.lower() for word in words]
a[k].append(tokens)
return a[0],a[1]
#------------------------------------------------------------------------------------------------#
def test_preprocessor(df,vocab): # processor only question and answer
# maximum length in dev set is 5 ,so we pad answer based on that.
"""
takes in dataframe and returns a padded text of context/question of TEST SET.
tokenization/padding/truncation/word2_int/conversion to torch tensor.
"""
c_val,q_val=val_set_tokenizer(df)
pad_feature_context=pad_truncate(400) # context/question/answer length
pad_feature_question=pad_truncate(30)
pad_feature_answer=pad_truncate(20)
pipe=pad_pipeline(pad_feature_context,pad_feature_question,pad_feature_answer,vocab)
c_pad,q_pad=pipe.context_processing(c_val),pipe.question_processing(q_val)
return c_pad,q_pad
#------------------------------------------------------------------------------------------------#
# for final processing of answers in test set
def test_answer_processing(h,vocab): # h is list of set of answers
"""
takes in LIST of answers of test set and outputs a torch tensor of padded -processed answers
which are of shape BATCH*5*20.
"""
text_pipeline = lambda x: vocab(x) # using [] since this accepts [] versions as input
a=torch.empty((len(h),5,20))
for i in range(len(h)):
c_=[]
x=[]
for j in range(len(h[i])):
pad_feature_answer=pad_truncate(20)
per_ans=[]
#per_ans.append(text_pipeline(k)[0])
x.append(text_pipeline(h[i][j]))
x_=pad_feature_answer.pad_features(x)
#print(x_)
if len(h[i])<5:
x_=np.array(x_)
c_=torch.from_numpy(x_)
#print(c_)
#sys.exit()
miss=5-len(h[i])
#print(miss,len(h[i]))
v_=torch.full((miss,20),0)
#print(v_)
ans_set=torch.vstack([c_,v_])
#print(ans_set)
else:
x_=np.array(x_)
c_=torch.from_numpy(x_)
ans_set=c_
pass
a[i,:,:]=ans_set
return a
#------------------------------------------------------------------------------------------------#
if __name__=='__main__':
pass