Skip to content

Commit 48526c4

Browse files
twitterSentimentalAnalysisScript (avinashkranjan#952)
1 parent cc6299d commit 48526c4

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from nltk.sentiment import SentimentIntensityAnalyzer
2+
from textblob import TextBlob
3+
4+
import tweepy
5+
6+
7+
class model(object):
8+
9+
def __init__(self, candidate_key, candidate_sec, access_key, access_sec):
10+
super().__init__()
11+
self.candidate_key = candidate_key
12+
self.candidate_sec = candidate_sec
13+
self.access_key = access_key
14+
self.access_sec = access_sec
15+
16+
def get_authenticated_api(self):
17+
auth = tweepy.OAuthHandler(self.candidate_key, self.candidate_sec)
18+
auth.set_access_token(self.access_key, self.access_sec)
19+
api = tweepy.API(auth)
20+
return api
21+
22+
def get_live_tweets_from_Twitter(self, text):
23+
24+
api = self.get_authenticated_api()
25+
26+
tweet_live = api.search(text, tweet_mode='extended')
27+
return tweet_live
28+
29+
def analysis_live_tweet_data(self, text):
30+
31+
tweet_live = self.get_live_tweets_from_Twitter(text)
32+
for tweet in tweet_live:
33+
tweet_is = tweet.text
34+
analysis = TextBlob(tweet_is)
35+
36+
def detailed_analysis_tweet_data(self, text):
37+
38+
# if polarity is in negative then the tweet is negative
39+
# if in positive then its a positive tweet
40+
# if polarity is greater then 0 and less then 5 then tweet is neutral
41+
42+
tweet_live = self.get_live_tweets_from_Twitter(text)
43+
result = []
44+
for tweet in tweet_live:
45+
polarity = TextBlob(tweet.full_text).sentiment.polarity
46+
subjectivity = TextBlob(tweet.full_text).sentiment.subjectivity
47+
score = SentimentIntensityAnalyzer().polarity_scores(tweet.full_text)
48+
49+
if polarity < 0 or subjectivity < 0 and score['neg'] > score['pos']:
50+
result.append([tweet.full_text, polarity, subjectivity, score, "negative"])
51+
elif polarity > 0 and subjectivity > 0 and score['neg'] < score['pos']:
52+
result.append([tweet.full_text, polarity, subjectivity, score, "positive"])
53+
else:
54+
result.append([tweet.full_text, polarity, subjectivity, score, "neutral"])
55+
56+
return result
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## Twitter Sentimental Analysis Script
2+
3+
This python script can be used to scrap twitter for a specific tweets according to there hashTags and then classify these tweets according to there sentiments example the tweet is positive, negative or neutral tweet
4+
5+
This provides features like :
6+
7+
1. Tweets Scraping
8+
2. sorting the Tweets
9+
3. Finding out the polarity,subjectivity score of a tweet
10+
4. SentimentIntensityAnalyzer on the tweet
11+
5. based on these scores it differentiate them in different categories like Positive , Negative and more.
12+
13+
14+
## Steps to Create your Credentials :
15+
16+
1. Apply for a Twitter Developer Account here [Developers site](https://developer.twitter.com/en)
17+
2. Create an Application a new Application
18+
3. Create the Authentication Credentials
19+
4. Copy and paste your credential_variables.env file
20+
21+
and done your credentials are setup for working
22+
23+
24+
## Installation
25+
26+
First of all install [python]("https://www.python.org/downloads/") on your system.
27+
28+
```
29+
pip install nltk
30+
pip install tweepy
31+
pip install textblob
32+
```
33+
34+
35+
### Made with ❤️ by Shantam Sultania
36+
37+
You can find me at:-
38+
[Linkedin](https://www.linkedin.com/in/shantam-sultania-737084175/) or [Github](https://github.com/shantamsultania) .
39+
40+
Happy coding ❤️ .
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from TwitterTweetSentimentalAnalysis import Model
2+
import getpass
3+
4+
5+
class app(object):
6+
7+
@staticmethod
8+
def run_app():
9+
try:
10+
candidate_key = getpass.getpass(prompt='enter your candidate Key : ')
11+
candidate_sec = getpass.getpass(prompt='enter your candidate secret Key : ')
12+
access_key = getpass.getpass(prompt='enter your access Key : ')
13+
access_sec = getpass.getpass(prompt='enter your access secret Key : ')
14+
15+
except Exception as E:
16+
print('There is an Error : ', E)
17+
else:
18+
model_object = Model.model(candidate_key, candidate_sec, access_key, access_sec)
19+
print(model_object.get_authenticated_api())
20+
text = input(" Enter the tag you want to perform sentimental analysis on : ")
21+
result = model_object.detailed_analysis_tweet_data(text)
22+
for i in result:
23+
print(i)
24+
25+
26+
if __name__ == "__main__":
27+
object = app()
28+
object.run_app()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
nltk==3.2.4
2+
tweepy==3.10.0
3+
textblob==0.15.3

0 commit comments

Comments
 (0)