LLM Applications
LLM for Sentiment Analysis — Understanding Opinions at Scale
Sentiment analysis enables machines to understand human opinions, emotions, and attitudes. LLMs have transformed this field by enabling nuanced understanding, aspect-based analysis, and emotion detection without task-specific training.
- Aspect-Based Sentiment — Analyzing sentiment toward specific aspects
- Emotion Detection — Identifying emotions beyond positive/negative
- Opinion Mining — Extracting structured opinions from text
Sentiment is the voice of the customer; analysis is the key to understanding.
LLM for Sentiment Analysis
Sentiment analysis (also called opinion mining) is the computational study of opinions, sentiments, and emotions expressed in text. LLMs have achieved state-of-the-art performance by understanding context, nuance, and implicit sentiment.
DfSentiment Analysis
Sentiment analysis is the process of automatically identifying and extracting subjective information from text, typically classifying text as positive, negative, or neutral, or detecting more fine-grained emotions and opinions.
Sentiment Analysis Types
Document-Level Sentiment
DfDocument-Level Sentiment
Document-level sentiment assigns a single sentiment label to an entire document, capturing the overall opinion expressed.
Sentence-Level Sentiment
DfSentence-Level Sentiment
Sentence-level sentiment assigns a sentiment label to each sentence in a document, capturing sentiment variation within the document.
Aspect-Based Sentiment
DfAspect-Based Sentiment Analysis (ABSA)
Aspect-based sentiment analysis identifies sentiment toward specific aspects or features mentioned in text. For example, in "The food was great but the service was terrible," the sentiment is positive toward food but negative toward service.
Emotion Detection
DfEmotion Detection
Emotion detection classifies text into emotion categories (e.g., joy, anger, sadness, fear, surprise) rather than simple positive/negative polarity.
| Type | Granularity | Use Case |
|---|---|---|
| Document | Overall | Review classification |
| Sentence | Per sentence | Paragraph-level analysis |
| Aspect | Per aspect | Product feature analysis |
| Emotion | Fine-grained | Customer support routing |
Mathematical Formulation
Classification-Based Sentiment
Sentiment Classification
Here,
- =Input text
- =Sentiment label
- =Text representation
- =Classification parameters
Aspect-Based Sentiment
ABSA Formulation
Here,
- =Aspect term
- =Input text
- =Sentiment for aspect a
- =Aspect-text representation
Multi-Label Emotion Detection
Multi-Label Emotion Classification
Here,
- =Binary indicator for emotion k
- =Number of emotion categories
- =Sigmoid function
- =Parameters for emotion k
LLM Approaches to Sentiment Analysis
Zero-Shot Classification
LLMs can classify sentiment without task-specific training by using prompt engineering.
Zero-Shot Sentiment Analysis
Prompt: "Classify the sentiment of the following review as positive, negative, or neutral:
'The camera quality is amazing, but the battery life is disappointing.'"
LLM Response: "Mixed (positive toward camera, negative toward battery)"
The LLM recognizes the nuance and provides a more detailed analysis than simple classification.
Aspect-Based Analysis with LLMs
ABSA with LLMs
Prompt: "Analyze sentiment for each aspect in the restaurant review:
'The pasta was delicious but the service was slow and the ambiance was nice.'
Provide aspect-sentiment pairs."
LLM Output:
- Pasta: Positive
- Service: Negative
- Ambiance: Positive
Emotion Detection
Emotion Detection with LLMs
Prompt: "Identify the primary and secondary emotions in the following text:
'I can't believe they cancelled the concert after I traveled all this way.'"
LLM Output:
- Primary emotion: Disappointment
- Secondary emotions: Frustration, sadness
Evaluation Metrics
Accuracy and F1
Weighted F1 Score
Here,
- =Number of classes
- =Number of samples in class k
- =Total number of samples
- =F1 score for class k
Aspect-Based Metrics
| Metric | Description | Use Case |
|---|---|---|
| Aspect Accuracy | Correct aspect classification | Aspect extraction |
| Sentiment Accuracy | Correct sentiment per aspect | Aspect sentiment |
| Macro F1 | Average F1 across aspects | Balanced evaluation |
| Micro F1 | Global F1 across all aspects | Imbalanced aspects |
Emotion Metrics
DfEmotion F1
For multi-label emotion detection, F1 is computed for each emotion independently, then averaged. Macro-averaging treats all emotions equally, while micro-averaging weights by frequency.
For sentiment analysis, consider class imbalance. Positive reviews often outnumber negative ones. Use macro-averaged metrics or class-weighted loss functions to handle imbalance.
Practical Implementation
Basic Sentiment Analysis
from transformers import AutoTokenizer, AutoModelForCausalLM
model_name = "meta-llama/Llama-3-8B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
review = """The new iPhone has an incredible camera and the battery
lasts all day, but the price is way too high for what you get."""
prompt = f"""Analyze the sentiment of the following review:
{review}
Provide:
1. Overall sentiment (positive/negative/neutral)
2. Key positive points
3. Key negative points
Analysis:"""
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=200)
analysis = tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=True)
print(analysis)
Aspect-Based Sentiment Analysis
def aspect_sentiment(text, aspects, model, tokenizer):
aspects_str = ", ".join(aspects)
prompt = f"""Analyze sentiment for each aspect in the following text:
Text: {text}
Aspects: {aspects_str}
Provide sentiment (positive/negative/neutral) for each aspect in JSON format:"""
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=150)
result = tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=True)
return json.loads(result)
# Example
text = "The hotel room was spacious and clean, but the breakfast was mediocre."
aspects = ["room", "breakfast"]
result = aspect_sentiment(text, aspects, model, tokenizer)
# {"room": "positive", "breakfast": "negative"}
Emotion Detection
EMOTIONS = ["joy", "sadness", "anger", "fear", "surprise", "disgust", "trust", "anticipation"]
def detect_emotions(text, model, tokenizer):
prompt = f"""Detect emotions in the following text.
Rate each emotion from 0-10:
Text: {text}
Emotions:"""
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=150)
result = tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=True)
return result
For production sentiment analysis, combine LLM-based approaches with traditional models for efficiency. Use LLMs for complex cases and faster models for straightforward classifications.
Advanced Techniques
Implicit Sentiment
DfImplicit Sentiment
Implicit sentiment is sentiment that is not explicitly stated but implied through context, word choice, or world knowledge. For example, "This phone is a brick" implies negative sentiment through metaphor.
LLMs excel at detecting implicit sentiment due to their world knowledge and contextual understanding.
Sarcasm Detection
Sarcasm in Sentiment
Text: "Oh great, another meeting that could have been an email." Literal sentiment: Positive ("great") Actual sentiment: Negative (sarcasm)
LLMs can detect sarcasm by understanding the mismatch between literal meaning and context.
Comparative Sentiment
DfComparative Sentiment
Comparative sentiment analyzes sentiment in comparisons between entities. For example, "iPhone is better than Android" expresses comparative preference.
Sentiment with Justifications
Justified Sentiment
Prompt: "Analyze sentiment and provide justification:
'The laptop overheats constantly and the keyboard feels cheap.'"
LLM Output:
- Sentiment: Negative
- Justification:
- "Overheats constantly" - indicates hardware quality issue
- "Keyboard feels cheap" - indicates poor build quality
- Overall tone expresses dissatisfaction
Domain-Specific Sentiment
Financial Sentiment
Financial sentiment analysis requires understanding market terminology and context.
Financial Sentiment
Text: "The company beat earnings expectations but issued a cautious outlook." Sentiment: Mixed (positive earnings, negative outlook)
Domain knowledge is essential to understand "beat expectations" (positive) and "cautious outlook" (negative).
Medical Sentiment
Medical sentiment analysis requires understanding clinical terminology and patient experiences.
Product Reviews
Product review sentiment often requires aspect-based analysis to capture feature-specific opinions.
Domain-specific sentiment analysis may require domain-adapted models or fine-tuning. General-purpose LLMs may misunderstand domain-specific terminology.
Best Practices
Prompt Engineering
- Clear instructions: Specify the sentiment categories and format
- Context provision: Include relevant domain context
- Examples: Provide example analyses for consistency
- Nuance handling: Allow for mixed or complex sentiments
Quality Assurance
- Inter-annotator agreement: Ensure consistent human labeling
- Regular calibration: Validate against human judgments
- Error analysis: Identify and address systematic errors
- Domain adaptation: Fine-tune or prompt for specific domains
For high-stakes applications like customer feedback analysis, combine automated sentiment analysis with human review for quality assurance.
Practice Exercises
-
Comparison: Compare zero-shot and few-shot sentiment analysis on a benchmark dataset. How many examples are needed for competitive performance?
-
Aspect Extraction: Build an aspect extraction system for restaurant reviews. What aspects are most commonly discussed?
-
Sarcasm Detection: Analyze how LLMs handle sarcastic text. What patterns help detect sarcasm?
-
Domain Transfer: Evaluate sentiment analysis performance across domains (e.g., electronics, restaurants, movies). What domain shifts affect performance?
Key Takeaways:
- Sentiment analysis ranges from document-level to aspect-based granularity
- LLMs enable zero-shot and few-shot sentiment analysis across domains
- Emotion detection provides finer-grained understanding than positive/negative
- Implicit sentiment and sarcasm require contextual understanding
- Domain-specific sentiment may require adaptation or fine-tuning
What to Learn Next
-> LLM for Recommendation Systems Conversational recommenders, preference learning, and cold start solutions.
-> LLM for Content Creation Creative writing, marketing copy, and content generation at scale.
-> LLM Compliance and Governance Regulatory compliance, audit trails, and data governance for LLMs.
-> LLM Testing Strategies Unit testing, integration testing, and regression testing for LLM systems.
-> LLM Capstone Project End-to-end LLM application project with design decisions and deployment.
-> LLM Research Paper Guide Key papers, reading guides, and research methodology for LLMs.