Hi guys,
In this tutorial, I will guide you on how to detect emotions associated with textual data using python and how can you apply it in real-world applications.
Understanding emotions associated with text is commonly known as sentiment analysis
where can you apply it ?
You can apply it to perform analysis of customer feedback by directly classifying and grouping them as either positive or negative feedback instead of manually doing it.
Requirements
There variety of libraries in python which can be used for natural language processing tasks including emotions detection from text including
let’s go on with text-blob
Well based on simplicity and ease of getting started I have chosen to go with TextBlob throughout a tutorial.
what is textblob?
TextBlob provides a simple API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more
Installation
# In Window pip install textblob python -m textblob.download_corpora #In Linux pip3 install textblob python3 -m textblob.download_corpora
Basics of textblob
In order to perform textual analysis using textblob, we have to create a textblob object as shown below;
>>>from textblob import TextBlob >>>text = 'I had an awesome day' >>>blob_text = TextBlob(text)
Once you have created a textblob object you can now access tons of textblob methods to manipulate textual data.
For example un tagging part of speech of a text can be as simple as shown below;
>>>from textblob import TextBlob >>>text = 'I had an awesome day' >>>blob_text = TextBlob(text) >>>tags = blob_text.tags print(tags)
Output :
[('I', 'PRP'), ('had', 'VBD'), ('an', 'DT'), ('awesome', 'JJ'), ('day', 'NN')]
Analyzing sentiment using textblob
In order to perform sentiment analysis using textblob we have to use the sentiment ( ) method as shown below;
>>sentiment = blob_text.sentiment >>>print(sentiment) Sentiment(polarity=1.0, subjectivity=1.0)
As we can see above as we call the sentiment () it returns a Textblob object Sentiment with polarity and subjectivity.
classifying polarity of text using textblob
In building our emotion analyzer we are more concerned about the polarity, we can determine the polarity of our text whether it is negative, positive, or neutral using by accessing the polarity attribute as shown below;
>>>polarity = sentiment.polarity >>>print(polarity) 1.0
Note:
The polarity of the textual data ranges from -1 to 1 , where negative polarity indicate negative emotions with -1 as mostly negative and vice verse
Classifying sentiment of users feedback using python- Demo Projects
Let’s assume we have our app which allows users to provide feedbacks If they like the user experience or not, and then we are going to use textblob to count positive feedbacks and negative feedbacks
from textblob import TextBlob feedbacks = ['I love the app is amazing ', "The experience was bad as hell", "This app is really helpful", "Damn the app tastes like shit ", 'Please don\'t download the app you will regret it '] positive_feedbacks = [] negative_feedbacks = [] for feedback in feedbacks: feedback_polarity = TextBlob(feedback).sentiment.polarity if feedback_polarity>0: positive_feedbacks.append(feedback) continue negative_feedbacks.append(feedback) print('Positive_feebacks Count : {}'.format(len(positive_feedbacks))) print(positive_feedbacks) print('Negative_feedback Count : {}'.format(len(negative_feedbacks))) print(negative_feedbacks)
Output :
Once you run the above code the below results with appear, the script with separate between negative and positive feedback given by the customer automatically as shown below
Positive_feebacks Count : 2 ['I love the app is amazing ', 'This app is really helpful'] Negative_feedback Count : 3 ['The experience was bad as hell', 'Damn the app tastes like shit ', "Please don't download the app you will regret it "]
Congratulations you performed emotion detection from text using Python, now don’t be shy share it will your fellow friends on Twitter, social media groups.
In case of anything comment, suggestion, or difficulty drop it in the comment and I will get back to you ASAP.
I recommend you to also read this;
- How to translate languages using Python
- 3 ways to convert speech to text in Python
- How to perform speech recognition in Python
- Make your own Plagiarism detector in Python
- Learn how to build your own spam filter in Python
- Make your own knowledge-based chatbot in Python
- How to perform automatic spelling correction in Python
- A Quick guide to Twitter sentiment analysis using python
Subscribe to this blog to stay updated on upcoming Python Tutorials, and also you can share
To get the whole code check it out here on My Github
How do you handle mixed cases, for example “The storyline in the movie was fantastic but the acting was so poor” which contain both a positive and negative sentiments. I don’t think it’s correct to just call this neutral (positive+negative=neutral) as there is clearly one positive sentiment and one negative sentiment