-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
373 lines (310 loc) · 10.4 KB
/
app.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
from flask import Flask, render_template, request, redirect, url_for, flash,send_file
import nltk
import hypernetx as hnx
import matplotlib.pyplot as plt
nltk.download('stopwords')
from nltk.corpus import stopwords
import pandas as pd
import pickle
import re
import io
from nltk.stem.porter import PorterStemmer
from sklearn.feature_extraction.text import TfidfVectorizer
app = Flask(__name__)
app.secret_key = 'many random bytes'
usr=""
port_stem = PorterStemmer()
stop_words = set(stopwords.words('english')) - set(['not','no','don\'t','n\'t'])
#divide words to cases
#case 1 : Not hate word
#case 2 : Hate word
#case 3 : Hate word, but depends on context
#case 4 : possibility of hate word, nbut high indeterminancy
#case 5 : not hate hate word, but depends on context
case1=['want','gonna','stop','apart','done','insid','bye','felicia','use','would','king','kong','insert','basebal','bat','male','domest','cell','she','still','thing','think','absolut','already','sun','readi','share','complet','meet','heaven','eat','abl','sayin','start','might','yo','forget','sooooooo','anoth','urself','privladg','ur','finish','read','amaz','book','recommend','love','spend','time','famili','friend','grate','opportun','far','great','workout','gym','today','excit','try','new','recip','dinner','tonight','music','favorit','artist','proud','hard','work','put','achieve','goal','kind','respect','norm','import','take','care','health','watch','inspir','ted','talk','feel','motiv','support','life','walk','alway','help','clear','mind','educ','key','travel','see','world','one','day','other','let','focu','unit','us','rather','divid','beauti','like','make','differ','someon','good','idea','learn','skill','keep','hous','apt','cold','hobbi','fulfil','break','must','bodi','refresh','benefici','cracker','somehow','avoid','get','shone','fr','bought','ah','sub','da','corner','sto','easi','dream','rememb','joyou','nazi','tell','bath','sorri','desir','say','abolish','gettin','gang','bred','beta',
'rough','ball','yeah','cleanin','deep','load','sweat','drippin','total','final','url','gettin','pleas','send','video','surround','also','follow','drool','bad','commun']
case2=['rape','boipussi','white','goat','fucker','brutalis','suck','slutti','whore','sex','fat','homo','dumb','shit','dead','racist','hoe','bitch','pussi','stupid','ugli','ass','divers','give','back','nigga','gay','fuck','bash','cunt','filthi','dirti','cock','cum','floodin','slut','dildo']
case3=['bull','terrorist','kill','thick','breed','swear','useless','spit','clout','drug','feend','bore','social','part','death','run','power','countri','greedi','bottom']
case4=['hole','cri','german','disgust','pound','smash','capri','sever','femal','choke','gal','men','hang','beat','lil','closet','mental','spread','inclus','penalti','short','first','freaki','punch','face','muslim','illeg','christian','daddi']
case5=['wish','huge','no','call','damn','photo','free','enjoy','deserv','not','except','peopl','go','outsid','unlock','potenti','small','act','make','out','father','big','posit','black','way',
]
#create subhypergraph from a threshold
c={}
speech=[]
speech11=[]
erosionk=[]
hatespeech=[]
nonhatespeech=[]
listx=[]
listy=[]
listk=[]
listr=[]
listg=[]
data1=[]
@app.route('/')
def home():
return render_template('homepage.html')
x={}
a=-1
def graph(s):
global a,x
word_list = s.split()
a+=1
x[a]=word_list
x1={}
def graph3(s):
global a,x
word_list = s.split()
a+=1
x1[a]=word_list
def graph1(s):
global a,c
word_list = s.split()
a+=1
c[a]=word_list
def weights(x):
global hatespeech,erosionk,speech,nonhatespeech,speech1
t=0
y=0
f=0
c=0
words=x.split()
for i in words:
c=c+1
if i in case1:
r=0
if(t<r):
t=r
y=y+0
f=f+1
elif i in case2:
t=1
elif i in case3:
t=1
y=y+0.5
elif i in case4:
r=0.5
if(t<r):
t=r
y=y+1
elif i in case5:
y=y+0.5
f=f+1
speech.append(x)
speech11.append(x)
erosionk.append(x)
if((t>0.8 and y>0.3)):
print(x," : hate speech")
hatespeech.append(x)
graph1(x)
else:
print(x," : non hate speech")
nonhatespeech.append(x)
def cleantext(text):
x=str(text).lower().replace('\\','').replace('_','')
tag=' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
spcl=tag.replace('[^\w\s]','')
return spcl
def stemming(content):
review = re.sub('[^a-zA-Z]',' ',content)
review = review.lower()
review = review.split()
review = [port_stem.stem(word) for word in review if not word in stop_words]
review = ' '.join(review)
return review
def model1(user):
pickle_in=open("SVM_model.pickle","rb")
classifier=pickle.load(pickle_in)
prediction=classifier.predict(user)
if prediction==0:
return("Non Hate Speech")
else:
return("Hate Speech")
def model2(user):
pickle_in=open("RF_model (1).pickle","rb")
classifier=pickle.load(pickle_in)
prediction=classifier.predict(user)
if prediction==0:
return("Non Hate Speech")
else:
return("Hate Speech")
def model3(user):
pickle_in=open("LR_model.pickle","rb")
classifier=pickle.load(pickle_in)
prediction=classifier.predict(user)
if prediction==0:
return("Non Hate Speech")
else:
return("Hate Speech")
#just pushing
@app.route('/Home')
def hoe():
return render_template('homepage.html')
@app.route('/Predict',methods=['GET','POST'])
def pre():
return render_template('prediction.html')
@app.route('/Tpred',methods=['GET','POST'])
def pred():
user=request.form["tweet"]
pickle_in=open("vectorized_data1.pickle","rb")
vectorizer=pickle.load(pickle_in)
user=cleantext(user)
user= stemming(user)
user=vectorizer.transform([str(user)])
mod1=model1(user)
mod2=model2(user)
mod3=model3(user)
return render_template('modeloutput.html',mod1=mod1,mod2=mod2,mod3=mod3)
@app.route('/upload', methods=['POST'])
def upload():
global a,data1
a=-1
csv_file=request.files['csv']
df = pd.read_csv(csv_file)
data=df.iloc[:, 0]
data1=df.iloc[:, 0]
data=data.apply(lambda x:cleantext(x))
data= data.apply(stemming)
d=data.apply(lambda y:graph(y))
d=data.apply(lambda z:weights(z))
return('',204)
@app.route('/hypergraph1', methods=['POST'])
def hypergraph():
# Generate the URL for the image file
G = hnx.Hypergraph(x)
# Draw the graph using matplotlib
fig, ax = plt.subplots()
hnx.draw(G, ax=ax)
ax.set_title("Main Hypergraph : Graph created using all the sentences in the dataset")
# Save the plot to an image file
png_output = io.BytesIO()
fig.savefig(png_output, format='png')
png_output.seek(0)
# Send the PNG file to the client
return send_file(png_output, mimetype='image/png')
@app.route('/hypergraph2', methods=['POST'])
def hypergraph2():
G = hnx.Hypergraph(c)
# Draw the graph using matplotlib
fig, ax = plt.subplots()
hnx.draw(G, ax=ax)
ax.set_title("Hypergraph X : Hate Speech Sentences above the threshold value")
# Save the plot to an image file
png_output = io.BytesIO()
fig.savefig(png_output, format='png')
png_output.seek(0)
# Send the PNG file to the client
return send_file(png_output, mimetype='image/png')
@app.route('/Dilationnodes')
def dilation():
#w.r.t nodes
global listx,hatespeech
for i in hatespeech:
words=i.split()
for j in words:
if j not in listx:
listx.append(j)
return render_template('dilationnodes.html',listx1=listx)
@app.route('/Dilationedges')
def dilation1():
#w.r.t edges
global listx,listg
for i in listx:
for j in speech:
words=j.split()
if i in words:
if j not in listg:
listg.append(j)
return render_template('dilationedges.html',listx1=listg)
@app.route('/Erosionnodes')
def erosion1():
#erosion w.r.t nodes
global nonhatespeech,listk,listx,listy
for i in nonhatespeech:
words=i.split()
for j in words:
if j not in listk:
listk.append(j)
for i in listx:
if i not in listk:
listr.append(i)
return render_template('erosionnodes.html',listy1=listr)
@app.route('/Erosionedges')
def erosion2():
global listx,erosionk
for y in erosionk:
words=y.split()
for k in words:
if k not in listx:
erosionk.remove(y)
break
return render_template('erosionedges.html',speech1=erosionk)
@app.route('/output')
def output1():
for i in hatespeech:
words=i.split()
for j in words:
for y in speech:
wordslist=y.split()
if j in wordslist:
speech.remove(y)
for i in speech11:
if i in speech:
speech11.remove(i)
return render_template('output.html',speech1=speech,speech2=speech11)
@app.route('/output1')
def output11():
global hatespeech,speech,speech11,data1
for i in hatespeech:
words=i.split()
for j in words:
for y in speech:
wordslist=y.split()
if j in wordslist:
speech.remove(y)
for i in speech11:
if i in speech:
speech11.remove(i)
for j in data1:
d=cleantext(j)
d=stemming(d)
for i in speech:
if d==i:
speech.remove(i)
speech.append(j)
for i in speech11:
if d==i:
speech11.remove(i)
speech11.append(j)
return render_template('output1.html',speech1=speech,speech2=speech11)
@app.route('/outputgraph')
def output2():
global a
for i in hatespeech:
words=i.split()
for j in words:
for y in speech:
wordslist=y.split()
if j in wordslist:
speech.remove(y)
for i in speech:
graph3(i)
G = hnx.Hypergraph(x1)
# Draw the graph using matplotlib
fig, ax = plt.subplots()
hnx.draw(G, ax=ax)
ax.set_title("Final Output Hypergraph : Graph after removing hatespeech ")
# Save the plot to an image file
png_output = io.BytesIO()
fig.savefig(png_output, format='png')
png_output.seek(0)
# Send the PNG file to the client
return send_file(png_output, mimetype='image/png')
@app.route('/accuracy',methods=['POST'])
def accuracy():
return render_template('image.html')
@app.route('/Fuzzy',methods=['POST'])
def fuz():
return render_template('fuzzy.html')
if __name__ == "__main__":
app.run(debug=True)