-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcyberbullyingbot.py
233 lines (173 loc) · 7.19 KB
/
cyberbullyingbot.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
# Data Processing Tools
import praw
import pickle
import pandas
import numpy
# Machine Learning Tools
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
# Natural Language Processing Tools
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem.snowball import SnowballStemmer
import re
class CyberbullyingDetectionEngine:
# Class that deals with training and deploying cyberbullying detection
# model selection
def __init__(self):
self.corpus = None
self.tags = None
self.lexicons = None
self.vectorizer = None
self.model = None
self.metrics = None
class CustomVectorizer:
# Extracts features from text and creates word vectors
def __init__(self, lexicons):
self.lexicons = lexicons
def transform(self, corpus):
# Returns a numpy array of word vectors
word_vectors = []
for text in corpus:
features = []
for k, v in self.lexicons.items():
features.append(
len([w for w in word_tokenize(text) if w in v]))
word_vectors.append(features)
return numpy.array(word_vectors)
def _simplify(self, corpus):
# Takes in a list of strings (corpus) and remove stopwords, convert
# to lowercase, remove non-alphabetic characters, stems each word
stop_words = set(stopwords.words('english'))
stemmer = SnowballStemmer('english')
def clean(text):
text = re.sub('[^a-zA-Z0-9]', ' ', text)
words = [stemmer.stem(w) for w in word_tokenize(text.lower()) if
w not in stop_words]
return " ".join(words)
return [clean(text) for text in corpus]
def _get_lexicon(self, path):
# Takes in a path to a text file and returns a set containing every
# word in the file
words = set()
with open(path) as file:
for line in file:
words.update(line.strip().split(' '))
return words
def _model_metrics(self, features, tags):
# Takes in testing data and returns dictionary of _model_metrics
tp = 0
fp = 0
tn = 0
fn = 0
predictions = self.model.predict(features)
for r in zip(predictions, tags):
if r[0] == 1 and r[1] == 1:
tp += 1
elif r[0] == 1 and r[1] == 0:
fp += 1
elif r[0] == 0 and r[1] == 1:
fn += 1
elif r[0] == 0 and r[1] == 0:
tn += 1
precision = tp / (tp + fp)
recall = tp / (tp + fn)
return {
'precision': precision,
'recall': recall,
'f1': (2 * precision * recall) / (precision + recall)
}
def load_corpus(self, path, corpus_col, tag_col):
# Takes in a path to pickled pandas dataframe, the name of corpus
# column, and the name of tag column, and extracts a tagged corpus
data = pandas.read_pickle(path)[[corpus_col, tag_col]].values
self.corpus = [row[0] for row in data]
self.tags = [row[1] for row in data]
def load_lexicon(self, fname):
# Loads a set of words from a text file
if self.lexicons is None:
self.lexicons = {}
self.lexicons[fname] = self._get_lexicon('./data/' + fname + '.txt')
def load_model(self, model_name):
# Loads a ML model, its corresponding feature vectorizer, and its
# performance metrics
self.model = pickle.load(
open('./models/' + model_name + '_ml_model.pkl', 'rb'))
self.vectorizer = pickle.load(
open('./models/' + model_name + '_vectorizer.pkl', 'rb'))
self.metrics = pickle.load(
open('./models/' + model_name + '_metrics.pkl', 'rb'))
def train_using_bow(self):
# Trains a model using bag of words (word counts) on corpus and tags
corpus = self._simplify(self.corpus)
self.vectorizer = CountVectorizer()
self.vectorizer.fit(corpus)
bag_of_words = self.vectorizer.transform(corpus)
x_train, x_test, y_train, y_test = train_test_split(
bag_of_words, self.tags, test_size=0.2, stratify=self.tags)
self.model = MultinomialNB()
self.model.fit(x_train, y_train)
self.metrics = self._model_metrics(x_test, y_test)
def train_using_tfidf(self):
# Trains model using tf-idf weighted words counts as features
corpus = self._simplify(self.corpus)
self.vectorizer = TfidfVectorizer()
self.vectorizer.fit(corpus)
word_vectors = self.vectorizer.transform(corpus)
x_train, x_test, y_train, y_test = train_test_split(
word_vectors, self.tags, test_size=0.2, stratify=self.tags)
self.model = MultinomialNB()
self.model.fit(x_train, y_train)
self.metrics = self._model_metrics(x_test, y_test)
def train_using_custom(self):
# Trains model using a custom feature extraction approach
corpus = self._simplify(self.corpus)
self.vectorizer = self.CustomVectorizer(self.lexicons)
word_vectors = self.vectorizer.transform(corpus)
x_train, x_test, y_train, y_test = train_test_split(
word_vectors, self.tags, test_size=0.2, stratify=self.tags)
self.model = SVC()
self.model.fit(x_train, y_train)
self.metrics = self._model_metrics(x_test, y_test)
def evaluate(self):
# Return model metrics
return self.metrics
def save_model(self, model_name):
# Save model for future use
pickle.dump(self.model, open(
'./models/' + model_name + '_ml_model.pkl', 'wb'))
pickle.dump(self.vectorizer, open(
'./models/' + model_name + '_vectorizer.pkl', 'wb'))
pickle.dump(self.metrics, open(
'./models/' + model_name + '_metrics.pkl', 'wb'))
def predict(self, corpus):
# Takes in a text corpus and returns predictions
x = self.vectorizer.transform(self._simplify(corpus))
return self.model.predict(x)
if __name__ == '__main__':
reddit = praw.Reddit(
client_id='Your ID',
client_secret='Your Secret',
user_agent='cyberbullying detection model'
)
new_comments = reddit.subreddit('uwaterloo').comments(limit=1000)
queries = [comment.body for comment in new_comments]
engine = CyberbullyingDetectionEngine()
engine.load_corpus('./data/final_labelled_data.pkl', 'tweet', 'class')
engine.train_using_bow()
print(engine.evaluate())
print(engine.predict(queries))
engine.train_using_tfidf()
print(engine.evaluate())
print(engine.predict(queries))
engine.load_lexicon('hate-words')
engine.load_lexicon('neg-words')
engine.load_lexicon('pos-words')
engine.load_lexicon('second_person_words')
engine.load_lexicon('third_person_words')
engine.train_using_custom()
print(engine.evaluate())
print(engine.predict(queries))