visit
$ pip install tweepy
$ git clone //github.com/tweepy/tweepy.git
$ cd tweepy
$ pip install .
$ pip install -U textblob
$ python -m textblob.download_corpora
.
├── API_KEYS.py
└── app.py
0 directories, 2 files
api_key = 'your api key'
api_secret_key = 'your api secret key'
from tweepy import API, OAuthHandler
from textblob import TextBlob
from API_KEYS import api_key, api_secret_key
To start fetching tweets from Twitter, firstly we have to authenticate our app using the API key and secret key.
To authenticate our API we will use OAuthHandler as shown below;
authentication = OAuthHandler(api_key, api_secret_key)
api = API(authentication)
public_tweets = api.search(Topic)
public_tweets is iterable of tweets objects but in order to perform sentiment analysis, we only require the tweet text.
Therefore in order to access text on each tweet, we have to use the text property on the tweet object just as shown in the example below.
from tweepy import API, OAuthHandler
from textblob import TextBlob
from API_KEYS import api_key, api_secre
authentication = OAuthHandler(api_key, api_secret_key)
api = API(authentication)
corona_tweets = api.search('corona virus')
for tweet in corona_tweets:
text = tweet.text
print(text)
$ python example.py
......
RT @amyklobuchar: So on Frontier Airlines you now have to pay an extra fee to keep yourself safe from corona virus. As I said today at the...
There are so many disturbing news these days ON TOP OF CORONA VIRUS. It just sinks my heart![😟](//s.w.org/images/core/emoji/12.0.0-1/svg/1f61f.svg) We all need therapy.
RT @ug_chelsea: Corona virus symptoms basically are the same feelings you get when your wife is checking your phone
Now Let's use TextBlob to perform sentiment analysis on those tweets to check out if they are positive or negative.
polarity = TextBlob(Text).sentiment.polarity
If the polarity is less than 0 it's negative
If the polarity is greater than 0 it's positive
from tweepy import API, OAuthHandler
from textblob import TextBlob
from API_KEYS import api_key, api_secret_key
def clean_tweets(tweet):
tweet_words = str(tweet).split(' ')
clean_words = [word for word in tweet_words if not word.startswith('#')]
return ' '.join(clean_words)
def analyze(Topic):
positive_tweets, negative_tweets = [], []
authentication = OAuthHandler(api_key, api_secret_key)
api = API(authentication)
public_tweets = api.search(Topic, count=10)
cleaned_tweets = [clean_tweets(tweet.text) for tweet in public_tweets]
for tweet in cleaned_tweets:
tweet_polarity = TextBlob(tweet).sentiment.polarity
if tweet_polarity<0:
negative_tweets.append(tweet)
continue
positive_tweets.append(tweet)
return positive_tweets, negative_tweets
positive, negative = analyze('Magufuli')
print(positive , '\n\n', negative)
print(len(positive), ' VS ', len(negative))
To change a topic you want to analyze or change the topic parameter in the analyze function to the topic of your interest.
Also, you can specify the number of tweets to be fetched from Twitter by changing the count parameter.When you run the above application it will produce results to what shown belowpython app.py..................... ['@o_abuga Obvious, the test kits the results are doubtful!! Magufuli said it']9 VS 1🎉🎉🎉 Congratulations you have just completed a tutorial on Twitter sentiment analysis using python, You should be proud of yourself, Tweet now to share this good news with your fellow developers.
I also recommend you to read this;Previously published at