This Python Tutorial shows you how to leverage textblob to read the tone and sentiment of people’s opinions behind some Twitter topics. It’s called Twitter sentiment analysis. This should be the second half chapter after the Twitter bot. You also need the Tweepy to scrape the post. By the end of the Python Tutorial, you master why sentiment analysis is valuable to your business and are able to implement the analysis yourself.
Python Libraries and Modules: Textblob, tweepy, pandas, csv
- Why Sentiment Analysis Is Vital for Business and Marketing?
- Tweepy and TextBlob
- Extract the Polarity and Subjectivity of Specific Queries
- Split into positive, neutral and negative by score
- Define a percentage function and Use
- Full Python Script of Twitter Sentiment Analysis
Why Sentiment Analysis Is Vital for the Business?
As a marketer or business man, you might be curious what people’s opinions are about most popular topics, products, and events. Perhaps as an analyst you wish to study the effect of your company’s recent brand marketing campaign. And most importantly, planning a content marketing campaign to engage with the customers need this. It’s because the right tone matching the sentiment and perception from customers’ mind is critical to succeed.
Sentiment analysis is extremely useful to help your business. It allows us to gain an overview of the wider public opinion behind certain topics, products, events and give you insight of content, and on-going campaign effect.
Meanwhile, you are able to find answers from the most important issue of a business from sentiment analysis. You can be based on the customer feedback, and tone of voice to adjust the strategy of a business. Meanwhile, you can observe and monitor your competitor branding and word of month as well. And absorbing what they did well, and leveraging what they did worse are super helpful.
In today’s environment, it’s totally feasible to collect the data and find the answers regarding the curiosity mentioned above. However, lunch might not for free, or even saying that people are now suffering from data overload. Businesses might have mountains of customer opinion collected. Yet for mere humans, it’s still impossible to analyze it manually without any sort of error or bias.
Luckily with Python, collecting and visualizing the sentiment analysis data in one hub is easy and automatic. And Twitter would be one of the most perfect social media channels to pump you the noise and voice data.
Tweepy and TextBlob
We’ve introduced and used Tweepy to build the Twitter Bot in the previous chapter. If you are interested in more details, please check out the article.
TextBlob is a python library and offers a simple API to access its methods and perform basic NLP tasks. A good thing about TextBlob is that they are just like python strings. So, you can transform and play with it just like we did in python.
Basically, TextBlob returns two main sentiment data of a sentence. The sentence can be from the blog, Q&A, and social post etc. They are polarity and subjectivity. In the quantitative perspective, these two essential methods are core and critical for you to analyse the generated dataset.
Polarity lies between
-1 defines a negative sentiment and 1 defines a positive sentiment. Negation words reverse the polarity. It has semantic labels that help with fine-grained analysis. For example — emoticons, exclamation marks, emojis, etc.
Subjectivity lies between
[0,1]. It quantifies the amount of personal opinion and factual information contained in the text. The higher subjectivity number means that the text contains more personal opinions.
TextBlob has one more parameter — intensity. TextBlob calculates subjectivity by looking at the ‘intensity’. Intensity determines if a word modifies the next word.
Extract the Polarity and Subjectivity of Specific Queries
Like building a Twitter bot for scraping specific topic content, sentiment analysis also needs to scrape the content first in Twitter. And the further step is to read through the sentiment information from the text rather than just finding the most popular ones.
It’s very straightforward by just using
TextBlob(). And then you can create the other two variables to get all posts’ polarity number and subjectivities’ number. Here are the codes:
Split into positive, neutral and negative by score
As mentioned, there is a range of number index to point out if a post is positive or negative from Polarity. In the previous paragraph, we had fetched the total Polarity number. So here we need to split out the positive, negative, neutral ones (the number is equal to zero) by using if conditions in the script
Define a percentage function and Use Round() method
Both positive and negative numbers can’t present a percentage out of all scraped posts. So you are not able to instantly find out the sentiment and the tone. Thus, you can create a def function which use the positive number divided by the total posts or negative number divided by the total posts.
What’s more, those number floats might have too much and make it hard to read if you use the float method in the def percentage function. So here you can plus and use the round method to shorten the number floats, such as 2, or 3.
Full Python Script of Twitter Sentiment Analysis
If you are interested in the full script ofTwitter Sentiment Analysis Using Tweepy & TextBlob, please subscribe to our newsletter by adding the message “Python Tutorial 35”. We would send you the script immediately to your mailbox.
I hope you enjoy reading Python Tutorial 35: Twitter Sentiment Analysis Using Tweepy & TextBlob. If you did, please support us by doing one of the things listed below, because it always helps out our channel.
- Support my channel through PayPal (paypal.me/Easy2digital)
- Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
- Follow and like my page Easy2Digital Facebook page
- Share the article to your social network with the hashtag #easy2digital
- Buy products with Easy2Digital 10% OFF Discount code (Easy2DigitalNewBuyers2021)
- You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount code on Buyfromlo products and digital software
- Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)