forked from kunal1244/khoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepression_detection_tweets.py
245 lines (192 loc) · 8.98 KB
/
depression_detection_tweets.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
# -*- coding: utf-8 -*-
"""Depression_detection_tweets.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1dH9LK0e_LA8Kf3x8Tc58r_ma2UYXtsxy
# Detecting depression in Tweets using Baye's Theorem
# Installing and importing libraries
"""
import nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from math import log, sqrt
import pandas as pd
import numpy as np
import re
import pickle
# %matplotlib inline
"""# Loading the Data"""
tweets = pd.read_csv('sentiment_tweets3.csv')
tweets.drop(['Unnamed: 0'], axis = 1, inplace = True)
"""# Splitting the Data in Training and Testing Sets
As you can see, I used almost all the data for training: 98% and the rest for testing.
"""
totalTweets = 8000 + 2314
trainIndex, testIndex = list(), list()
for i in range(tweets.shape[0]):
if np.random.uniform(0, 1) < 0.98:
trainIndex += [i]
else:
testIndex += [i]
trainData = tweets.iloc[trainIndex]
testData = tweets.iloc[testIndex]
"""# Wordcloud Analysis"""
depressive_words = ' '.join(list(tweets[tweets['label'] == 1]['message']))
positive_words = ' '.join(list(tweets[tweets['label'] == 0]['message']))
"""#Pre-processing the data for the training: Tokenization, stemming, and removal of stop words"""
def process_message(message, lower_case = True, stem = True, stop_words = True, gram = 2):
if lower_case:
message = message.lower()
words = word_tokenize(message)
words = [w for w in words if len(w) > 2]
if gram > 1:
w = []
for i in range(len(words) - gram + 1):
w += [' '.join(words[i:i + gram])]
return w
if stop_words:
sw = stopwords.words('english')
words = [word for word in words if word not in sw]
if stem:
stemmer = PorterStemmer()
words = [stemmer.stem(word) for word in words]
return words
class TweetClassifier(object):
def __init__(self, trainData, method = 'tf-idf'):
self.tweets, self.labels = trainData['message'], trainData['label']
self.method = method
def train(self):
self.calc_TF_and_IDF()
if self.method == 'tf-idf':
self.calc_TF_IDF()
else:
self.calc_prob()
def calc_prob(self):
self.prob_depressive = dict()
self.prob_positive = dict()
for word in self.tf_depressive:
self.prob_depressive[word] = (self.tf_depressive[word] + 1) / (self.depressive_words + \
len(list(self.tf_depressive.keys())))
for word in self.tf_positive:
self.prob_positive[word] = (self.tf_positive[word] + 1) / (self.positive_words + \
len(list(self.tf_positive.keys())))
self.prob_depressive_tweet, self.prob_positive_tweet = self.depressive_tweets / self.total_tweets, self.positive_tweets / self.total_tweets
def calc_TF_and_IDF(self):
noOfMessages = self.tweets.shape[0]
self.depressive_tweets, self.positive_tweets = self.labels.value_counts()[1], self.labels.value_counts()[0]
self.total_tweets = self.depressive_tweets + self.positive_tweets
self.depressive_words = 0
self.positive_words = 0
self.tf_depressive = dict()
self.tf_positive = dict()
self.idf_depressive = dict()
self.idf_positive = dict()
for i in range(noOfMessages):
message_processed = process_message(self.tweets.iloc[i])
count = list() #To keep track of whether the word has ocured in the message or not.
#For IDF
for word in message_processed:
if self.labels.iloc[i]:
self.tf_depressive[word] = self.tf_depressive.get(word, 0) + 1
self.depressive_words += 1
else:
self.tf_positive[word] = self.tf_positive.get(word, 0) + 1
self.positive_words += 1
if word not in count:
count += [word]
for word in count:
if self.labels.iloc[i]:
self.idf_depressive[word] = self.idf_depressive.get(word, 0) + 1
else:
self.idf_positive[word] = self.idf_positive.get(word, 0) + 1
def calc_TF_IDF(self):
self.prob_depressive = dict()
self.prob_positive = dict()
self.sum_tf_idf_depressive = 0
self.sum_tf_idf_positive = 0
for word in self.tf_depressive:
self.prob_depressive[word] = (self.tf_depressive[word]) * log((self.depressive_tweets + self.positive_tweets) \
/ (self.idf_depressive[word] + self.idf_positive.get(word, 0)))
self.sum_tf_idf_depressive += self.prob_depressive[word]
for word in self.tf_depressive:
self.prob_depressive[word] = (self.prob_depressive[word] + 1) / (self.sum_tf_idf_depressive + len(list(self.prob_depressive.keys())))
for word in self.tf_positive:
self.prob_positive[word] = (self.tf_positive[word]) * log((self.depressive_tweets + self.positive_tweets) \
/ (self.idf_depressive.get(word, 0) + self.idf_positive[word]))
self.sum_tf_idf_positive += self.prob_positive[word]
for word in self.tf_positive:
self.prob_positive[word] = (self.prob_positive[word] + 1) / (self.sum_tf_idf_positive + len(list(self.prob_positive.keys())))
self.prob_depressive_tweet, self.prob_positive_tweet = self.depressive_tweets / self.total_tweets, self.positive_tweets / self.total_tweets
def classify(self, processed_message):
pDepressive, pPositive = 0, 0
for word in processed_message:
if word in self.prob_depressive:
pDepressive += log(self.prob_depressive[word])
else:
if self.method == 'tf-idf':
pDepressive -= log(self.sum_tf_idf_depressive + len(list(self.prob_depressive.keys())))
else:
pDepressive -= log(self.depressive_words + len(list(self.prob_depressive.keys())))
if word in self.prob_positive:
pPositive += log(self.prob_positive[word])
else:
if self.method == 'tf-idf':
pPositive -= log(self.sum_tf_idf_positive + len(list(self.prob_positive.keys())))
else:
pPositive -= log(self.positive_words + len(list(self.prob_positive.keys())))
pDepressive += log(self.prob_depressive_tweet)
pPositive += log(self.prob_positive_tweet)
return pDepressive >= pPositive
def predict(self, testData):
result = dict()
for (i, message) in enumerate(testData):
processed_message = process_message(message)
result[i] = int(self.classify(processed_message))
return result
def metrics(labels, predictions):
true_pos, true_neg, false_pos, false_neg = 0, 0, 0, 0
for i in range(len(labels)):
true_pos += int(labels.iloc[i] == 1 and predictions[i] == 1)
true_neg += int(labels.iloc[i] == 0 and predictions[i] == 0)
false_pos += int(labels.iloc[i] == 0 and predictions[i] == 1)
false_neg += int(labels.iloc[i] == 1 and predictions[i] == 0)
precision = true_pos / (true_pos + false_pos)
recall = true_pos / (true_pos + false_neg)
Fscore = 2 * precision * recall / (precision + recall)
accuracy = (true_pos + true_neg) / (true_pos + true_neg + false_pos + false_neg)
print("Precision: ", precision)
print("Recall: ", recall)
print("F-score: ", Fscore)
print("Accuracy: ", accuracy)
sc_tf_idf = TweetClassifier(trainData, 'tf-idf')
sc_tf_idf.train()
save_classifier = open("tfidf.pkl","wb")
pickle.dump(sc_tf_idf, save_classifier)
save_classifier.close()
preds_tf_idf = sc_tf_idf.predict(testData['message'])
print(metrics(testData['label'], preds_tf_idf))
sc_bow = TweetClassifier(trainData, 'bow')
sc_bow.train()
save_classifier = open("bow.pkl","wb")
pickle.dump(sc_bow, save_classifier)
save_classifier.close()
preds_bow = sc_bow.predict(testData['message'])
print(metrics(testData['label'], preds_bow))
"""# Predictions with TF-IDF
# Depressive Tweets
"""
pm = process_message('Lately I have been feeling unsure of myself as a person & an artist')
print(sc_tf_idf.classify(pm))
"""# Positive Tweets"""
pm = process_message('Loving how me and my lovely partner is talking about what we want.')
print(sc_tf_idf.classify(pm))
"""# Predictions with Bag-of-Words (BOW)
# Depressive tweets
"""
pm = process_message('Hi hello depression and anxiety are the worst')
print(sc_bow.classify(pm))
"""# Positive Tweets"""
pm = process_message('Loving how me and my lovely partner is talking about what we want.')
print(sc_bow.classify(pm))