Sentiment Analysis Twitter

Overview:

1. Sentiment Analysis is a method to find the emotion behind an information.

2. It involves four steps: Data Collection, Data Processing, Data Analysis, and Data Visualization.

3. Here, we perform the Sentiment Analysis on Twitter Data using three tools called Zapier, Inference API, and Google Sheets.

4. The method described here works on automation, means, once we setup the process it automatically extract the tweet and writes its sentiment on Google Sheets.

Did you launch a product recently and now people are talking about it on Twitter?

Do you want to find out whether people love your product positively or negatively?

If your answer to both the questions above is Yes, then this article is for you.

Here I am going to explain you about a method that helps you to find out how people are talking about your product on Twitter.

The method I am going to explain to you is called Sentiment Analysis.

Here, I am going to explain to you, How you can perform the sentiment analysis of tweets on Twitter. And yes, to do the same, you may not be required to have coding knowledge.

Before moving on to drink the tasty juice, let me first explain to you a bit about Sentiment Analysis.

What is Sentiment Analysis?

Sentiment Analysis is a method to find out the emotion behind the information.

But how does Sentiment Analysis do the same?

Well, it uses Machine Learning and Natural Language Processing.

These days companies are using different Sentiment Analysis software to find out the popularity of their product.

Let’s learn how the process of Sentiment Analysis work.

What are the Steps of Sentiment Analysis?

There are basically four steps of Sentiment Analysis.

Data Collection: The first and the most important step is data collection because the quality of data gathered will affect the whole process.

Now, there are two ways to collect the data. The first way is the automated one where we use the APIs of the platform to easily collect the data and the second one is we manually enter the data.

In this article, we will use the API to perform the sentiment analysis of Twitter.

Data Processing: Once the data is collected, it is processed to be ready for the sentiment analysis.

Data Analysis: This is the actual step where sentiment analysis is performed. Here, the sentiment analysis software looks up each data of the information and understands its meaning.

Data Visualization: In this step, the software presents the final result in the easiest way to understand.

So, now we learn what is sentiment analysis and what is its process, it’s time to dive into performing the sentiment analysis of Twitter.

How to Perform the Sentiment Analysis of Tweets on Twitter?

First and foremost, the below process doesn’t require you to be a coder.

We are going to use three tools to complete the sentiment analysis of tweets.

  1. Zapier
  2. Inference API
  3. Google Sheets

The Process

With Zapier, we will create a Zap that will be triggered when someone mentioned the product name on Twitter.

Thereafter, the zap will ask the inference API to analyze the tweet and calculate its sentiment.

And lastly, the zap will populate the sentiment data into the Google Sheets.

Step 1: Importing the Tweets

First, we need to create a Zap on Zapier.

After creating the Zap, In the App & Event section select Twitter as the app and Search Mention as the Event.

Then, In the Twitter account section connect your Twitter account.

Now, mention the search term (the product) for which you want to perform the sentiment analysis. For example, I want to examine the sentiment analysis of Notion, that’s why I have chosen @NotionHQ (the official Twitter handle of Notion).

Lastly, test the trigger to confirm the zap is correctly fetching the tweets.

Step 2: Sentiment Analysis of Tweets

In this step, we are going to use the Inference API to perform the sentiment analysis of tweets.

However, to complete this step we need two things:

  1. Inference API Model ID
  2. Hugging Face Token ID

The Inference API Model ID is: “cardiffnlp/twitter-roberta-base-sentiment-latest

On the other hand, you can create a Hugging Face Token ID by first creating a free account on huggingface.co and then copying the token from here.

Now, that you have both the above IDs, let’s move on to the Zap again (that we have created).

In the Zap, we are now configuring the action part.

So, In the App event section select Code by Zapier.

And then In the even section select Run Python and click on Continue.

Now, we get into the Set up action section.

Here in the Input Data field we have two boxes.

In the first one fill: input_data and In the second one select Full Text.

Thereafter, In the code section, fill out the below 29 lines of Python code.

import requests

# Add the model ID and your Hugging Face Token
model = "cardiffnlp/twitter-roberta-base-sentiment-latest"
hf_token = "ADD_YOUR_HUGGING_FACE_TOKEN_HERE"

# Create the API request
API_URL = "https://api-inference.huggingface.co/models/" + model
headers = {"Authorization": "Bearer %s" % (hf_token)}
def analysis(payload):
  response = requests.post(API_URL, headers=headers, json=payload)
  while "is currently loading" in str(response.json()): # retry request while model loads
    print(response.json())
    time.sleep(30)
    response = requests.post(API_URL, headers=headers, json=payload)
  return response.json()

# Do the request to the Inference API and process the resutls
result = analysis({"inputs": str(input_data['input_data'])})
negative = result[0][0]['score']
neutral = result[0][1]['score']
positive = result[0][2]['score']

scores = [{'label': 'negative', 'score': negative}, {'label': 'neutral', 'score': neutral}, {'label': 'positive', 'score': positive}]
top_score = max(item['score'] for item in scores)
top_sentiment = next(item for item in scores if item["score"] == top_score)['label']

# Define the output for Zapier
output = [{'sentiment_label': top_sentiment, 'sentiment_score': top_score}]

Now, replace the hf_token code (present in line 5) with your own Hugging Face Token ID that you generated earlier.

After filling out all the above details, the set up action will look like what I have shown you in the screenshot below.

Yup! Don’t forget to click on the Continue Button.

Now, we have to perform the test of the action we have created which means whether the above code will successfully be able to test the sentiment of the tweet.

Once we confirm our above code successfully analyzes the sentiment of the tweet, we will now have to add one more action in the Zap to save the sentiment report in a Google Sheet.

Step 3: Save Sentiment Analysis Report on Google Sheets

In this last step, we are going to save the sentiment analysis report on a spreadsheet and then visualize the result with charts.

To do that, first, create a new spreadsheet on Google Sheets and mark the name of four columns with below name.

  1. Tweet: In this column, the sheet will contain the tweet extracted.
  2. Sentiment: This column will contain the sentiment of the tweet. For example, positive, negative, or neutral.
  3. Score: This column contains the value of the sentiment which means how accurate the sentiment score is.
  4. Date: This column contains the date of the tweet.

After creating the sheet, open the Zap again and select Google Sheets as the App and Create Spreadsheet Row as the event.

Now, In the choose account section, select the google account in which you have created the spreadsheet.

Moving onto the Set up action section, choose the My Drive in the drive section and then select the spreadsheet we have created.

In the worksheet field, choose Sheet1 and In the Tweet and Sentiment section, choose Full Text and Sentiment Label.

Lastly, In the Score and Date field choose Sentiment Score and Created At.

See the below screenshot for reference.

Now, we have to test the action as I have shown you in the screenshot below.

Now, sit tight as it will take some time to get your sheet populated with the tweets and their sentiment.

Conclusion

With the above technique, you can easily identify what people are thinking about a product or person on Twitter.

It can also help you to identify the popularity of a newly launched product on Twitter.

Do let me know in the comments down below what you are going to do with this technique. Also, if you have any doubts or queries, feel free to ask.

Code Credits: Federico Pascual

I am a Search Engine Optimization Specialist taming the elusive search engine algorithms for over 6 years now. I have created some pretty darn impressive SEO projects. While I'm not busy dominating the...

Leave a comment

Your email address will not be published. Required fields are marked *