Skip to main content
Article
sentiment-analysisopinion-miningnlptext-classificationpythonnltkvadercustomer-feedback

Perform Sentiment Analysis on Text with Python & NLTK

Use Python and the NLTK library's VADER tool to quickly perform Sentiment Analysis. This skill classifies text polarity (positive, negative, neutral) and provides a compound score, ideal for analyzing customer feedback or social media posts.

beginner15 min5 steps
The play
  1. Install NLTK
    First, install the Natural Language Toolkit (NLTK), a foundational Python library for working with human language data. You will use it to access the pre-trained Sentiment Analysis model.
  2. Download the VADER Lexicon
    NLTK comes with many corpora and pre-trained models. For Sentiment Analysis, download the VADER (Valence Aware Dictionary and sEntiment Reasoner) lexicon, which is specifically tuned for sentiments expressed in social media.
  3. Analyze a Sentence's Polarity
    Import `SentimentIntensityAnalyzer`, create an instance of it, and use the `polarity_scores()` method on a string. This performs the core Sentiment Analysis, returning a dictionary of scores.
  4. Interpret the Compound Score
    The result includes negative, neutral, and positive scores representing the proportion of text in each category. The 'compound' score is a normalized, weighted composite. A common convention is: positive sentiment: compound score >= 0.05, neutral: > -0.05 and < 0.05, negative: <= -0.05.
  5. Batch Analyze Customer Feedback
    To apply Sentiment Analysis at scale, loop through a list of texts, such as customer reviews. This allows you to quickly categorize and quantify sentiment across a large dataset.
Starter code
import nltk
from nltk.sentiment.vader import SentimentIntensityAnalyzer

# This command only needs to be run once to download the lexicon
try:
    nltk.data.find('sentiment/vader_lexicon.zip')
except nltk.downloader.DownloadError:
    print('Downloading VADER lexicon...')
    nltk.download('vader_lexicon')

# 1. Initialize the analyzer
sia = SentimentIntensityAnalyzer()

# 2. Define a list of texts to analyze (e.g., customer feedback)
feedback_list = [
    "The new feature is amazing and so intuitive! Great job!",
    "I am extremely frustrated with the latest update. It's slow and buggy.",
    "The product is fine. It does what it says on the tin.",
    "Customer support was not helpful at all.",
    "I can't believe how much this has improved my workflow. Highly recommended!"
]

print("--- Running Sentiment Analysis on Customer Feedback ---")

# 3. Loop through feedback, perform analysis, and classify
for feedback in feedback_list:
    scores = sia.polarity_scores(feedback)
    compound = scores['compound']

    # 4. Determine sentiment category based on compound score
    sentiment = "Neutral"
    if compound >= 0.05:
        sentiment = "Positive"
    elif compound <= -0.05:
        sentiment = "Negative"

    print(f'\nFeedback: "{feedback}"')
    print(f'Sentiment: {sentiment} (Compound Score: {compound:.2f})')
    # print(f'Detailed Scores: {scores}') # Uncomment for full details
Perform Sentiment Analysis on Text with Python & NLTK — Action Pack