AI Course
Lesson 75: Sentiment Analysis
Sentiment Analysis is one of the most widely used applications of Natural Language Processing. It focuses on understanding the emotion or opinion expressed in text.
In simple terms, sentiment analysis answers one question: is the text positive, negative, or neutral?
Real-World Connection
Companies analyze customer reviews to understand satisfaction, brands track social media to monitor public opinion, and governments analyze feedback to understand citizen sentiment.
Whenever a product review is marked as positive or negative automatically, sentiment analysis is being used.
What Is Sentiment Analysis?
Sentiment analysis is the process of identifying emotional tone in text. The model learns patterns that indicate happiness, anger, disappointment, or neutrality.
- Positive sentiment — approval or satisfaction
- Negative sentiment — complaint or dissatisfaction
- Neutral sentiment — factual or emotionless text
How Sentiment Analysis Works
Modern sentiment analysis systems use pretrained language models like BERT, RoBERTa, or DistilBERT. These models understand context and word relationships instead of relying on simple keyword matching.
The model reads the entire sentence and predicts sentiment based on learned patterns.
Simple Sentiment Analysis Example
Below is an example of using a pretrained NLP model to analyze sentiment.
from transformers import pipeline
sentiment_model = pipeline("sentiment-analysis")
text = "Dataplexa explains AI concepts very clearly"
result = sentiment_model(text)
print(result)
Understanding the Code
The pipeline loads a pretrained sentiment analysis model. The input sentence is tokenized and passed through transformer layers.
The output contains the predicted sentiment label along with a confidence score that indicates how certain the model is.
Handling Negative Sentiment
Let’s analyze a negative example.
text = "The service was slow and disappointing"
result = sentiment_model(text)
print(result)
Common Use Cases
- Customer review analysis
- Social media monitoring
- Product feedback analysis
- Survey response analysis
Challenges in Sentiment Analysis
- Sarcasm and irony
- Mixed emotions in one sentence
- Domain-specific language
Advanced models reduce these issues by learning deeper context.
Practice Questions
Practice 1: What does sentiment analysis identify in text?
Practice 2: What sentiment does the sentence “I love this product” express?
Practice 3: Which models are commonly used for sentiment analysis today?
Quick Quiz
Quiz 1: Which NLP task detects opinions in text?
Quiz 2: What does the score in sentiment output represent?
Quiz 3: Which challenge is hardest for sentiment analysis?
Coming up next: Named Entity Recognition — identifying people, places, and organizations in text.