-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
339 lines (267 loc) · 8.33 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
import os
import re
import nltk
from nltk.stem import WordNetLemmatizer
nltk.download('wordnet')
import itertools
from string import ascii_lowercase
import pandas as pd
import pickle
import tensorflow as tf
from keras.preprocessing import sequence
from tensorflow import keras
#global variables defined
stopword_list = []
dual_alpha_list = []
train_text = []
lemma_train_text = []
processed_train_text = []
RE_PATTERNS = {
' american ':
[
'amerikan'
],
' adolf ':
[
'adolf'
],
' hitler ':
[
'hitler'
],
' fuck':
[
'(f)(u|[^a-z0-9 ])(c|[^a-z0-9 ])(k|[^a-z0-9 ])([^ ])*',
'(f)([^a-z]*)(u)([^a-z]*)(c)([^a-z]*)(k)',
' f[!@#\$%\^\&\*]*u[!@#\$%\^&\*]*k', 'f u u c',
'(f)(c|[^a-z ])(u|[^a-z ])(k)', r'f\*',
'feck ', ' fux ', 'f\*\*',
'f\-ing', 'f\.u\.', 'f###', ' fu ', 'f@ck', 'f u c k', 'f uck', 'f ck','fuk', 'wtf','fucck','f cking'
],
' ass ':
[
'[^a-z]ass ', '[^a-z]azz ', 'arrse', ' arse ', '@\$\$'
'[^a-z]anus', ' a\*s\*s', '[^a-z]ass[^a-z ]',
'a[@#\$%\^&\*][@#\$%\^&\*]', '[^a-z]anal ', 'a s s'
],
' asshole ':
[
' a[s|z]*wipe', 'a[s|z]*[w]*h[o|0]+[l]*e', '@\$\$hole', 'ass hole'
],
' bitch ':
[
'b[w]*i[t]*ch', 'b!tch',
'bi\+ch', 'b!\+ch', '(b)([^a-z]*)(i)([^a-z]*)(t)([^a-z]*)(c)([^a-z]*)(h)',
'biatch', 'bi\*\*h', 'bytch', 'b i t c h','beetch'
],
' bastard ':
[
'ba[s|z]+t[e|a]+rd'
],
' transgender':
[
'transgender','trans gender'
],
' gay ':
[
'gay'
],
' cock ':
[
'[^a-z]cock', 'c0ck', '[^a-z]cok ', 'c0k', '[^a-z]cok[^aeiou]', ' cawk',
'(c)([^a-z ])(o)([^a-z ]*)(c)([^a-z ]*)(k)', 'c o c k'
],
' dick ':
[
' dick[^aeiou]', 'deek', 'd i c k','diick '
],
' suck ':
[
'sucker', '(s)([^a-z ]*)(u)([^a-z ]*)(c)([^a-z ]*)(k)', 'sucks', '5uck', 's u c k'
],
' cunt ':
[
'cunt', 'c u n t'
],
' bullshit ':
[
'bullsh\*t', 'bull\$hit','bs'
],
' homosexual':
[
'homo sexual','homosex'
],
' jerk ':
[
'jerk'
],
' idiot ':
[
'i[d]+io[t]+', '(i)([^a-z ]*)(d)([^a-z ]*)(i)([^a-z ]*)(o)([^a-z ]*)(t)', 'idiots', 'i d i o t'
],
' dumb ':
[
'(d)([^a-z ]*)(u)([^a-z ]*)(m)([^a-z ]*)(b)'
],
' shit ':
[
'shitty', '(s)([^a-z ]*)(h)([^a-z ]*)(i)([^a-z ]*)(t)', 'shite', '\$hit', 's h i t'
],
' shithole ':
[
'shythole','shit hole'
],
' retard ':
[
'returd', 'retad', 'retard', 'wiktard', 'wikitud'
],
' rape ':
[
' raped'
],
' dumbass':
[
'dumb ass', 'dubass'
],
' asshead':
[
'butthead', 'ass head'
],
' sex ':
[
's3x', 'sexuality',
],
' nigger ':
[
'nigger', 'ni[g]+a', ' nigr ', 'negrito', 'niguh', 'n3gr', 'n i g g e r'
],
' shut the fuck up':
[
'stfu'
],
' pussy ':
[
'pussy[^c]', 'pusy', 'pussi[^l]', 'pusses'
],
' faggot ':
[
'faggot', ' fa[g]+[s]*[^a-z ]', 'fagot', 'f a g g o t', 'faggit',
'(f)([^a-z ]*)(a)([^a-z ]*)([g]+)([^a-z ]*)(o)([^a-z ]*)(t)', 'fau[g]+ot', 'fae[g]+ot',
],
' motherfucker':
[
' motha ', ' motha f', ' mother f', 'motherucker', 'mother fucker'
],
' whore ':
[
'wh\*\*\*', 'w h o r e'
],
}
potential_stopwords=['editor', 'reference', 'thank', 'work','find', 'good', 'know', 'like', 'look', 'thing', 'want',
'time', 'list', 'section','wikipedia', 'doe', 'add','new', 'try', 'think', 'write','use', 'user', 'way', 'page']
labels = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
def clean_text(text, remove_repeat_text=True, remove_patterns_text=True, is_lower=True):
if is_lower:
text=text.lower()
if remove_patterns_text:
for target, patterns in RE_PATTERNS.items():
for pat in patterns:
text=str(text).replace(pat, target)
if remove_repeat_text:
text = re.sub(r'(.)\1{2,}', r'\1', text)
text = str(text).replace("\n", " ")
text = re.sub(r'[^\w\s]',' ',text)
text = re.sub('[0-9]',"",text)
text = re.sub(" +", " ", text)
text = re.sub("([^\x00-\x7F])+"," ",text)
return text
def lemma(text, lemmatization=True):
lemmatizer = WordNetLemmatizer()
output=''
if lemmatization:
text=text.split(' ')
for word in text:
word1 = lemmatizer.lemmatize(word, pos = "n") #noun
word2 = lemmatizer.lemmatize(word1, pos = "v") #verb
word3 = lemmatizer.lemmatize(word2, pos = "a") #adjective
word4 = lemmatizer.lemmatize(word3, pos = "r") #adverb
output=output + " " + word4
else:
output=text
return str(output.strip())
def iter_all_strings():
for size in itertools.count(1):
for s in itertools.product(ascii_lowercase, repeat=size):
yield "".join(s)
def dual_alpha():
for s in iter_all_strings():
dual_alpha_list.append(s)
if s == 'zz':
break
def alter_dual_alpha():
dual_alpha_list.remove('i')
dual_alpha_list.remove('a')
dual_alpha_list.remove('am')
dual_alpha_list.remove('an')
dual_alpha_list.remove('as')
dual_alpha_list.remove('at')
dual_alpha_list.remove('be')
dual_alpha_list.remove('by')
dual_alpha_list.remove('do')
dual_alpha_list.remove('go')
dual_alpha_list.remove('he')
dual_alpha_list.remove('hi')
dual_alpha_list.remove('if')
dual_alpha_list.remove('is')
dual_alpha_list.remove('in')
dual_alpha_list.remove('me')
dual_alpha_list.remove('my')
dual_alpha_list.remove('no')
dual_alpha_list.remove('of')
dual_alpha_list.remove('on')
dual_alpha_list.remove('or')
dual_alpha_list.remove('ok')
dual_alpha_list.remove('so')
dual_alpha_list.remove('to')
dual_alpha_list.remove('up')
dual_alpha_list.remove('us')
dual_alpha_list.remove('we')
for letter in dual_alpha_list:
stopword_list.append(letter)
def alter_stopwords():
for word in potential_stopwords:
stopword_list.append(word)
print(len(stopword_list))
def remove_stopwords(text, remove_stop=True):
output = ""
if remove_stop:
text=text.split(" ")
for word in text:
if word not in stopword_list:
output=output + " " + word
else :
output=text
return str(output.strip())
def modelLoad_Tokenize(text):
# Load tokenizer and model
tokenizer = 'C:/Users/Hetvi/Desktop/OneDrive/Projects/Kaggle competition - jigsaw\data_transformation/tokenizer'
with open(tokenizer, 'rb') as handle:
tokenizer = pickle.load(handle)
model = tf.keras.models.load_model('C:/Users/Hetvi/Desktop/OneDrive/Projects/Kaggle competition - jigsaw/model_trainer')
# Tokenize and pad the text
text = tokenizer.texts_to_sequences([text])
text = keras.preprocessing.sequence.pad_sequences(text, maxlen=200, padding='post')
# Make prediction
prediction = model.predict(text)
return prediction
def dataTransform(text):
text = clean_text(text)
text = lemma(text)
dual_alpha()
alter_dual_alpha()
alter_stopwords()
text = remove_stopwords(text)
prediction = modelLoad_Tokenize(text)
return prediction
result = dataTransform("You are an asshole")
print(result)