Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import tweepy
- from tweepy import OAuthHandler
- from textblob import TextBlob
- import matplotlib.pyplot as plt
- class Tweet_shit(object):
- def __init__(self):
- #Aici va puneti propriile credentiale
- consumer_key = '//////'
- consumer_secret = '//////'
- access_token = '//////'
- access_token_secret = '/////////'
- try:
- self.auth = OAuthHandler(consumer_key, consumer_secret)
- self.auth.set_access_token(access_token, access_token_secret)
- self.api = tweepy.API(self.auth)
- except:
- print("Error: Authentication Failed")
- def curata_tweet(self, tweet):
- return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split())
- def get_tweet_sentiment(self, tweet):
- analysis = TextBlob(self.clean_tweet(tweet))
- if analysis.sentiment.polarity > 0:
- return 'positive'
- elif analysis.sentiment.polarity == 0:
- return 'neutral'
- else:
- return 'negative'
- def get_tweets(self, query, count = 100):
- tweets = []
- try:
- fetched_tweets = self.api.search(q = query, count = count)
- for tweet in fetched_tweets:
- parsed_tweet = {}
- parsed_tweet['text'] = tweet.text
- parsed_tweet['sentiment'] = self.get_tweet_sentiment(tweet.text)
- if tweet.retweet_count > 0:
- if parsed_tweet not in tweets:
- tweets.append(parsed_tweet)
- else:
- tweets.append(parsed_tweet)
- return tweets
- except tweepy.TweepError as e:
- print("Error : " + str(e))
- def main():
- api = TwitterClient()
- tweets = api.get_tweets(query = 'Klaus Iohannis', count = 200)
- ptweets = [tweet for tweet in tweets if tweet['sentiment'] == 'positive']
- print("Procent tweet-uri pozitive: {} %".format(100*len(ptweets)/len(tweets)))
- ntweets = [tweet for tweet in tweets if tweet['sentiment'] == 'negative']
- print("Procent tweet-uri negative: {} %".format(100*len(ntweets)/len(tweets)))
- etweets = [tweet for tweet in tweets if tweet['sentiment'] == 'neutral']
- netweets = len(ntweets) + len(ptweets)
- neutraltweets = len(tweets) - netweets
- q = neutraltweets / len(tweets)
- print ("Procent tweet-uri neutre: {} %" .format(100*q))
- print("\n\nPositive tweets:")
- for tweet in ptweets[:10]:
- print(tweet['text'])
- print("\n\nNegative tweets:")
- for tweet in ntweets[:10]:
- print(tweet['text'])
- print("\n\nNeutral tweets")
- for tweet in etweets[:10]:
- print(tweet['text'])
- print('Neutre: ', len(etweets))
- print('Pozitive: ', len(ptweets))
- print('Negative: ', len(ntweets))
- fig = plt.figure()
- labels = 'Neutre', 'Pozitive', 'Negative'
- sizes = [len(etweets), len(ptweets), len(ntweets)]
- colors = ['gold', 'yellowgreen', 'lightcoral']
- explode = (0.1, 0, 0)
- plt.legend(patches, labels, loc="lower right")
- plt.title('Tweet-uri referitoare la Klaus Iohannis')
- plt.pie(sizes, explode=explode, labels=labels, colors=colors,
- autopct='%1.1f%%', shadow=True, startangle=140)
- plt.axis('equal')
- plt.show()
- fig.savefig('Klaus_Iohannis')
- plt.close()
- if __name__ == "__main__":
- main()
- #Succes!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement