-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhehe.py
197 lines (174 loc) · 7.69 KB
/
hehe.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
import nltk
nltk.download('punkt')
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
import matplotlib.pyplot as plt
from math import log, sqrt
import pandas as pd
import numpy as np
import re
from get_all_tweets import get_all_tweets
from datetime import datetime
tweets = pd.read_csv('dataset/sentiment_tweets3.csv')
tweets.drop(['Unnamed: 0'], axis = 1, inplace = True)
get_all_tweets("anubhavp28")
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]
depressive_words = ' '.join(list(tweets[tweets['label'] == 1]['message']))
positive_words = ' '.join(list(tweets[tweets['label'] == 0]['message']))
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
def decide_mental_state(username):
data = pd.read_csv("dataset/tweets.csv")
tweets = data["tweet"]
dates = data["date"]
date_format = "%Y-%m-%d"
decision = False
result = [False]*len(tweets)
total = 0
for i in range(0,len(tweets)):
pm = process_message(tweets[i])
result[i] = sc_tf_idf.classify(pm)
x = len(dates)-1
now = datetime.strptime(dates[len(dates)-1],date_format)
while x>=0:
date = datetime.strptime(dates[x], date_format)
tf = [x for x in dates if (date-datetime.strptime(x, date_format)).days<7]
for i in tf:
if result[x]==True:
total += 1
else:
total -= 1
avg = total/len(tf)
if avg<0.5:
x-=1
elif avg>0.6:
decision = True
break
if (date-now).days>30:
decision = False
break
return decision
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()
self.calc_TF_IDF()
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()
preds_tf_idf = sc_tf_idf.predict(testData['message'])
metrics(testData['label'], preds_tf_idf)
#pm = process_message('Lately I have been feeling unsure of myself as a person & an artist')
#print(sc_tf_idf.classify(pm))