Sentiment Analysis is an application of natural language processing that is used to understand people’s opinions. Today, many companies use real-time sentiment analysis by asking users about their service. In this article, I’ll walk you through real-time sentiment analysis using Python.
Real-Time Sentiment Analysis
The main purpose of sentiment analysis is to analyze the opinions of users of a particular product or service, which helps customers understand the quality of the product. For example, every time Apple releases a new iPhone, we see a lot of people giving their opinion on it, some like it and some criticize it, in the end, all people’s opinions help us decide whether we should buy the new iPhone or not.
Opinions are nothing more than people’s feelings about a particular product, which helps a business improve its product and helps customers decide whether or not to buy the product. Now, what if you want to analyze people’s feelings in real-time, i.e. ask a user about your product and understand your product in real-time. In the section below, I’ll walk you through a simple way of real-time sentiment analysis using Python.
Real-Time Sentiment Analysis using Python
To analyze feelings in real-time, we need to request input from the user and then analyze user feelings given by him/her as input. So for this real-time sentiment analysis task using Python, I will be using the NLTK library in Python which is a very useful tool for all the tasks of natural language processing. So let’s import the NLTK library and start with sentiment analysis:
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import nltk
nltk.download('vader_lexicon')
So, I will be using the SentimentIntensityAnalyzer() class provided by the NLTK library in Python. Now let’s take a user input and have a look at the sentiment score:
user_input = input("Please Rate Our Services >>: ")
sid = SentimentIntensityAnalyzer()
score = sid.polarity_scores(user_input)
print(score)
Please Rate Our Services >>: great {'neg': 0.0, 'neu': 0.0, 'pos': 1.0, 'compound': 0.6249}
Final Step:
So the sentiments score looks like a dictionary with keys as ‘neg’, ‘neu’, ‘pos’, ‘compound’. The above output says that the sentiment of the user is 100% positive. So we can use an if-else statement by passing a condition that if the value of the key(neg) is not 0.0 then the sentiment is negative and otherwise it’s positive. So here is the complete Python code for real-time sentiment analysis:
user_input = input("Please Rate Our Services >>: ")
sid = SentimentIntensityAnalyzer()
score = sid.polarity_scores(user_input)
if score["neg"] != 0:
print("Negative")
else:
print("Positive")
Please Rate Our Services >>: not bad Positive Please Rate Our Services >>: not good Negative
So now we can see positive or negative as an output instead of the sentiment scores. I hope you liked this article on Realtime Sentiment Analysis using Python. Feel free to ask your valuable questions in the comments section below.