Sentiment Analysis: Detecting Opinions and Emotions in Text
Learn how to detect positive, negative, and neutral sentiment in text using lexicon-based and machine learning approaches, with practical tips for accurate results.
Sentiment Analysis: Detecting Opinions and Emotions in Text
Sentiment analysis (also called opinion mining) automatically determines whether a piece of text expresses a positive, negative, or neutral opinion. It is used to analyze customer reviews, social media posts, survey responses, support tickets, and more.
---
Types of Sentiment Analysis
| Type | Output | Example |
|---|---|---|
| Polarity | Positive / Negative / Neutral | "Great product!" → Positive |
| Fine-grained | Very Positive / Positive / Neutral / Negative / Very Negative | Star ratings (1-5) |
| Emotion detection | Joy / Anger / Sadness / Fear / Surprise | "I'm furious about this delay" → Anger |
| Aspect-based | Sentiment per feature/aspect | "Battery is great but screen is terrible" → Battery: Positive, Screen: Negative |
Two Approaches to Sentiment Analysis
Approach 1: Lexicon-Based (Rule-Based)
Uses a dictionary of words with pre-assigned sentiment scores.
How it works:
- Look up each word in a sentiment dictionary
- Sum the scores: positive words add, negative words subtract
- The final score determines the sentiment
| Word | Score |
|---|---|
| "great" | +3 |
| "terrible" | -3 |
| "okay" | +1 |
| "disaster" | -4 |
| "not" | (negation modifier) |
- VADER (Valence Aware Dictionary and Sentiment Reasoner) — designed for social media, handles emojis, slang, and intensifiers
- AFINN — simple word list with scores from -5 to +5
- SentiWordNet — based on WordNet, fine-grained scores
| Pros | Cons |
|---|---|
| No training data needed | Limited to words in the dictionary |
| Fast and interpretable | Struggles with sarcasm and context |
| Works out of the box | Cannot learn domain-specific sentiment |
Approach 2: Machine Learning
Train a classifier on labeled sentiment data (text + positive/negative/neutral label).
How it works:
- Collect labeled data (e.g., product reviews with star ratings)
- Preprocess and extract features (TF-IDF, embeddings)
- Train a classifier (Naive Bayes, SVM, etc.)
- Predict sentiment on new text
| Pros | Cons |
|---|---|
| Learns domain-specific patterns | Needs labeled training data |
| Handles complex expressions | Slower to set up |
| Can be very accurate | May overfit on small datasets |
Using VADER in the Platform
VADER is the default lexicon-based analyzer in the platform. It is excellent for social media, reviews, and informal text.
VADER Scores
| Score | Range | Meaning |
|---|---|---|
| Positive | 0-1 | Proportion of positive sentiment |
| Negative | 0-1 | Proportion of negative sentiment |
| Neutral | 0-1 | Proportion of neutral sentiment |
| Compound | -1 to +1 | Overall sentiment (most useful) |
Interpreting Compound Score
| Compound Score | Sentiment |
|---|---|
| >= 0.05 | Positive |
| <= -0.05 | Negative |
| Between -0.05 and 0.05 | Neutral |
VADER Handles
- Capitalization: "GREAT" is stronger than "great"
- Punctuation: "Good!!!" is stronger than "Good"
- Negation: "not good" is negative
- Intensifiers: "very good" is stronger than "good"
- Emojis: :) is positive, :( is negative
How to Run Sentiment Analysis
In the Platform
- Go to NLP → Sentiment
- Select your dataset and text column
- Choose approach: Lexicon (VADER) or ML-based
- For ML-based: select a label column with sentiment labels
- Click Analyze
- Review results: sentiment scores per row, distribution chart, summary stats
Output Columns
| Column | What It Shows |
|---|---|
| sentimentpositive | Positive score (0-1) |
| sentimentnegative | Negative score (0-1) |
| sentimentneutral | Neutral score (0-1) |
| sentimentcompound | Overall score (-1 to +1) |
| sentiment_label | Positive / Negative / Neutral |
Improving Sentiment Analysis
| Strategy | How It Helps |
|---|---|
| Domain-specific lexicon | Add industry terms ("bullish" is positive in finance) |
| Custom training data | Label 200-500 examples from your domain |
| Aspect-based analysis | Separate sentiment by feature |
| Handle negation | "Not bad" = somewhat positive |
| Preprocessing | Clean text before analysis |
| Bigrams | Capture "not good" as a single feature |
Aspect-Based Sentiment
Instead of one sentiment per document, detect sentiment for each aspect/feature mentioned.
Example: "The food was delicious but the service was slow."
| Aspect | Sentiment |
|---|---|
| Food | Positive |
| Service | Negative |
How to Implement
- Identify aspects (manually or with NLP extraction)
- Extract sentences mentioning each aspect
- Run sentiment analysis on each extracted sentence
Challenges in Sentiment Analysis
| Challenge | Example | Why It Is Hard |
|---|---|---|
| Sarcasm | "Oh great, another delay" | Words are positive, meaning is negative |
| Negation | "Not bad at all" | "Bad" is negative but "not bad" is positive |
| Comparison | "Better than X but worse than Y" | Mixed sentiment |
| Domain specificity | "The stock is volatile" | Neutral in general, negative in finance? |
| Context dependence | "This is sick!" | Negative literally, positive in slang |
| Mixed sentiment | "Love the design, hate the price" | Both positive and negative |
Evaluation Metrics
If using ML-based sentiment:
| Metric | Use When |
|---|---|
| Accuracy | Balanced sentiment classes |
| F1 Score (macro) | Imbalanced classes |
| Confusion Matrix | See which sentiments are confused |
| Cohen's Kappa | Compare with human annotations |
---
Quick Reference
| Approach | Training Data? | Speed | Accuracy | Best For |
|---|---|---|---|---|
| VADER (lexicon) | No | Very fast | Good for social media | Quick analysis, informal text |
| ML (Naive Bayes) | Yes | Fast | Good | Domain-specific, formal text |
| ML (SVM/XGBoost) | Yes | Medium | Very good | High-accuracy needs |
| Deep Learning | Yes (lots) | Slow | Best | Large datasets, complex text |
What's Next?
- [Text Classification Guide](text-classification-guide) — General text classification
- [Topic Modeling Guide](topic-modeling-guide) — Discover themes in text
- [Text Preprocessing Guide](text-preprocessing-deep-dive) — Clean text for analysis
- [NLP Overview](nlp-overview) — All NLP capabilities
Related Articles
Natural Language Processing (NLP) Overview
A beginner-friendly guide to NLP: text preprocessing, BOW and TF-IDF, word clouds, text classification, sentiment analysis, and topic modeling.
Text Classification: Categorizing Text with Machine Learning
Learn how to classify text documents into categories using bag-of-words, TF-IDF, and ML models, with a complete workflow from data preparation to evaluation.
Text Preprocessing: Tokenization, Stop Words, Stemming, and Lemmatization
Learn the essential text preprocessing steps — tokenization, lowercasing, stop word removal, stemming, lemmatization, and regex cleaning — that prepare raw text for NLP analysis.
Topic Modeling: Discovering Themes in Text with LDA and NMF
Learn how to automatically discover topics in a collection of text documents using LDA and NMF, choose the right number of topics, and interpret results.
Was this helpful?