Juggling between coding languages? Let our Code Converter help. Your one-stop solution for language conversion. Start now!
Text-Based data is known to be abundant since it is generally practically everywhere, including social media interactions, reviews, comments and even surveys.
Sentences hold many valuable information that may have a huge impact on the decision making process of a given company, since it is a way to perform customer analytics to get to better know your users hence giving them better products in the future.
In this tutorial, we will learn on how to extract the sentiment score (-1 for negative, 0 for neutral and 1 for positive) from any given text using the vaderSentiment library.
VADER stands for Valence Aware Dictionary and sEntiment Reasoner, which is a lexicon and rule-based sentiment analysis tool that is specifically attuned to sentiments expressed in social media, and works well on text from other domains.
Installing the requirements for this tutorial:
pip install vaderSentiment
The nice thing about this library is that you don't have to train anything in order to use it, you'll soon realize that it is pretty straightforward to use it, open up a new Python file and import SentimentIntensityAnalyzer
class:
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
# init the sentiment analyzer
sia = SentimentIntensityAnalyzer()
We will create a list of sentences on which we will apply sentiment analysis using the polarity_score()
method from SentimentIntensity
Analyzer
class.
sentences = [
"This food is amazing and tasty !",
"Exoplanets are planets outside the solar system",
"This is sad to see such bad behavior"
]
for sentence in sentences:
score = sia.polarity_scores(sentence)["compound"]
print(f'The sentiment value of the sentence :"{sentence}" is : {score}')
polarity_score()
method returns a float for the sentiment strength based on the input text, the result of running the above code is the following:
The sentiment value of the sentence :"This food is amazing and tasty !" is : 0.6239
The sentiment value of the sentence :"Exoplanets are planets outside the solar system" is : 0.0
The sentiment value of the sentence :"This is sad to see such bad behavior" is : -0.765
We can also calculate the percentage of each sentiment present in that sentence using "pos", "neu" and "neg" keys after computing the polarity score.
for sentence in sentences:
print(f'For the sentence "{sentence}"')
polarity = sia.polarity_scores(sentence)
pos = polarity["pos"]
neu = polarity["neu"]
neg = polarity["neg"]
print(f'The percententage of positive sentiment in :"{sentence}" is : {round(pos*100,2)} %')
print(f'The percententage of neutral sentiment in :"{sentence}" is : {round(neu*100,2)} %')
print(f'The percententage of negative sentiment in :"{sentence}" is : {round(neg*100,2)} %')
print("="*50)
Output:
For the sentence "This food is amazing and tasty !"
The percententage of positive sentiment in :"This food is amazing and tasty !" is : 40.5 %
The percententage of neutral sentiment in :"This food is amazing and tasty !" is : 59.5 %
The percententage of negative sentiment in :"This food is amazing and tasty !" is : 0.0 %
==================================================
For the sentence "Exoplanets are planets outside the solar system"
The percententage of positive sentiment in :"Exoplanets are planets outside the solar system" is : 0.0 %
The percententage of neutral sentiment in :"Exoplanets are planets outside the solar system" is : 100.0 %
The percententage of negative sentiment in :"Exoplanets are planets outside the solar system" is : 0.0 %
==================================================
For the sentence "This is sad to see such bad behavior"
The percententage of positive sentiment in :"This is sad to see such bad behavior" is : 0.0 %
The percententage of neutral sentiment in :"This is sad to see such bad behavior" is : 47.6 %
The percententage of negative sentiment in :"This is sad to see such bad behavior" is : 52.4 %
==================================================
In this tutorial you have learned:
Learn also: How to Perform Text Classification in Python using Tensorflow 2 and Keras.
Happy Coding ♥
Save time and energy with our Python Code Generator. Why start from scratch when you can generate? Give it a try!
View Full Code Create Code for Me
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!