Sentiment analysis using NLTK SentimentIntensityAnalyzer and AFINN Lexicon

from nltk.sentiment import SentimentIntensityAnalyzer

# Create an instance of the SentimentIntensityAnalyzer
sia = SentimentIntensityAnalyzer()

# Load the AFINN-111.txt file
afinn_file = "/content/AFINN-en-165.txt"  # Replace with the actual path to the AFINN-111.txt file

# Read the AFINN-111.txt file and create a dictionary of word-score pairs
afinn_scores = {}
with open(afinn_file, encoding="utf8") as file:
    for line in file:
        word, score = line.strip().split("\t")
        afinn_scores[word] = int(score)

# Update the analyzer's lexicon with AFINN scores
sia.lexicon.update(afinn_scores)

# Example text
text = "I love using NLTK for natural language processing!"

# Perform sentiment analysis using the AFINN lexicon
sentiment = sia.polarity_scores(text)

# Print the sentiment scores
print(sentiment)

Output:

{'neg': 0.0, 'neu': 0.443, 'pos': 0.557, 'compound': 0.7424}

The sentiment scores provided for the text "I love using NLTK for natural language processing!" using the AFINN sentiment analyzer with NLTK are as follows:

  • Negative sentiment score (neg): 0.0

  • Neutral sentiment score (neu): 0.443

  • Positive sentiment score (pos): 0.557

  • Compound sentiment score (compound): 0.7424 The compound score is a normalized value that combines the positive and negative scores to give an overall sentiment score. In this case, the compound score of 0.7424 indicates a highly positive sentiment for the given text.