- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Emotion classification using NRC Lexicon in Python
Emotion identification or recognition is the ability of a person or an object to sense a particular emotion exhibited in the environment and place it into one of the many categories of emotions.
Emotion Classification in python is a feasible alternative to the traditional sentiment analysis technique which labels words or sentences as positive or negative and allots them with a polarity score accordingly.
The basic ideology behind this algorithm is to mimic the human thought process and it tries to segment the words portraying emotions from the text. The analysis is performed with a training set of data where a set of pre assumed information is fed to the system which acts as the basis for classification.
Here's the package based on the WordNet synonym sets from the NLTK library and the affect lexicon of the National Research Council of Canada (NRC), which has over 27,000 terms.
The following categories are used by the library to measure and classify words' emotional effects −
fear
anger
anticipation
trust
surprise
positive
negative
sadness
disgust
joy
Steps for installation
Step 1 − install the NRC module by using the pip install command in the terminal.
pip install NRCLex
Installing in a jupyter
notebook and command prompt usually follows the same steps if you’re using Windows.Installing in MacOs also follows the same command. Use the terminal directly.
Step 2 − install textblob in addition with nrclex to avoid facing MissingCorpusError
pip install textblob
Step 3 −download corpora from textblob
python -m textblob.download_corpora
After installation, we can go ahead and import the library and create a text object.
Elementary Methods
1. raw text to filtered text(for best results, 'text' should be unicode).
text_object.load_raw_text(text: str)
2. converts tokenized list of words into a list of tokens
text_object.load_token_list(list_of_tokens: list)
3. Return words list.
text_object.words
4. Return sentences list.
text_object.sentences
5. Return affect list.
text_object.affect_list
6. Return affect dictionaries.
text_object.affect_dict
7. Return raw emotional counts.
text_object.raw_emotion_scores
8. Return highest emotions.
text_object.top_emotions
9. Return frequencies.
Text_object.frequencies
Here, we have made use of the top_emotions function to classify a list of words based on emotions.
Algorithm
Step 1 − import nrclex import nrclex
Step 2 − from nrclex import NRCLex
Step 3 − initialize a list of strings-words that you’d like to classify
Step 4 − for i in range len(text)
Step 4 − emotion = NRCLex(text[i]) #creates an object for each text
Step 5 − emotion.top_emotions #classifies the emotion
Example
# Import module import nrclex from nrclex import NRCLex text = ['happy', 'beautiful', 'exciting', 'depressed'] # Iterate through list for i in range(len(text)): # call by object creation emotion = NRCLex(text[i]) # Classify emotion print('\n', text[i], ': ', emotion.top_emotions)
Output
innocent : [('trust', 0.5), ('positive', 0.5)] hate : [('fear', 0.2), ('anger', 0.2), ('negative', 0.2), ('sadness', 0.2), ('disgust', 0.2)] irritating : [('anger', 0.3333333333333333), ('negative', 0.3333333333333333), ('disgust', 0.3333333333333333)] annoying : [('anger', 0.5), ('negative', 0.5)]
Algorithm
Step 1 − import nrclex
Step 2 − from nrclex import NRCLex
Step 3 − initialize a list of strings-words that you’d like to classify
Step 4 − for i in range len(text)
Step 4 − emotion = NRCLex(text[i]) #creates an object for each text
Step 5 − emotion.top_emotions #classifies the emotion
Example
import nrclex from nrclex import NRCLex # Assign list of strings text = ['innocent','hate', 'irritating','annoying'] # Iterate through list for i in range(len(text)): # Create object emotion = NRCLex(text[i]) # Classify emotion print('\n\n', text[i], ': ', emotion.top_emotions)
Output
innocent : [('trust', 0.5), ('positive', 0.5)] hate : [('fear', 0.2), ('anger', 0.2), ('negative', 0.2), ('sadness', 0.2), ('disgust', 0.2)] irritating : [('anger', 0.3333333333333333), ('negative', 0.3333333333333333), ('disgust', 0.3333333333333333)] annoying : [('anger', 0.5), ('negative', 0.5)]
Conclusion
The NRC Emotion Lexicon is widely used in research and industry for sentiment analysis and emotion classification tasks. This means that there is a large community of users and resources available for support and further development. NRCLex has also managed to break the language barrier by providing a stable output for more than 100 languages worldwide with the help of google translate. This finds various applications in the health care sector in understanding pandemic responses. Practical applications include psychology and behavioral sciences, fake news detection and enhancing human-computer interaction.