-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentiment-analysis-main.py
239 lines (220 loc) · 10.8 KB
/
sentiment-analysis-main.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
#!/usr/bin/env python3
import os
from py.CreateClassifiers import create_classifiers, cleanup_classifiers
from py.DrawGraph import draw_piechart
from py.StoreTweetReview import cleanup_reviews, store
from py.GetLiveTweets import cleanup_tweets, get_live_tweets
from py.GetSearchTweets import get_search_tweets
from py.AnalysisWithAffin import analyse_with_affin
from py.AnalysisWithOpinionLexicon import analyse_with_opinion
from py.CreateDatasetWithTweets import create_dataset
if os.name == 'nt':
clear_screen = "cls"
else:
clear_screen = "clear"
def main():
while True:
os.system(clear_screen)
print("\t\t\tSentiment Analysis")
print("\t\t\t" + 18 * "-")
print("\nOptions:-\n")
print("1. Get live tweets using Tweepy\n2. Get search tweets using Tweepy\n3. Show first 10 tweets\n4. Sentiment Analysis using NLTK's Opinion Lexicon\n5. Sentiment Analysis using AFINN-111.txt\n6. Sentiment Analysis using Machine Learning Algorithms\n7. Exit")
c = input("\nCHOICE: ")
try:
c = int(c)
except:
print("Enter an integer value")
input("Press ENTER to continue.....")
continue
if c < 1 or c > 7:
print("Enter a value between 1 and 6")
input("Press ENTER to continue.....")
continue
if c == 1:
while True:
os.system(clear_screen)
print("\t\t\tGet live tweets using Tweepy")
print("\t\t\t----------------------------")
print("\nINFO:\tThis option will fetch live tweets from Twitter using Tweepy API.\n\tSince Tweepy is fetching live tweets it is much slower than TwitterSearch API.\n\tVery useful if you want to look for current affairs.\n\n")
print("1. Delete old tweets in the tweets folder\n2. Get new tweets\n3. Go Back")
ch = input("\nCHOICE: ")
try:
ch = int(ch)
except:
print("Enter an integer value")
input("Press ENTER to continue...")
continue
if ch == 1:
print("Old tweets removed.")
cleanup_tweets()
input("Press ENTER to continue...")
elif ch == 2:
search_topic = []
topic = input("Enter topic you need to search: ")
search_topic.append(topic)
with open("tweets/twitter.txt", encoding = "utf_16") as f:
lines = len(f.read().split("\n"))
while True:
tweet_count = input("How many tweets do you need? (default is 200): ")
if tweet_count == "":
get_live_tweets(search_topic, 200, lines)
break
try:
tweet_count = int(tweet_count)
break
except:
print("Enter an integer value")
get_live_tweets(search_topic, tweet_count, lines)
print("\n\nNew Tweets fetched")
input("Press ENTER to continue...")
elif ch == 3:
break
else:
print("Enter a number between 1 and 3")
input("Press ENTER to continue...")
elif c == 2:
while True:
os.system(clear_screen)
print("\t\t\tGet search tweets using Tweepy")
print("\t\t\t-------------------------------")
print("\nINFO:\tThis option will fetch all except live tweets from Twitter using Tweepy API.\n\tSince we are searching instead of live streaming it is much faster.\n\tVery useful if you want to look for old events.\n\n")
print("1. Delete old tweets in the tweets folder\n2. Get new tweets\n3. Go Back")
ch = input("\nCHOICE: ")
try:
ch = int(ch)
except:
print("Enter an integer value")
input("Press ENTER to continue...")
continue
if ch == 1:
print("Old tweets removed.")
cleanup_tweets()
input("Press ENTER to continue...")
elif ch == 2:
search_topic = input("Enter topic you need to search: ")
f = 0
while True:
tweet_count = input("How many tweets do you need? (default is 200): ")
if tweet_count == "":
f = 1
break
try:
tweet_count = int(tweet_count)
break
except:
print("Enter an integer value")
if f == 0:
print("\nFetching %d tweets on the topic %s...." %(tweet_count, search_topic))
for i in range(int(tweet_count/100)):
get_search_tweets(search_topic, 100)
if not tweet_count%100 == 0:
get_search_tweets(search_topic, tweet_count%100)
else:
print("\nFetching 200 tweets on the topic %s...." %search_topic)
get_search_tweets(search_topic, 100)
get_search_tweets(search_topic, 100)
print("\n\nNew Tweets fetched")
input("Press ENTER to continue...")
elif ch == 3:
break
else:
print("Enter a number between 1 and 3")
input("Press ENTER to continue...")
elif c == 3:
os.system(clear_screen)
print("\t\t\tFirst 10 tweets")
print("\t\t\t---------------\n\n")
with open("tweets/twitter.txt", encoding = "utf_16") as f:
tweets = f.read().split("\n")
for tweet in tweets[:10]:
print(tweet)
print("------------------------------------")
input("\n\nPress ENTER to continue......")
elif c == 4:
while True:
os.system(clear_screen)
print("\t\t\tSentiment Analysis using NLTK's Opinion Lexicon")
print("\t\t\t-----------------------------------------------")
print("\nINFO:\tNLTK's opinion lexicon contains lists for positive and negative words.\n\tThis algorithm uses these lists to find out the number of positive and negative words in a tweet.\n\tThe number of positive and negative words in the tweet determines if it is positive or negative.")
print("\n1. Analyse using Opinion lexicon.\n2. Go Back")
ch = input("\nCHOICE: ")
try:
ch = int(ch)
except:
print("Enter an integer value")
input("Press ENTER to continue...")
continue
if ch == 1:
print("Analyzing. Please wait....")
analyse_with_opinion()
input("Press ENTER to continue...")
elif ch == 2:
break
else:
print("Enter a number between 1 and 2")
input("Press ENTER to continue...")
elif c == 5:
while True:
os.system(clear_screen)
print("\t\t\tSentiment Analysis using AFINN-111.txt")
print("\t\t\t--------------------------------------")
print("\nINFO:\tAFINN-111.txt contains a list of posiive and negative words along with their score.\n\tThis algorithm uses these scores to find out the total score of positive and negative words in a tweet.\n\tThe total score of positive and negative words in the tweet determines if it is positive or negative.")
print("\n1. Analyse using AFINN-111.txt.\n2. Go Back")
ch = input("\nCHOICE: ")
try:
ch = int(ch)
except:
print("Enter an integer value")
input("Press ENTER to continue...")
continue
if ch == 1:
print("Analyzing. Please wait....")
analyse_with_affin()
input("Press ENTER to continue...")
elif ch == 2:
break
else:
print("Enter a number between 1 and 2")
input("Press ENTER to continue...")
elif c == 6:
while True:
os.system(clear_screen)
print("\t\t\tSentiment Analysis using Machine Learning")
print("\t\t\t-----------------------------------------")
print("\nINFO:\tThis module trains and creates classifiers which are used to classify the tweets.\n\tThe classifiers (which are mainly Machine Learning algorithms) used here are Naive Bayes, Bernoulli Naive Bayes,\n\tMultinomial Naive Bayes, Logistic Regression and Stochastic Gradient Descent.")
print("\n1. Delete the old trained classifiers\n2. Train classifiers using new tweets in tweets/twitter.txt\n3. Analyse tweets using the classifiers and draw pie charts for them.\n4. Go Back")
ch = input("\nCHOICE: ")
try:
ch = int(ch)
except:
print("Enter an integer value")
input("Press ENTER to continue...")
continue
if ch == 1:
print("Previous classifiers are being wiped out....")
cleanup_classifiers()
input("Press ENTER to continue...")
elif ch == 2:
print("Dataset is being created.....")
create_dataset("tweets/twitter.txt")
print("Classfiers are being created....")
create_classifiers()
input("Press ENTER to continue...")
elif ch == 3:
print("Analyzing. Please wait....")
cleanup_reviews()
store()
draw_piechart()
input("Press ENTER to continue...")
elif ch == 4:
break
else:
print("Enter a number between 1 and 4")
input("Press ENTER to continue...")
elif c == 7:
print("Exiting.....")
return
try:
main()
except KeyboardInterrupt as e:
print("\n\nCTRL + C pressed. Exiting....")