2

When I execute this code it terminates without any error but there is no any output. I have attached the right twitter authentication credentials.what would be the issue

Twitter-Sentiment-Analysis in python using tweepy and Textblob

from textblob import TextBlob
import sys, tweepy
import matplotlib.pyplot as plt

def percentage(part, whole):
    return 100* float(part)/float(whole)

consumerKey = "xxxxx"
consumerSecret = "xxxx"
accessToken = "xxxx"
accessTokenSecret = "xxxx"

auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)
api = tweepy.API(auth)

searchTerm = input("Enter keyword/ hashtag you want to search : ")
noOfSearchTerm = int(input("Enter how many tweets to analyze : "))

tweets = tweepy.Cursor(api.search, q=searchTerm, lang="English").items(noOfSearchTerm)


positive = 0
negative = 0
neutral = 0
polarity = 0

make textblob variable called analysis

for tweet in tweets:
    analysis = TextBlob(tweet.text)
    polarity += analysis.sentiment.polarity

    if (analysis.sentiment.polarity == 0):
        neutral += 1

    elif (analysis.sentiment.polarity < 0.00):
        negative += 1

    if (analysis.sentiment.polarity > 0.00):
        positive += 1

positive = percentage(positive, noOfSearchTerm)
negative = percentage(negative, noOfSearchTerm)
neutral = percentage(neutral, noOfSearchTerm)

positive = format(positive, '.2f')
neutral = format(neutral, '.2f')
negative = format(negative, '.2f')

print("How people are reacting on " + searchTerm + "by analysing " + str(noOfSearchTerm) + " Tweets.")

if(polarity == 0):
    print("Neutral")
elif (polarity < 0):
    print("Negative")
elif (polarity > 0):
    print("Positive")

labels = ['Positive ['+str(positive)+ '%]', 'Neutral [' + str(neutral) + '% ]', 'Negative [' + str(negative) + '%]']
sizes = [positive, neutral, negative]
colors = ['yellowgreen', 'gold', 'red']
patches, texts = plt.pie(sizes, colors=colors, startangle=90)
plt.legend(patches, labels, loc="best")
plt.title("How people are reacting on " + searchTerm + "by analysing " + str(noOfSearchTerm) + " Tweets.")
plt.axis('equal')
plt.tight_layout()
plt.show()
Hashan Malawana
  • 333
  • 1
  • 2
  • 10
  • 1
    Hashan Malawana - many thanks for coming up with your question - great thing. Plz Hashan - let us know what the wanted output is looking like. With that were able to help you even faster, even better and even more and more satisfying .. so we look forward to hear from you . plz describe your errors mote in depth going . thx in advance - have a creat day – zero May 07 '20 at 22:10
  • Hi thank you for your response.results I need to have is filtered tweets according to my query. It should be something like this - How people are reacting on + {searchTerm} + by analysing + {str(noOfSearchTerm)} + Tweets. – Hashan Malawana May 08 '20 at 14:04
  • 1
    good day dear Hashan - many thanks for the quick reply. Great to hear from you again. Well i like your code - it is very well written. I am a Python beginner - but i think that you have written a good piece of code. – zero May 08 '20 at 14:43

0 Answers0