Sentiment analysis using NLTK SentimentIntensityAnalyzer and AFINN Lexicon
Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He is studying at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).
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.

![Text Representation Basics for Natural Language Processing [Interactive Simulation]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1769996097089%2Fbe2ca449-7145-4a87-ae0b-c1ccd57960f2.webp&w=3840&q=75)
