-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentimentfinal.py
More file actions
701 lines (613 loc) · 30.1 KB
/
Sentimentfinal.py
File metadata and controls
701 lines (613 loc) · 30.1 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#try1
#stop words
from nltk.tokenize import word_tokenize , sent_tokenize
from nltk.corpus import stopwords, wordnet
from nltk.tag import pos_tag as pos
from nltk.stem import WordNetLemmatizer
from nltk.probability import FreqDist
from nltk.classify.scikitlearn import SklearnClassifier
from sklearn.naive_bayes import MultinomialNB,BernoulliNB
from sklearn.linear_model import LogisticRegression,SGDClassifier
from sklearn.svm import SVC,LinearSVC,NuSVC
from nltk import NaiveBayesClassifier
from nltk.classify.maxent import MaxentClassifier
from nltk.classify import ClassifierI,accuracy
from statistics import mode
import pickle, numpy, pymysql, random
import re
from csv import reader as csvreader
from voteclassifier import voteClassifier
import threading
import time
lemmatizer = WordNetLemmatizer()
class Sentiment(object):
def main(self):
training_set, testing_set, training_featured_words = self.f_createData()
self.f_callClassifiers(training_set, testing_set)
## self.f_test_sentiment()
self.f_roger_sentiment(training_featured_words)
self.f_novak_sentiment(training_featured_words)
self.f_serena_sentiment(training_featured_words)
self.f_gar_sentiment(training_featured_words)
#=================================================================
#=====================Get the wordnet POS tag=====================
def f_get_wordnet_pos(self,treebank_tag):
if treebank_tag.startswith('J'):
return wordnet.ADJ #'a'
elif treebank_tag.startswith('V'):
return wordnet.VERB #'V'
elif treebank_tag.startswith('R'):
return wordnet.ADV #'r'
else:
return wordnet.NOUN #'n' #as NOUN is default
#=================================================================
#=================================================================
#=================================================================
#=====================Processes initial tweets====================
def f_process_tweets(self,tweets):
#***initial declarations
stop_words = self.f_stop_words()
self.processed_tweets=[]
# tweets = tweets.lower()
for tweet in tweets:
## print(tweet)
#process tweet
re.LOCALE
#covert to lower case
tweet = tweet.lower()
#Convert https?://* to URL
tweet = re.sub('(http:[^\s]+)', 'URL', tweet)
tweet = re.sub('(https:[^\s]+)', 'URL', tweet)
#Convert @username to AT_USER
tweet = re.sub('@([^\s]+)',' ',tweet)
#Remove additional white spaces
tweet = re.sub('[\s]+', ' ', tweet)
#Replace #word with word
tweet = re.sub(r'#([^\s]+)', r'\1', tweet)
#trim
## tweet = tweet.strip('\'"')
#************Remove stop words and punctuation**************
words = word_tokenize(tweet)
## words = words.lower()
punc_tweet=[]
for word in words:
## print(word)
#strip punctuation
word.strip(':"?,.\'')
val = re.search(r"^[a-zA-Z][a-zA-Z0-9]*$", word)
#ignore if it is a stop word
if word in stop_words or val is None:
continue
else:
punc_tweet.append(word)
#****************parts of speech tags**********************
tagged_tweet = pos(punc_tweet)
## print(tagged_tweet)
#********************Lemmatizer***************************
lemmatizer_words=[]
for pos_tags in tagged_tweet:
lemmatizer_words.append(lemmatizer.lemmatize(pos_tags[0],self.f_get_wordnet_pos(pos_tags[1])))
#saving lemmatizer_words for everytweet in processed_tweets
self.processed_tweets.append(lemmatizer_words)
## print(processed_tweets)
return self.processed_tweets
#=================================================================
#=================================================================
#=================================================================
#===================For removal of stop words=====================
#requires word tokens
def f_stop_words(self):#(word_tokens):
filtered_words=[]
self.stop_words = set(stopwords.words("english"))
## print (stop_words)
self.stop_words.add('AT_USER')
self.stop_words.add('roger')
self.stop_words.add('federer')
self.stop_words.add('novak')
self.stop_words.add('djokovic')
self.stop_words.add('murray')
self.stop_words.add('serena')
self.stop_words.add('williams')
self.stop_words.add('garbine')
self.stop_words.add('muguruza')
self.stop_words.add('garbi')
self.stop_words.add('URL')
self.stop_words.add('rt')
return self.stop_words
#=================================================================
#=================================================================
#=================================================================
#========================Training Data============================
def f_training_data(self):
training_data=[]
sentiment= []
tweets = []
#!!!!!!!!!!!!!!!!!!Better way to use CSV file!!!!!!!!!!!!!!!!!
# x,y = numpy.loadtxt('training_2.txt', delimiter=',', unpack=True)
#********************Gets the total number of rows***************
#using csv.reader() as csvreader
check_length = csvreader(open('data/training_tennis.csv', encoding='latin-1'))
length = sum(1 for row in check_length)#-500
print(length)
#********************Saves the Tweets and sentiment**************
raw_data = csvreader(open('data/training_tennis.csv', encoding='latin-1'), delimiter =",")
count = 0
for row in raw_data:
if count == 800:
break
tweets.append(row[1])
sentiment.append(row[0])
count+=1
## print(self.tweets)
processed_tweets = self.f_process_tweets(tweets)
## print(self.trprocessed_tweets)
## print(len(self.trprocessed_tweets), len(self.trsentiment))
for i in range(len(processed_tweets)):
#to convert it in [([words],positive),([words],positive)] format
temp=(processed_tweets[i], sentiment[i])
#to convert it in [[[words],positive],[[words],positive]] format
## temp=[]
## temp.append(self.trprocessed_tweets[i])
## temp.append(self.trsentiment[i])
training_data.append(temp)
## print(self.training_data)
random.shuffle(training_data)
return training_data
#=================================================================
#=================================================================
#=================================================================
#========================Testing Data=============================
def f_testing_data(self):
testing_data=[]
sentiment= []
tweets = []
#********************Gets the total number of rows***************
#using csv.reader() as csvreader
check_length = csvreader(open('data/training_tennis.csv', encoding='latin-1'))
length = sum(1 for row in check_length)-800
print(length)
#********************Saves the Tweets and sentiment**************
raw_data = csvreader(open('data/training_tennis.csv', encoding='latin-1'), delimiter =",")
count = 0
for row in raw_data:
if raw_data.line_num > length:
## print(raw_data.line_num)
if count == 100:
break
tweets.append(row[1])
sentiment.append(row[0])
count+=1
## print(self.tetweets)
processed_tweets = self.f_process_tweets(tweets)
## print(self.teprocessed_tweets)
## print(len(self.teprocessed_tweets), len(self.tesentiment))
for i in range(len(processed_tweets)):
#to convert it in [([words],positive),([words],positive)] format
temp=(processed_tweets[i], sentiment[i])
#to convert it in [[[words],positive],[[words],positive]] format
## temp=[]
## temp.append(self.teprocessed_tweets[i])
## temp.append(self.tesentiment[i])
testing_data.append(temp)
## print(self.testing_data)
## random.shuffle(testing_data)
return testing_data
#=================================================================
#=================================================================
#=================================================================
#==============Returns just words from specific==============
def f_specific_all_words(self,training_data):
self.all_words=[]
for data in training_data:
for words in data:#[0]: #Just the words not sentiment
self.all_words.append(words)
return(self.all_words)
#=================================================================
#==============Returns just words from training_data==============
def f_all_words(self,training_data):
self.all_words=[]
for data in training_data:
for words in data[0]: #Just the words not sentiment
self.all_words.append(words)
return(self.all_words)
#=================================================================
#=================================================================
#=================================================================
#==============Returns just words from training_data============
def f_feature_word(self,all_words):
all_words = FreqDist(all_words)
#**********IMPORTANT!!! Remeber to change the number******
common_words =all_words.most_common(3000)
## print(common_words)#prints word and maximum occurences
self.featured_words = []
for word in common_words:
#just the word not the number of occurences!!
self.featured_words.append(word[0])
return self.featured_words
#=================================================================
#=================================================================
#=================================================================
#==============Finds the featured_word from the data==============
###!!!!!!!!IMPORTANT!!!! ASK WHY TO USE THIS
def f_specific_find_feature(self,training_data, featured_words):
word = set(training_data)
feature = {}
for i in featured_words:
self.feature[i] = (i in word)
return feature
def f_find_feature(self,training_data, featured_words):
#**** Use either list of words or set of words**********
## word = []
## for words in training_data[0]:
## word.append(words)
word = set(training_data[0])
## print (word)
## word=f_lemmatizer(word) # remember to lemmatize the original to check the equality
feature = {}
for i in featured_words:
feature[i] = (i in word)
return feature
#=================================================================
#=================================================================
#=================================================================
#==============Finds the featured_set from the data===============
def f_feature_set(self,training_data, featured_word):
self.feature_set = [(self.f_find_feature(i,featured_word),i[1]) for i in training_data]
return self.feature_set
#=================================================================
#=================================================================
#=================================================================
#========================Naive bayes Classifier===================
def f_naivebayes(self,training_set,testing_set):
NBClassifier = NaiveBayesClassifier.train(training_set)
#**********Save Classifier to Pickle******************
## save_naivebayes = open('data/pickles/naivebayes.pickle','wb')
## pickle.dump(self.NBClassifier, save_naivebayes)
## save_naivebayes.close()
##************Open classifier from pickle*********************
## open_naivebayes = open('data/pickles/naivebayes.pickle','rb')
## self.NBClassifier = pickle.load(open_naivebayes)
## open_naivebayes.close()
print("Naive Bayes Algo accuracy", (accuracy(NBClassifier, testing_set))*100)
#classifier.show_most_informative_features(15)
return NBClassifier
#=================================================================
#============Multinomial Naive bayes Classifier===================
def f_multinomialNB(self,training_set,testing_set):
multinomialNBClassifier =SklearnClassifier(MultinomialNB())
multinomialNBClassifier.train(training_set)
#**********Save Classifier to Pickle******************
## save_multinomialNB = open('data/pickles/multinomialNB.pickle','wb')
## pickle.dump(self.multinomialNBClassifier, save_multinomialNB)
## save_multinomialNB.close()
#************Open classifier from pickle*********************
## open_multinomialNB = open('data/pickles/multinomialNB.pickle','rb')
## self.multinomialNBClassifier = pickle.load(open_multinomialNB)
## open_multinomialNB.close()
print("multinomialNB Algo accuracy", (accuracy(multinomialNBClassifier, testing_set))*100)
return multinomialNBClassifier
#=================================================================
#============Bernoulli Naive bayes Classifier===================
def f_bernoulliNB(self,training_set,testing_set):
bernoulliNBClassifier =SklearnClassifier(BernoulliNB())
bernoulliNBClassifier.train(training_set)
#**********Save Classifier to Pickle******************
## save_bernoulliNB = open('data/pickles/bernoulliNB.pickle','wb')
## pickle.dump(self.bernoulliNBClassifier, save_bernoulliNB)
## save_bernoulliNB.close()
#************Open classifier from pickle*********************
## open_bernoulliNB = open('data/pickles/bernoulliNB.pickle','rb')
## self.bernoulliNBClassifier = pickle.load(open_bernoulliNB)
## open_bernoulliNB.close()
print("bernoulliNB accuracy", (accuracy(bernoulliNBClassifier, testing_set))*100)
return bernoulliNBClassifier
#=================================================================
#============LogisticRegression Classifier===================
def f_logisticRegression(self,training_set,testing_set):
logisticRegressionClassifier =SklearnClassifier(LogisticRegression())
logisticRegressionClassifier.train(training_set)
#**********Save Classifier to Pickle******************
## save_logisticRegression = open('data/pickles/logisticRegression.pickle','wb')
## pickle.dump(self.logisticRegressionClassifier, save_logisticRegression)
## save_logisticRegression.close()
#************Open classifier from pickle*********************
## open_logisticRegression = open('data/pickles/logisticRegression.pickle','rb')
## self.logisticRegressionClassifier = pickle.load(open_logisticRegression)
## open_logisticRegression.close()
print("LogisticRegression Algo accuracy", (accuracy(logisticRegressionClassifier, testing_set))*100)
return logisticRegressionClassifier
#=================================================================
#============SGD Classifier===================
def f_sGD(self,training_set,testing_set):
sGDClassifier =SklearnClassifier(SGDClassifier())
sGDClassifier.train(training_set)
##
## #**********Save Classifier to Pickle******************
## save_sGDClassifier = open('data/pickles/sGD.pickle','wb')
## pickle.dump(self.sGDClassifier, save_sGDClassifier)
## save_sGDClassifier.close()
#************Open classifier from pickle*********************
## open_sGDClassifier = open('data/pickles/sGD.pickle','rb')
## self.sGDClassifier = pickle.load(open_sGDClassifier)
## open_sGDClassifier.close()
print("SGD Algo accuracy", (accuracy(sGDClassifier, testing_set))*100)
return sGDClassifier
#=================================================================
#============NuSVC Classifier===================
def f_nuSVC(self,training_set,testing_set):
nuSVCClassifier =SklearnClassifier(NuSVC())
nuSVCClassifier.train(training_set)
##
## #**********Save Classifier to Pickle******************
## save_NuSVC = open('data/pickles/nuSVC.pickle','wb')
## pickle.dump(self.nuSVCClassifier, save_NuSVC)
## save_NuSVC.close()
#************Open classifier from pickle*********************
## open_NuSVC = open('data/pickles/nuSVC.pickle','rb')
## self.nuSVCClassifier = pickle.load(open_NuSVC)
## open_NuSVC.close()
print("NuSVC Algo accuracy", (accuracy(nuSVCClassifier, testing_set))*100)
return nuSVCClassifier
#=================================================================
#============LinearSVC Classifier===================
def f_linearSVC(self,training_set,testing_set):
linearSVCClassifier =SklearnClassifier(LinearSVC())
linearSVCClassifier.train(training_set)
##
## #**********Save Classifier to Pickle******************
## save_linearSVC = open('data/pickles/linearSVC.pickle','wb')
## pickle.dump(self.linearSVCClassifier, save_linearSVC)
## save_linearSVC.close()
#************Open classifier from pickle*********************
## open_linearSVC= open('data/pickles/linearSVC.pickle','rb')
## self.linearSVCClassifier = pickle.load(open_linearSVC)
## open_linearSVC.close()
print("LinearSVC Algo accuracy", (accuracy(linearSVCClassifier, testing_set))*100)
return linearSVCClassifier
#=================================================================
#============Max Entropy Classifier===================
def f_maxEnt(self,training_set,testing_set):
## self.MaxEntClassifier = MaxentClassifier.train(training_set, 'GIS', trace=3,encoding=None, labels=None, gaussian_prior_sigma=0, max_iter = 10)
##
## #**********Save Classifier to Pickle******************
## save_maxEnt = open('data/pickles/MaxEnt.pickle','wb')
## pickle.dump(self.MaxEntClassifier, save_maxEnt)
## save_maxEnt.close()
#************Open classifier from pickle*********************
open_maxEnt= open('pickles/MaxEnt.pickle','rb')
self.MaxEntClassifier = pickle.load(open_maxEnt)
open_maxEnt.close()
print("Max Entropy Algo accuracy", (accuracy(self.MaxEntClassifier, testing_set))*100)
return
def f_createData(self):
#=================================================================
#=====================Creating testing and training sets========================
training_data = self.f_training_data()
## print(self.training_data)
##all_words= f_all_words(training_data)
training_featured_words = self.f_feature_word(self.f_all_words(training_data))
##print(self.training_featured_words)
#self.featureFile= open('feature_words','w')
##for word in self.training_featured_words:
## self.featureFile.write(word+'\n')
##self.featureFile.close()
self.training_set = self.f_feature_set(training_data, training_featured_words)
testing_data= self.f_testing_data()
self.testing_set = self.f_feature_set(testing_data, training_featured_words)
## print(self.testing_set)
return self.training_set, self.testing_set, training_featured_words
def f_callClassifiers(self, training_set, testing_set):
#======================================================================
#======================Calling Classifiers=============================
NBClassifier=self.f_naivebayes(training_set,testing_set)
bernoulliNBClassifier=self.f_bernoulliNB(training_set,testing_set)
multinomialNBClassifier=self.f_multinomialNB(training_set,testing_set)
logisticRegressionClassifier=self.f_logisticRegression(training_set,testing_set)
sGDClassifier=self.f_sGD(training_set,testing_set)
## nuSVCClassifier=self.f_nuSVC(training_set,testing_set)
linearSVCClassifier=self.f_linearSVC(training_set,testing_set)
##self.maxEntClassifier = self.f_maxEnt(training_set,testing_set) #Takes really long
self.voted_classifier = voteClassifier(linearSVCClassifier,)
## bernoulliNBClassifier,
## multinomialNBClassifier,
## logisticRegressionClassifier,
## sGDClassifier,
## ,nuSVCClassifier
## NBClassifier)
## print("voted_classifier accuracy percent:", (accuracy(self.voted_classifier, testing_set))*100)
## print("Classification:", voted_classifier.f_classify(testing_set[0][0]), "Confidence %:",voted_classifier.f_confidence(testing_set[0][0])*100)
## print("Classification:", voted_classifier.f_classify(testing_set[1][0]), "Confidence %:",voted_classifier.f_confidence(testing_set[1][0])*100)
def f_test_sentiment(self):
self.loop_count = 0
self.count = 0
self.pos = 0
self.neg = 0
self.neut = 0
for sentiment in range(len(self.testing_set)):
self.loop_count +=1
if self.voted_classifier.f_classify(self.testing_set[sentiment][0]) == self.testing_set[sentiment][1]:
self.count +=1
if self.voted_classifier.f_classify(self.testing_set[sentiment][0]) == 'positive':
self.pos += 1
elif self.voted_classifier.f_classify(self.testing_set[sentiment][0]) == 'negative':
self.neg += 1
elif self.voted_classifier.f_classify(self.testing_set[sentiment][0]) == 'neutral':
self.neut += 1
print(self.loop_count,self.count, self.pos, self.neg, self.neut)
def f_roger_sentiment(self, training_featured_words):
rtweets=[]
rsentiment=[]
roger_data=[]
#********************Gets the total number of rows***************
#using csv.reader() as csvreader
check_length = csvreader(open('data/roger_tweets.csv', encoding='latin-1'))
length = sum(1 for row in check_length)#-500
print(length)
#********************Saves the Tweets and sentiment**************
file = open('data/roger_tweets.csv').read()
count = 0
for r in file.split('\n'):
if count == 1000:
break
rtweets.append(r)
rsentiment.append('None')
count+=1
## print(self.rtweets)
roger_tweets = self.f_process_tweets(rtweets)
## print(self.roger_tweets)
for i in range(len(roger_tweets)):
if roger_tweets[i]:
temp = (roger_tweets[i], rsentiment[i])
roger_data.append(temp)
## print(self.roger_data)
roger_words = self.f_feature_word(self.f_specific_all_words(roger_tweets))
## print(self.roger_words)
roger_set = self.f_feature_set(roger_data, training_featured_words)
## print(self.roger_set)
self.rloop_count = 0
self.rpos = 0
self.rneg = 0
self.rneut = 0
for tweets in range(len(roger_set)):
self.rloop_count +=1
if self.voted_classifier.f_classify(roger_set[tweets][0]) == 'positive':
self.rpos += 1
elif self.voted_classifier.f_classify(roger_set[tweets][0]) == 'negative':
self.rneg += 1
elif self.voted_classifier.f_classify(roger_set[tweets][0]) == 'neutral':
self.rneut += 1
print(self.rloop_count, self.rpos, self.rneg, self.rneut)
def f_novak_sentiment(self,training_featured_words):
ntweets=[]
nsentiment=[]
novak_data=[]
#********************Gets the total number of rows***************
#using csv.reader() as csvreader
check_length = csvreader(open('data/novak_tweets.csv', encoding='latin-1'))
length = sum(1 for row in check_length)#-500
print(length)
#********************Saves the Tweets and sentiment**************
file = open('data/novak_tweets.csv').read()
count = 0
for r in file.split('\n'):
if count == 1000:
break
ntweets.append(r)
nsentiment.append('None')
count+=1
## print(ntweets)
novak_tweets = self.f_process_tweets(ntweets)
## print(novak_tweets)
for i in range(len(novak_tweets)):
if novak_tweets[i]: #to remove empty lists
temp = (novak_tweets[i], nsentiment[i])
novak_data.append(temp)
## print(novak_data)
novak_words = self.f_feature_word(self.f_specific_all_words(novak_tweets))
## print(self.novak_words)
novak_set = self.f_feature_set(novak_data, training_featured_words)
## for i in range(10):
## print(novak_set[i])
self.nloop_count = 0
self.npos = 0
self.nneg = 0
self.nneut = 0
for tweets in range(len(novak_set)):
self.nloop_count +=1
if self.voted_classifier.f_classify(novak_set[tweets][0]) == 'positive':
self.npos += 1
elif self.voted_classifier.f_classify(novak_set[tweets][0]) == 'negative':
self.nneg += 1
elif self.voted_classifier.f_classify(novak_set[tweets][0]) == 'neutral':
self.nneut += 1
print(self.nloop_count, self.npos, self.nneg, self.nneut)
def f_serena_sentiment(self,training_featured_words):
stweets=[]
ssentiment=[]
serena_data=[]
#********************Gets the total number of rows***************
#using csv.reader() as csvreader
check_length = csvreader(open('data/serena_tweets.csv', encoding='latin-1'))
length = sum(1 for row in check_length)#-500
print(length)
#********************Saves the Tweets and sentiment**************
file = open('data/serena_tweets.csv').read()
count = 0
for r in file.split('\n'):
if count == 500:
break
stweets.append(r)
ssentiment.append('None')
count+=1
## print(self.stweets)
serena_tweets = self.f_process_tweets(stweets)
## print(self.serena_tweets)
for i in range(len(serena_tweets)):
if serena_tweets[i]:
temp = (serena_tweets[i], ssentiment[i])
serena_data.append(temp)
## print(self.serena_data)
##
serena_words = self.f_feature_word(self.f_specific_all_words(serena_tweets))
## print(self.serena_words)
serena_set = self.f_feature_set(serena_data, training_featured_words)
## for i in range(1):
## print(serena_set[i][0])
self.sloop_count = 0
self.spos = 0
self.sneg = 0
self.sneut = 0
for tweets in range(len(serena_set)):
self.sloop_count +=1
if self.voted_classifier.f_classify(serena_set[tweets][0]) == 'positive':
self.spos += 1
elif self.voted_classifier.f_classify(serena_set[tweets][0]) == 'negative':
self.sneg += 1
elif self.voted_classifier.f_classify(serena_set[tweets][0]) == 'neutral':
self.sneut += 1
print(self.sloop_count, self.spos, self.sneg, self.sneut)
def f_gar_sentiment(self,training_featured_words):
gtweets=[]
gsentiment=[]
gar_data=[]
#********************Gets the total number of rows***************
#using csv.reader() as csvreader
check_length = csvreader(open('data/garbi_tweets.csv', encoding='latin-1'))
length = sum(1 for row in check_length)#-500
print(length)
#********************Saves the Tweets and sentiment**************
file = open('data/garbi_tweets.csv').read()
count = 0
for r in file.split('\n'):
if count == 500:
break
gtweets.append(r)
gsentiment.append('None')
count+=1
## print(gtweets)
gar_tweets = self.f_process_tweets(gtweets)
## print(gar_tweets)
for i in range(len(gar_tweets)):
if gar_tweets[i]:
temp = (gar_tweets[i], gsentiment[i])
gar_data.append(temp)
## print(gar_data)
gar_words = self.f_feature_word(self.f_specific_all_words(gar_tweets))
## print(gar_words)
gar_set = self.f_feature_set(gar_data, training_featured_words)
## print(gar_set)
self.gloop_count = 0
self.gpos = 0
self.gneg = 0
self.gneut = 0
for tweets in range(len(gar_set)):
self.gloop_count +=1
if self.voted_classifier.f_classify(gar_set[tweets][0]) == 'positive':
self.gpos += 1
elif self.voted_classifier.f_classify(gar_set[tweets][0]) == 'negative':
self.gneg += 1
elif self.voted_classifier.f_classify(gar_set[tweets][0]) == 'neutral':
self.gneut += 1
print(self.gloop_count, self.gpos, self.gneg, self.gneut)
if __name__ == '__main__':
Sentiment().main()