In this tutorial, I will guide you on how to detect emotions associated with textual data and how can you apply it in real-world applications. Understanding emotions associated with text is commonly known as . sentiment analysis You can apply it to perform analysis of customer feedback by directly reading them as either positive or negative feedback instead of manually reading to detect the emotions. Requirements There variety of libraries in python which can be used for natural language processing tasks including emotions detection from text including: (NLTK) Natural Language Toolkit Gensim. polyglot. TextBlob. CoreNLP. spaCy. Pattern. Vocabulary. Well based on simplicity and ease of getting started I have chosen to go with TextBlob throughout this tutorial. 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. TextBlob The good thing about it is its simplicity on getting started with natural language processing tasks. Installation # In Window pip install textblob python -m textblob.download_corpora #In Linux pip3 install textblob python3 -m textblob.download_corpora Let’s get started In order to perform textual analysis using we have to create a object as shown below: textblob textblob >>> textblob TextBlob >>>text = >>>blob_text = TextBlob(text) from import 'I had an awesome day' 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: TextBlob tags () method >>> textblob TextBlob >>>text = >>>blob_text = TextBlob(text) >>>tags = blob_text.tags print(tags) from import 'I had an awesome day' Output : [( , ), ( , ), ( , ), ( , ), ( , )] 'I' 'PRP' 'had' 'VBD' 'an' 'DT' 'awesome' 'JJ' 'day' 'NN' TextBlob Sentiment ( ) In order to perform sentiment analysis using we have to use sentiment ( ) method as shown below: textblob >>sentiment = blob_text.sentiment >>>print(sentiment) Sentiment(polarity= , subjectivity= ) 1.0 1.0 As we can see above, we call the sentiment () it returns a Textblob object Sentiment with polarity and subjectivity. TextBlob Polarity When building emotion analyzers we are more concerned on the polarity, therefore to get exactly polarity from Sentiment object we have to get it as its attribute: >>>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 versa. Use case (Demo Project) 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 to count negative feedbacks and negative feedbacks. textblob textblob TextBlob feedbacks = [ , , , , ] positive_feedbacks = [] negative_feedbacks = [] feedback feedbacks: feedback_polarity = TextBlob(feedback).sentiment.polarity feedback_polarity> : positive_feedbacks.append(feedback) negative_feedbacks.append(feedback) print( .format(len(positive_feedbacks))) print(positive_feedbacks) print( .format(len(negative_feedbacks))) print(negative_feedbacks) from import '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 ' for in if 0 continue 'Positive_feebacks Count : {}' 'Negative_feedback Count : {}' 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 : [ , ] Negative_feedback Count : [ , , ] 2 'I love the app is amazing ' 'This app is really helpful' 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 and share it will your fellow friends on , . twitter social media groups 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 Subscribe My Github Also published at https://kalebujordan.com/emotion-detection-from-text-python/