Skip to content
Related Articles

Related Articles

Emotion classification using NRC Lexicon in Python

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 03 Sep, 2021

Many a time, for real-world projects, emotion recognition is often just the start of the project. That time writing a whole code on that will not only increase time but also efficiency is hindered. 

NRCLexicon is an MIT-approved pypi project by Mark M. Bailey which predicts the sentiments and emotion of a given text. The package contains approximately 27,000 words and is based on the National Research Council Canada (NRC) affect lexicon and the NLTK library’s WordNet synonym sets.


To install this module type the below command in the terminal. 

pip install NRCLex

Even after the installation of this module, MissingCorpusError may occur while running programs. So it is advised to also install textblob.download_corpora by using the below command on the command prompt. 

python -m textblob.download_corpora


  • Import the module


# Import required modules
from nrclex import NRCLex

  • Assign input text


# Assigning list of words
text = ['hate', 'lovely', 'person', 'worst']

  • Create NRCLex object for each input text.


for i in range(len(text)):
    # creating objects
    emotion = NRCLex(text[i])

  • Apply methods to classify emotions.
1emotion.words Return words list.
2emotion.sentencesReturn sentences list.
3emotion.affect_listReturn affect list.
4emotion.affect_dictReturn affect dictionary.
5emotion.raw_emotion_scoresReturn raw emotional counts.
6emotion.top_emotionsReturn highest emotions.
7emotion.affect_frequenciesReturn affect frequencies.
  • Emotional affects measured include the following:
  1. fear
  2. anger
  3. anticipation
  4. trust
  5. surprise
  6. positive
  7. negative
  8. sadness
  9. disgust
  10. joy

Below is the Implementation.

Example 1:

Based on the above approach, the below example classifies various emotions using top_emotions.


# Import module
from nrclex import NRCLex
# Assign list of strings
text = ['hate', 'lovely', 'person', 'worst']
# 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)


Example 2:

Here a single emotion love is classified using all the methods of NCRLex module.


# Import module
from nrclex import NRCLex
# Assign emotion
text = 'love'
# Create object
emotion = NRCLex(text)
# Using methods to classigy emotion
print('\n', emotion.words)
print('\n', emotion.sentences)
print('\n', emotion.affect_list)
print('\n', emotion.affect_dict)
print('\n', emotion.raw_emotion_scores)
print('\n', emotion.top_emotions)
print('\n', emotion.affect_frequencies)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!