-
Notifications
You must be signed in to change notification settings - Fork 10
/
Hate speech detection on twitter data_live.py
297 lines (159 loc) · 5.5 KB
/
Hate speech detection on twitter data_live.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# import the required libraries
import pandas as pd
import numpy as np
import re
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
from wordcloud import WordCloud
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix, ConfusionMatrixDisplay
# In[2]:
tweet_df = pd.read_csv('hateDetection_train.csv')
# In[3]:
tweet_df.head()
# In[4]:
tweet_df.info()
# In[5]:
# printing random tweets
print(tweet_df['tweet'].iloc[0],"\n")
print(tweet_df['tweet'].iloc[1],"\n")
print(tweet_df['tweet'].iloc[2],"\n")
print(tweet_df['tweet'].iloc[3],"\n")
print(tweet_df['tweet'].iloc[4],"\n")
# In[18]:
#creating a function to process the data
def data_processing(tweet):
tweet = tweet.lower()
tweet = re.sub(r"https\S+|www\S+http\S+", '', tweet, flags = re.MULTILINE)
tweet = re.sub(r'\@w+|\#','', tweet)
tweet = re.sub(r'[^\w\s]','',tweet)
tweet = re.sub(r'ð','',tweet)
tweet_tokens = word_tokenize(tweet)
filtered_tweets = [w for w in tweet_tokens if not w in stop_words]
return " ".join(filtered_tweets)
# In[19]:
tweet_df.tweet = tweet_df['tweet'].apply(data_processing)
# In[20]:
tweet_df = tweet_df.drop_duplicates('tweet')
# In[21]:
lemmatizer = WordNetLemmatizer()
def lemmatizing(data):
tweet = [lemmarizer.lemmatize(word) for word in data]
return data
# In[22]:
tweet_df['tweet'] = tweet_df['tweet'].apply(lambda x: lemmatizing(x))
# In[23]:
# printing the data to see the effect of preprocessing
print(tweet_df['tweet'].iloc[0],"\n")
print(tweet_df['tweet'].iloc[1],"\n")
print(tweet_df['tweet'].iloc[2],"\n")
print(tweet_df['tweet'].iloc[3],"\n")
print(tweet_df['tweet'].iloc[4],"\n")
# In[24]:
tweet_df.info()
# In[25]:
tweet_df['label'].value_counts()
# ### Data visualization
# In[26]:
fig = plt.figure(figsize=(5,5))
sns.countplot(x='label', data = tweet_df)
# In[27]:
fig = plt.figure(figsize=(7,7))
colors = ("red", "gold")
wp = {'linewidth':2, 'edgecolor':"black"}
tags = tweet_df['label'].value_counts()
explode = (0.1, 0.1)
tags.plot(kind='pie',autopct = '%1.1f%%', shadow=True, colors = colors, startangle =90,
wedgeprops = wp, explode = explode, label='')
plt.title('Distribution of sentiments')
# In[28]:
non_hate_tweets = tweet_df[tweet_df.label == 0]
non_hate_tweets.head()
# In[30]:
text = ' '.join([word for word in non_hate_tweets['tweet']])
plt.figure(figsize=(20,15), facecolor='None')
wordcloud = WordCloud(max_words=500, width=1600, height=800).generate(text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Most frequent words in non hate tweets', fontsize = 19)
plt.show()
# In[31]:
neg_tweets = tweet_df[tweet_df.label == 1]
neg_tweets.head()
# In[32]:
text = ' '.join([word for word in neg_tweets['tweet']])
plt.figure(figsize=(20,15), facecolor='None')
wordcloud = WordCloud(max_words=500, width=1600, height=800).generate(text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Most frequent words in hate tweets', fontsize = 19)
plt.show()
# In[35]:
vect = TfidfVectorizer(ngram_range=(1,2)).fit(tweet_df['tweet'])
# In[36]:
feature_names = vect.get_feature_names()
print("Number of features: {}\n".format(len(feature_names)))
print("First 20 features: \n{}".format(feature_names[:20]))
# In[37]:
vect = TfidfVectorizer(ngram_range=(1,3)).fit(tweet_df['tweet'])
# In[38]:
feature_names = vect.get_feature_names()
print("Number of features: {}\n".format(len(feature_names)))
print("First 20 features: \n{}".format(feature_names[:20]))
# ## Model Building
# In[39]:
X = tweet_df['tweet']
Y = tweet_df['label']
X = vect.transform(X)
# In[40]:
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
# In[41]:
print("Size of x_train:", (x_train.shape))
print("Size of y_train:", (y_train.shape))
print("Size of x_test: ", (x_test.shape))
print("Size of y_test: ", (y_test.shape))
# In[42]:
logreg = LogisticRegression()
logreg.fit(x_train, y_train)
logreg_predict = logreg.predict(x_test)
logreg_acc = accuracy_score(logreg_predict, y_test)
print("Test accuarcy: {:.2f}%".format(logreg_acc*100))
# In[43]:
print(confusion_matrix(y_test, logreg_predict))
print("\n")
print(classification_report(y_test, logreg_predict))
# In[44]:
style.use('classic')
cm = confusion_matrix(y_test, logreg_predict, labels=logreg.classes_)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=logreg.classes_)
disp.plot()
# In[45]:
from sklearn.model_selection import GridSearchCV
import warnings
warnings.filterwarnings('ignore')
# In[46]:
param_grid = {'C':[100, 10, 1.0, 0.1, 0.01], 'solver' :['newton-cg', 'lbfgs','liblinear']}
grid = GridSearchCV(LogisticRegression(), param_grid, cv = 5)
grid.fit(x_train, y_train)
print("Best Cross validation score: {:.2f}".format(grid.best_score_))
print("Best parameters: ", grid.best_params_)
# In[47]:
y_pred = grid.predict(x_test)
# In[48]:
logreg_acc = accuracy_score(y_pred, y_test)
print("Test accuracy: {:.2f}%".format(logreg_acc*100))
# In[49]:
print(confusion_matrix(y_test, y_pred))
print("\n")
print(classification_report(y_test, y_pred))