Twitter Sentiment Analysis using Python in Google Colab
What is Sentiment Analysis?
Sentiment analysis is a natural language processing task that involves classifying text based on the sentiment it expresses. It can be useful for a variety of applications, such as determining the sentiment of social media posts, analyzing customer reviews, or detecting the sentiment of customer interactions with a company.
To perform sentiment analysis you will typically follow these steps:
How do I perform Sentiment Analysis using Python?
There are several ways to perform sentiment analysis in Python. One approach is to use a pre-built library or tool that provides sentiment analysis capabilities. Some popular options for this include:
What is 'tweepy' and how can I use it for performing twitter Sentiment Analysis?
'tweepy' is a Python library for accessing the Twitter API. It allows developers to easily interact with the Twitter platform by providing a set of tools for performing common tasks, such as searching for tweets, posting updates, and retrieving user information.
Using tweepy, you can do things like:
Note that you will need to provide your own Twitter API credentials in order to run this code.
How do I get Twitter API credentials?
To get Twitter API credentials, you will need to create a Twitter developer account and apply for a developer account. Once your developer account has been approved, you can create a new app in your developer dashboard.
Once you have created your app, you will need to generate your API keys, which will allow you to access the Twitter API. To do this, go to the "Keys and Tokens" tab in your app settings and click on the "Generate" button next to the "Consumer Keys" section. This will generate a consumer key and consumer secret for your app, which you can use to authenticate with the Twitter API.
In addition to the consumer keys, you will also need to generate an access token and access token secret for your app. To do this, go to the "Keys and Tokens" tab in your app settings and click on the "Generate" button next to the "Access Tokens" section. This will generate an access token and access token secret for your app, which you can use to authenticate with the Twitter API.
Once you have generated your API keys and access tokens, you can use them to access the Twitter API and start building your application. It is important to keep your keys and tokens secure, as they provide access to the Twitter API on behalf of your app.
I don't have Python installed in my machine. Where do I start?
Ready to get your hands dirty but don't have have Python installed on your machine? Try Google Colab.
Google Colab, or Google Colaboratory, is a free, cloud-based Jupyter notebook environment that allows you to run and share Python code, as well as create and share documents that contain live code, equations, visualizations, and text. Colab is a great tool for data science and machine learning, as it allows you to build and run code in a web browser, without the need to install any software locally.
领英推荐
Colab is a powerful tool for data science and machine learning, and it's a great way to get started with Python programming. You can find more information about Colab, including tutorials and documentation, on the Google Colab website.
Here's an example of how to use Colab to run a simple Python script:
print("Hello, Colab!")
4. To run the code, click the "Run" button (or use the Shift + Enter keyboard shortcut).
This will execute the code and print "Hello, Colab!" in the output area below the cell.
Twitter Sentiment Analysis using Python?
Let's get into action
#?Import?necessary?libraries
import?tweepy
import?nltk
from?nltk.sentiment.vader?import?SentimentIntensityAnalyzer
#?Set?up?the?Twitter?API?client using credentials from Twitter Developer Acc
consumer_key?=?""
consumer_secret?=?""
access_token?=?""
access_token_secret?=?""
auth?=?tweepy.OAuthHandler(consumer_key,?consumer_secret)
auth.set_access_token(access_token,?access_token_secret)
api?=?tweepy.API(auth)
#?Set?up?the?sentiment?analyzer
analyzer?=?SentimentIntensityAnalyzer()
#?Search?for?tweets?about?chatgpt
tweets?=?api.search("chatgpt",lang="en",?count=100)
# Initialize?the?lists?to?save?tweets?based?on?the?polarity?score
neutral_list?=?[]
negative_list?=?[]
positive_list?=?[]
Total_tweets?=?0
#?Analyze?the?sentiment?of?the?tweets
for?tweet?in?tweets:
????#print(tweet.user)
????Total_tweets?+=1
????text?=?tweet.text ????
#?Get the polarity score for the tweet
???sentiment?=?analyzer.polarity_scores(text)
????
????if?sentiment["compound"]?>?0:
??????positive_list.append(text)
??????????????????
????elif?sentiment["compound"]?<?0:
??????negative_list.append(text)
????????????????
????elif?sentiment["compound"]?==?0:
??????neutral_list.append(text)
?
# Lets print the numbers?????
print(f'Total?tweets?:?{Total_tweets}')
print(f'Positive?tweets?:?{len(positive_list)}')????
print(f'Negative?tweets:?{len(negative_list)}')?????
print(f'Neutral?:?{len(neutral_list)}')?????
#?Print?all?positive?tweets
for?tweet?in?positive_list:
??print(tweet)
#?Print?all?negative?tweets
for?tweet?in?negative_list:
??print(tweet)s
Here we are restricting the number of tweets to 100 and the language to English. You can also restrict using the geocodes.
What is Polarity Score and how do I interpret the score?
The SentimentIntensityAnalyzer class in the nltk library in Python provides various methods for analyzing the sentiment of a piece of text. One of these methods is the polarity_scores method, which returns a dictionary containing various scores that represent the overall sentiment of the text.
The dictionary will have four keys: neg, neu, pos, and compound. These keys represent the negative, neutral, positive, and overall sentiment scores, respectively.
To interpret these scores, you can use the following guidelines:
? If the compound score is greater than 0, the text is considered to have a positive sentiment.
? If the compound score is less than 0, the text is considered to have a negative sentiment.
? If the compound score is equal to 0, the text is considered to have a neutral sentiment.
Let's analyze few examples from the results that I got when I ran the above snippet.
Positive tweets :
Negative Tweets :
It is important to note that no sentiment analysis tool is 100% accurate, and the accuracy of the results can vary depending on a number of factors. The accuracy of a sentiment analysis tool can be influenced by factors such as the quality of the training data, the complexity of the language being analyzed, and the context in which the text is used. In general, it is important to be aware of the limitations of any sentiment analysis tool and to carefully evaluate the results it produces.
P S : I referred ChatGPT for the sentiment analysis code. Quite impressive !