Twitter sentiment analysis using python

I used to do data analysis regularly with different data.

By the way, I did my masters at BITS Pilani on Data Analytics. So I keep doing some analysis on data.

Example: Earlier I have analysed my skype history data. I captured some of fun facts like

  • How many times my name was pronounced wrongly?
  • Who is the leading person to do that?
  • How time good morning message is received in the evenings?
  • How many times received urgent call from contacts?
  • What is average, mean, min, max time people waited for me to reply hi to their hi message? (actually people should not wait for other end person to salute for their message)
  • Likewise, did some more analysis and captured some fun facts.

So now, the ongoing social media topic is “Elon Musk buys twitter”.

Lets do some sentiment analysis with twitter data.

I haven’t invloved in sentiment analysis before. I was referring internet to do those with the help of python libraries.

import re
import tweepy
from tweepy import OAuthHandler
from textblob import TextBlob

class TwitterClient(object):
	'''
	Generic Twitter Class for sentiment analysis.
	'''
	def __init__(self):
		'''
		Class constructor or initialization method.
		'''
		# keys and tokens from the Twitter Dev Console
		consumer_key = 'xxxxxxxxxxxxxxxxxxxxxx'
		consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxx'
		access_token = 'xxxxxxxxxxxxxxxxxxxxxx'
		access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxx

		# attempt authentication
		try:
			# create OAuthHandler object
			self.auth = OAuthHandler(consumer_key, consumer_secret)
			# set access token and secret
			self.auth.set_access_token(access_token, access_token_secret)
			# create tweepy API object to fetch tweets
			self.api = tweepy.API(self.auth)
		except:
			print("Error: Authentication Failed")

	def clean_tweet(self, tweet):
		'''
		Utility function to clean tweet text by removing links, special characters
		using simple regex statements.
		'''
		return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split())

	def get_tweet_sentiment(self, tweet):
		'''
		Utility function to classify sentiment of passed tweet
		using textblob's sentiment method
		'''
		# create TextBlob object of passed tweet text
		analysis = TextBlob(self.clean_tweet(tweet))
		# set sentiment
		if analysis.sentiment.polarity > 0:
			return 'positive'
		elif analysis.sentiment.polarity == 0:
			return 'neutral'
		else:
			return 'negative'

	def get_tweets(self, query, count = 10):
		'''
		Main function to fetch tweets and parse them.
		'''
		# empty list to store parsed tweets
		tweets = []

		try:
			# call twitter api to fetch tweets
			fetched_tweets = self.api.search_tweets(q = query, count = count)

			# parsing tweets one by one
			for tweet in fetched_tweets:
				# empty dictionary to store required params of a tweet
				parsed_tweet = {}

				# saving text of tweet
				parsed_tweet['text'] = tweet.text
				# saving sentiment of tweet
				parsed_tweet['sentiment'] = self.get_tweet_sentiment(tweet.text)

				# appending parsed tweet to tweets list
				if tweet.retweet_count > 0:
					# if tweet has retweets, ensure that it is appended only once
					if parsed_tweet not in tweets:
						tweets.append(parsed_tweet)
				else:
					tweets.append(parsed_tweet)

			# return parsed tweets
			return tweets

		except tweepy.TweepError as e:
			# print error (if any)
			print("Error : " + str(e))

def main():
	# creating object of TwitterClient Class
	api = TwitterClient()
	# calling function to get tweets
	tweets = api.get_tweets(query = 'elon musk bought twitter', count = 200)

	# picking positive tweets from tweets
	ptweets = [tweet for tweet in tweets if tweet['sentiment'] == 'positive']
	# percentage of positive tweets
	print("Positive tweets percentage: {} %".format(100*len(ptweets)/len(tweets)))
	# picking negative tweets from tweets
	ntweets = [tweet for tweet in tweets if tweet['sentiment'] == 'negative']
	# percentage of negative tweets
	print("Negative tweets percentage: {} %".format(100*len(ntweets)/len(tweets)))
	# percentage of neutral tweets
	print("Neutral tweets percentage: {} % \
		".format(100*(len(tweets) -(len( ntweets )+len( ptweets)))/len(tweets)))

	# printing first 5 positive tweets
	print("\n\nPositive tweets:")
	for tweet in ptweets[:10]:
		print(tweet['text'])
		print("-------------------")

	# printing first 5 negative tweets
	print("\n\nNegative tweets:")
	for tweet in ntweets[:10]:
		print(tweet['text'])

if __name__ == "__main__":
	# calling main function
	main()

Output

Connected to pydev debugger (build 213.7172.26)
Positive tweets percentage: 42.1875 %
Negative tweets percentage: 12.5 %
Neutral tweets percentage: 45.3125 % 		


Positive tweets:
@realDond_Trump It’s interesting how this new position came right after Elon Musk bought Twitter. Wow! When they co… https://t.co/Nua5PamFnJ
-------------------
RT @BelindaW75: So did all of you come to Twitter only after Elon Musk bought it? I already had an account but didn't really use it til Elo…
-------------------
@rtenews Irish billionaire Denis o brien owns most the Irish media.

Amazon creator Jeff benzos owns washington pos… https://t.co/uUs13EaSrd
-------------------
I and many other people bought into it. But the fact of the matter is, he can post as many silly memes on Twitter a… https://t.co/IBqdu1MDVt
-------------------
RT @OlympTrade: 🦾 Elon Musk bought 9.2% of the Twitter stock. The stock rose by more than 20% immediately after that. 
 
 🤑 Our traders mad…
-------------------
RT @amtvmedia: Elon Musk started PayPal that censors conservative political speech now he bought Twitter because free speech. Hahaha!! Amer…
-------------------
RT @hqfNFT: (5/10) With Twitter getting bought out by Elon Musk, he'd be pushing Twitter's boundaries in many aspects, and I believe one sm…
-------------------
RT @TrungTPhan: Larry Ellison and Elon Musk are buds.

Larry bought $1B of Tesla in December 2018. The stake is worth ~$15B now.

He just p…
-------------------
My hot take: Elon Musk bought twitter soley to delete @ElonJet instead of giving him this 50k
-------------------
RT @Freedom9369: Elon Musk just bought Instagram (after acquiring Twitter and Pepsi, plus owning the new Starlink Satellite System. Humm. W…
-------------------


Negative tweets:
RT @azidan_GOS: Elon Musk bought twitter yesterday, and in less than 24hours he has already changed the colour of the like and Retweet butt…
Arsenal is playing better Barca style than us right now.
Our last games were absolute dog shit, shocking to see the… https://t.co/3k1nLY1v2p
RT @Dream: Elon Musk bought Twitter when it’s literally free 🤣🤣😂😂🤣😝😝 what an idiot
RT @TimRunsHisMouth: It took the Biden administration less than 3 days to create a Ministry of Truth after Elon Musk bought Twitter...  Ima…
elon musk bought twitter and some weird troll kept banging on my youtube channel and not my brand site
@mfer4lyfe Maybe … anything can happen .. Elon Musk bought twitter for 4billy and if he’s been secretly buying into… https://t.co/zAup584dAA
@3nyoom @mfer4lyfe What if … Elon Musk has secretly been buying into Bayc and just bought twitter ; then he goes fo… https://t.co/aEMDzCviZq
RT @_TheShawn: Wait hold up, so you mean to tell me that we can tweet whatever we want now without worrying about a suspension since Elon M…

Process finished with exit code 0

Happy coding!

This is just a experiment I have made. I will keep posted the improvised edition once I complete.

Reference

https://www.geeksforgeeks.org/twitter-sentiment-analysis-using-python/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s