Twitter sentiment analysis with python library

With the help of twitter app credentials, i have started working on another set of sentiment analysis.

I have used couple of popular libraries this time.

  • TextBlob
  • Tweepy

TextBlob is used to check the polarity of the given text.

And tweepy is used to get the tweets from real-time data.

To use tweepy, first we need to create an app in twitter environment. This can be done in their developers site. Once app is created we need to get the following tokens from it.

api_key = 'XXXX'

api_key_secret = 'XXXX'

access_token = 'XXXX'

access_token_secret = 'XXXX'

You can find the entire main.py file content below.

from textblob import TextBlob
import tweepy

api_key = 'XXXX'
api_key_secret = 'XXXX'
access_token = 'XXXX'
access_token_secret = 'XXXX'

auth_handler = tweepy.OAuthHandler(consumer_key=api_key, consumer_secret=api_key_secret)
auth_handler.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth_handler)

search_terms = 'peace'
tweet_count = 200

tweets = tweepy.Cursor(api.search_tweets, q=search_terms, lang='en').items(tweet_count)

overall_polarity = 0
positive_polarity = 0
neutral_polarity = 0
negative_polarity = 0

for tweet in tweets:
cleaned_up_text = tweet.text.replace('RT', '')
if cleaned_up_text.startswith(' @'):
position = cleaned_up_text.index(':')
cleaned_up_text = cleaned_up_text[position + 2:]
if cleaned_up_text.startswith('@'):
position = cleaned_up_text.index(' ')
cleaned_up_text = cleaned_up_text[position + 2:]

analysis = TextBlob(cleaned_up_text)
overall_polarity += analysis.polarity

if analysis.polarity > 0.00:
positive_polarity += 1
elif analysis.polarity < 0.00:
negative_polarity += 1
print(cleaned_up_text)
elif analysis.polarity == 0.00:
neutral_polarity += 1

print(f'overall: {overall_polarity}')
print(f'positive: {positive_polarity}')
print(f'negative: {negative_polarity}')
print(f'neutral: {neutral_polarity}')

How to suppress Pandas Future warning ?

I’ve been working some code with python and doing some operation with pandas library.

I used to get future warning in the console like some method is going to changed or deprecated in the future.

So change it to something else like that.

For sometime, it was fine for me. Over period of time, it was bit annoying. Because I was looking for errors description or output in the console. But this future warning message is showing up regularly and reduced my focus towards main items I need to work on.

So I decided to hide those future warning message in the console for a moment. I was looking at the internet and found an generic way to do it.

By adding following line in the code, all the future warning has been hidden from the console.

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import pandas

Hope this will be helpful for someone in future or may be for myself.

Happy Coding!

Reference

https://github.com/pandas-dev/pandas/issues/2841#issuecomment-13382440


Related post -> Pandas “Can only compare identically-labeled DataFrame objects” error