Skip to content

Commit e62db2e

Browse files
committed
Twitter sentimental analysis using python
1 parent 8f4876d commit e62db2e

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
## Get Started
3+
4+
Tweepy:
5+
Tweepy is the python client for the official Twitter API. Install it using following pip command:
6+
7+
```bash
8+
pip install tweepy
9+
```
10+
11+
TextBlob: textblob is the python library for processing textual data. Install it using following pip command:
12+
13+
```bash
14+
pip install textblob
15+
```
16+
17+
Install some NLTK corpora using following command:
18+
19+
```bash
20+
python -m textblob.download_corpora
21+
```
22+
## Authentication:
23+
In order to fetch tweets through Twitter API, one needs to register an App through their twitter account. Follow these steps for the same:
24+
25+
1. Open developer.twitter.com/apps and click the button: ‘Create New App’
26+
2. Fill the application details. You can leave the callback url field empty.
27+
3. Once the app is created, you will be redirected to the app page.
28+
4. Open the ‘Keys and Access Tokens’ tab.
29+
5. Copy ‘Consumer Key’, ‘Consumer Secret’, ‘Access token’ and ‘Access Token Secret’.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import re
2+
import tweepy
3+
from tweepy import OAuthHandler
4+
from textblob import TextBlob
5+
6+
class TwitterClient(object):
7+
def __init__(self):
8+
# keys and tokens from the Twitter Dev Console
9+
consumer_key = 'XXXXXXXXXXXXXXXXXXXXXXXX'
10+
consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
11+
access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'
12+
access_token_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXX'
13+
14+
# attempt authentication
15+
try:
16+
# create OAuthHandler object
17+
self.auth = OAuthHandler(consumer_key, consumer_secret)
18+
# set access token and secret
19+
self.auth.set_access_token(access_token, access_token_secret)
20+
# create tweepy API object to fetch tweets
21+
self.api = tweepy.API(self.auth)
22+
except:
23+
print("Error: Authentication Failed")
24+
25+
def clean_tweet(self, tweet):
26+
'''
27+
Utility function to clean tweet text by removing links, special characters
28+
using simple regex statements.
29+
'''
30+
return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])
31+
|(\w+:\/\/\S+)", " ", tweet).split())
32+
33+
def get_tweet_sentiment(self, tweet):
34+
'''
35+
Utility function to classify sentiment of passed tweet
36+
using textblob's sentiment method
37+
'''
38+
# create TextBlob object of passed tweet text
39+
analysis = TextBlob(self.clean_tweet(tweet))
40+
# set sentiment
41+
if analysis.sentiment.polarity > 0:
42+
return 'positive'
43+
elif analysis.sentiment.polarity == 0:
44+
return 'neutral'
45+
else:
46+
return 'negative'
47+
48+
def get_tweets(self, query, count = 10):
49+
'''
50+
Main function to fetch tweets and parse them.
51+
'''
52+
# empty list to store parsed tweets
53+
tweets = []
54+
55+
try:
56+
# call twitter api to fetch tweets
57+
fetched_tweets = self.api.search(q = query, count = count)
58+
59+
# parsing tweets one by one
60+
for tweet in fetched_tweets:
61+
# empty dictionary to store required params of a tweet
62+
parsed_tweet = {}
63+
64+
# saving text of tweet
65+
parsed_tweet['text'] = tweet.text
66+
# saving sentiment of tweet
67+
parsed_tweet['sentiment'] = self.get_tweet_sentiment(tweet.text)
68+
69+
# appending parsed tweet to tweets list
70+
if tweet.retweet_count > 0:
71+
# if tweet has retweets, ensure that it is appended only once
72+
if parsed_tweet not in tweets:
73+
tweets.append(parsed_tweet)
74+
else:
75+
tweets.append(parsed_tweet)
76+
77+
# return parsed tweets
78+
return tweets
79+
80+
except tweepy.TweepError as e:
81+
# print error (if any)
82+
print("Error : " + str(e))
83+
84+
def main():
85+
# creating object of TwitterClient Class
86+
api = TwitterClient()
87+
# calling function to get tweets
88+
tweets = api.get_tweets(query = 'Donald Trump', count = 200)
89+
90+
# picking positive tweets from tweets
91+
ptweets = [tweet for tweet in tweets if tweet['sentiment'] == 'positive']
92+
# percentage of positive tweets
93+
print("Positive tweets percentage: {} %".format(100*len(ptweets)/len(tweets)))
94+
# picking negative tweets from tweets
95+
ntweets = [tweet for tweet in tweets if tweet['sentiment'] == 'negative']
96+
# percentage of negative tweets
97+
print("Negative tweets percentage: {} %".format(100*len(ntweets)/len(tweets)))
98+
# percentage of neutral tweets
99+
print("Neutral tweets percentage: {} % \
100+
".format(100*(len(tweets) -(len( ntweets )+len( ptweets)))/len(tweets)))
101+
102+
# printing first 5 positive tweets
103+
print("\n\nPositive tweets:")
104+
for tweet in ptweets[:10]:
105+
print(tweet['text'])
106+
107+
# printing first 5 negative tweets
108+
print("\n\nNegative tweets:")
109+
for tweet in ntweets[:10]:
110+
print(tweet['text'])
111+
112+
if __name__ == "__main__":
113+
# calling main function
114+
main()

0 commit comments

Comments
 (0)