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