It’s no secret that machines can now detect and measure and study our emotions from parsing text. One of ways this is achieved is through sentiment analysis. Just ask Siri, Google or Alexa. Here is how Wikipedia defines sentiment analysis: “Sentiment analysis (also known as opinion mining or emotion AI) refers to the use of natural language processing, text analysis, computational linguistics, and biometrics to systematically identify, extract, quantify, and study affective states and subjective information. Sentiment analysis is widely applied to voice of the customer materials such as reviews and survey responses, online and social media, and healthcare materials for applications that range from marketing to customer service to clinical medicine.” – https://en.wikipedia.org/wiki/Sentiment_analysis
With the surge of social media platform, sentiment analysis has really gained in popularity with businesses. Imagine for example being able to comb through comments on a product page, a Facebook page or a YouTube video and understanding if the comments are positive, neutral or negative. Imagine being able to identify within the comments which aspects people view as positive or negative. Equipped with this information, businesses can make improvement. Pre-Natural Text Analysis with machine learning, this would have hours of reading and categorizing by people. Now, a computer program can quickly comb through the text and within a few minutes, output the result. That’s the true power behind sentiment analysis.
How does Python do sentiment analysis?
We have released a series of Python courses to help young people pick up this much needed skill. If you have completed our Python 101, that’s enough skill to do what we are about to show you here. Don’t be intimidated. But first, let’s understand how sentiment analysis works.
For this code, we will use the TextBlob Python module. You can learn about this module here: https://textblob.readthedocs.io/en/dev/index.html. This module has abstracted a lot of the hard work so you can do sentiment analysis with just a couple lines of code. For sentiment, this module outputs 2 values:
- Polarity: this is a number between -1.0 and 1.0 which indicates on a scale whether the text expresses a negative sentiment (negative number) or a positive sentiment (positive number). The closer the number is to -1.0 or 1.0, the more negative or positive the text is respectively.
- Subjectivity: this is a number between 0.0 and 1.0 where 0.0 is very objective (facts) and 1.0 is very subjective (opinion)
Write your first sentiment analysis in Python
Your ready? Here is the code:
from textblob import TextBlob text = "I have been taking some Python courses at TekZone Academy and I really like it" blob = TextBlob(text) print(blob.sentiment) #output should give you something like: "Sentiment(polarity=0.2, subjectivity=0.2)"
If you get some errors, just make sure the textblob module is installed. You can easily do this by typing the command “pip3 install textblob” or “pip install textblob.” That’s it. You can play around with various text and see how the output changes. You can explore more functionality of the TextBlob by reading the page provided above.
Now you can start analysis emails and text from your friends. In a future post, we will look at how to use another nice plugin to automatically grab text from web pages. Those two modules combined can be quite powerful.
I’ll leave you with this article where you can find a bit more about applications of sentiment analysis: https://medium.com/manishmshiva/a-complete-guide-to-sentiment-analysis-and-its-applications-72adb3b057f5