-
Notifications
You must be signed in to change notification settings - Fork 1
/
testing_8models_1.py
337 lines (288 loc) · 11.4 KB
/
testing_8models_1.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
import os
import pandas as pd
import ktrain
import re, string, unicodedata
import nltk
import contractions
import inflect
from nltk import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
from nltk.stem import LancasterStemmer, WordNetLemmatizer
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
def tup2dict(tup):
di = {}
for a, b in tup:
di[a] = b
return di
##def answers(pred):
## taskA = '9'
## taskB = ['9']*4
## taskC = ['9']*4
## p=pred['positive']
## neg=pred['negative']
## neu=pred['neutral']
##
##
## if neg>0.250 :
## taskA = '-1'
## elif neu>0.4:
## taskA = '0'
## else:
## taskA = '1'
##
## if pred['general']>0.70 :
## taskB[1] = '1'
## taskC[1] = '1'
## elif pred['not_sarcastic'] < pred['twisted_meaning'] and pred['very_twisted'] < pred['twisted_meaning']:
## taskB[1] = '1'
## taskC[1] = '2'
## elif pred['very_twisted'] > pred['twisted_meaning'] and pred['very_twisted'] > pred['not_sarcastic']:
## taskB[1] = '1'
## taskC[1] = '3'
## elif pred['not_sarcastic'] > pred['twisted_meaning'] and pred['very_twisted'] < pred['not_sarcastic']:
## taskB[1] = '0'
## taskC[1] = '0'
##
## if pred['general']>0.70 :
## taskB[1] = '1'
## taskC[1] = '1'
##
##
##
#### if pred['positive'] > pred['negative'] and pred['positive'] > pred['neutral']:
#### taskA = '1'
#### elif pred['positive'] < pred['negative'] and pred['negative'] > pred['neutral']:
#### taskA = '-1'
#### elif pred['positive'] < pred['neutral'] and pred['negative'] < pred['neutral']:
#### taskA = '0'
##
## if pred['not_funny'] > pred['funny'] and pred['not_funny'] > pred['very_funny'] and pred['hilarious'] < pred['not_funny']:
## taskB[0] = '0'
## taskC[0] = '0'
## elif pred['hilarious'] < pred['funny'] and pred['funny'] > pred['very_funny'] and pred['funny'] > pred['not_funny']:
## taskB[0] = '1'
## taskC[0] = '1'
## elif pred['very_funny'] > pred['funny'] and pred['hilarious'] < pred['very_funny'] and pred['very_funny'] > pred['not_funny']:
## taskB[0] = '1'
## taskC[0] = '2'
## elif pred['hilarious'] > pred['funny'] and pred['hilarious'] > pred['very_funny'] and pred['hilarious'] > pred['not_funny']:
## taskB[0] = '1'
## taskC[0] = '3'
##
## if pred['not_sarcastic'] > pred['general'] and pred['not_sarcastic'] > pred['twisted_meaning'] and pred['very_twisted'] < pred['not_sarcastic']:
## taskB[1] = '0'
## taskC[1] = '0'
## elif pred['not_sarcastic'] < pred['general'] and pred['general'] > pred['twisted_meaning'] and pred['very_twisted'] < pred['general']:
## taskB[1] = '1'
## taskC[1] = '1'
## elif pred['not_sarcastic'] < pred['twisted_meaning'] and pred['general'] < pred['twisted_meaning'] and pred['very_twisted'] < pred['twisted_meaning']:
## taskB[1] = '1'
## taskC[1] = '2'
## elif pred['general'] < pred['very_twisted'] and pred['very_twisted'] > pred['twisted_meaning'] and pred['very_twisted'] > pred['not_sarcastic']:
## taskB[1] = '1'
## taskC[1] = '3'
##
## if pred['not_offensive'] > pred['slight'] and pred['not_offensive'] > pred['very_offensive'] and pred['not_offensive'] > pred['hateful_offensive']:
## taskB[2] = '0'
## taskC[2] = '0'
## elif pred['not_offensive'] < pred['slight'] and pred['slight'] > pred['very_offensive'] and pred['slight'] > pred['hateful_offensive']:
## taskB[2] = '1'
## taskC[2] = '1'
## elif pred['not_offensive'] < pred['very_offensive'] and pred['slight'] < pred['very_offensive'] and pred['very_offensive'] > pred['hateful_offensive']:
## taskB[2] = '1'
## taskC[2] = '2'
## elif pred['not_offensive'] < pred['hateful_offensive'] and pred['hateful_offensive'] > pred['very_offensive'] and pred['slight'] < pred['hateful_offensive']:
## taskB[2] = '1'
## taskC[2] = '3'
##
## if pred['not_motivational'] > pred['motivational']:
## taskB[3] = '0'
## taskC[3] = '0'
## elif pred['not_motivational'] < pred['motivational']:
## taskB[3] = '1'
## taskC[3] = '1'
##
##
## line = taskA + '_' + ''.join(taskB) + '_' + ''.join(taskC) + '\n'
##
## return line
def replace_contractions(text):
"""Replace contractions in string of text"""
return contractions.fix(text)
def remove_URL(sample):
"""Remove URLs from a sample string"""
rurl = re.sub(r"http\S+", "", sample)
return re.sub(r"\w+[.]com", "", rurl)
def remove_non_ascii(words):
"""Remove non-ASCII characters from list of tokenized words"""
new_words = []
for word in words:
new_word = unicodedata.normalize('NFKD', word).encode('ascii', 'ignore').decode('utf-8', 'ignore')
new_words.append(new_word)
return new_words
def to_lowercase(words):
"""Convert all characters to lowercase from list of tokenized words"""
new_words = []
for word in words:
new_word = word.lower()
new_words.append(new_word)
return new_words
def remove_punctuation(words):
"""Remove punctuation from list of tokenized words"""
new_words = []
for word in words:
new_word = re.sub(r'[^\w\s]', '', word)
if new_word != '':
new_words.append(new_word)
return new_words
def replace_numbers(words):
"""Replace all interger occurrences in list of tokenized words with textual representation"""
p = inflect.engine()
new_words = []
for word in words:
if word.isdigit():
new_word = p.number_to_words(word)
new_words.append(new_word)
else:
new_words.append(word)
return new_words
def remove_stopwords(words):
"""Remove stop words from list of tokenized words"""
new_words = []
for word in words:
if word not in stopwords.words('english'):
new_words.append(word)
return new_words
def stem_words(words):
"""Stem words in list of tokenized words"""
stemmer = LancasterStemmer()
stems = []
for word in words:
stem = stemmer.stem(word)
stems.append(stem)
return stems
def lemmatize_verbs(words):
"""Lemmatize verbs in list of tokenized words"""
lemmatizer = WordNetLemmatizer()
lemmas = []
for word in words:
lemma = lemmatizer.lemmatize(word, pos='v')
lemmas.append(lemma)
return lemmas
def normalize(words):
words = remove_non_ascii(words)
words = to_lowercase(words)
words = remove_punctuation(words)
words = replace_numbers(words)
words = remove_stopwords(words)
return words
def preprocess(sample):
sample = remove_URL(sample)
sample = replace_contractions(sample)
# Tokenize
words = nltk.word_tokenize(sample)
# Normalize
return normalize(words)
def pre_preprocess(xval):
xval = str(xval.replace('\n',' ').replace('|','').encode("utf-8"))[2:-1]
xval = xval.lower()
xval = remove_URL(xval)
xval = replace_contractions(xval)
# Tokenize
words = nltk.word_tokenize(xval)
#print(words)
# Normalize
words = normalize(words)
#print(words)
words = [word for word in words if word.isalpha()]
xval = ' '.join(words)
return xval
##main
answers_dict = {'hilarious':'3','very_funny':'2','funny':'1','general':'1', 'twisted_meaning':'2', 'very_twisted':'3', 'not_sarcastic':'0','not_funny':'0','motivational':'1','not_motivational':'0', 'positive':'1', 'neutral':'0', 'negative':'-1','not_offensive':'0','very_offensive':'2', 'slight':'1', 'hateful_offensive':'3'}
humourpredictor = ktrain.load_predictor('/home/dgxuser136/ambuje1/senti_humour_ktrainbert.h5')
motivationpredictor = ktrain.load_predictor('/home/dgxuser136/ambuje1/motivation_ktrainbert.h5')
sentipredictor = ktrain.load_predictor('/home/dgxuser136/ambuje1/senti_ktrainbert.h5')
onlyfunnypredictor = ktrain.load_predictor('/home/dgxuser136/ambuje1/senti_onlyfunny_ktrainbert.h5')
offensivepredictor = ktrain.load_predictor('/home/dgxuser136/ambuje1/senti_offensive_ktrainbert.h5')
sarcasticpredictor = ktrain.load_predictor('/home/dgxuser136/ambuje1/senti_sarcastic_ktrainbert.h5')
onlysarcasticpredictor = ktrain.load_predictor('/home/dgxuser136/ambuje1/senti_onlysarcastic_ktrainbert.h5')
onlyoffensivepredictor = ktrain.load_predictor('/home/dgxuser136/ambuje1/senti_onlyoffensive_ktrainbert.h5')
df = pd.read_csv('/home/dgxuser136/ambuje1/2000_testdata.csv')
file1 = open("answer_8models.txt","w")
#x = 'Phrase|Index\n'
#print (tup2dict(tups, dictionary))
for i in range(len(df)):
#text = str(df['cleaned_ocr'][i])
text = pre_preprocess(str(df['corrected_text'][i]))
taskA = '9'
taskB = ['9']*4
taskC = ['9']*4
pred = sentipredictor.predict(text)
print(pred)
## pred = tup2dict(pred)
## pred = max(pred, key=pred.get)
taskA = answers_dict[pred]
pred = humourpredictor.predict(text)
print(pred)
## pred = tup2dict(pred)
## pred = max(pred, key=pred.get)
if pred == 'fun':
taskB[0] = '1'
else:
taskB[0] = '0'
## taskB[0] = answers_dict[pred]
if taskB[0] == '1':
pred = onlyfunnypredictor.predict(text)
print(pred)
## pred = tup2dict(pred)
## pred = max(pred, key=pred.get)
taskC[0] = answers_dict[pred]
else:
taskC[0] = '0'
pred = sarcasticpredictor.predict(text)
print(pred)
if pred == 'sarcastic':
taskB[1] = '1'
else:
taskB[1] = '0'
## pred = tup2dict(pred)
## pred = max(pred, key=pred.get)
## taskB[1] = answers_dict[pred]
if taskB[1] == '1':
pred = onlysarcasticpredictor.predict(text)
print(pred)
## pred = tup2dict(pred)
## pred = max(pred, key=pred.get)
taskC[1] = answers_dict[pred]
else:
taskC[1] = '0'
pred = offensivepredictor.predict(text)
print(pred)
if pred == 'offensive':
taskB[2] = '1'
else:
taskB[2] = '0'
## pred = tup2dict(pred)
## pred = max(pred, key=pred.get)
## taskB[2] = answers_dict[pred]
if taskB[2] == '1':
pred = onlyoffensivepredictor.predict(text)
print(pred)
## pred = tup2dict(pred)
## pred = max(pred, key=pred.get)
taskC[2] = answers_dict[pred]
else:
taskC[2] = '0'
pred = motivationpredictor.predict(text)
print(pred)
## pred = tup2dict(pred)
## pred = max(pred, key=pred.get)
taskB[3] = answers_dict[pred]
taskC[3] = taskB[3]
ans = taskA + '_' + ''.join(taskB) + '_' + ''.join(taskC) + '\n'
file1.write(ans)
file1.close()
##pred = [('hilarious', 0.10454379), ('not_funny', 0.21206911), ('very_funny', 0.32491603), ('funny', 0.36210373), ('general', 0.55724233), ('not_sarcastic', 0.11089532), ('twisted_meaning', 0.24999025), ('very_twisted', 0.09583253), ('not_offensive', 0.10120422), ('very_offensive', 0.30312324), ('slight', 0.5820346), ('hateful_offensive', 0.04012201), ('not_motivational', 0.9636585), ('motivational', 0.036573086), ('positive', 0.5848341), ('neutral', 0.28037217), ('negative', 0.14398904)]
##pred = tup2dict(pred)
##print(answers(pred))