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 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
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}')

Microsoft MVP award

I’ve been awarded as Microsoft MVP. This is second time in a row. For the first time, i didn’t wrote any blog post since it was pandemic and lockdown time.

But this time, i feel more special and energetic. I’m passing all good vibes to my friends and family.

Looking forward to contribute more to community and learn as well.

To know more about mvp award program please check this link

And also following goodies from Microsoft for past year contribution. All came together, what a co incident and surprise.

One advice i always give to myself. When life gives you opportunity, use it wisely ✌️

Happy sharing!