CW

LLM for Sentiment Analysis

ApplicationsSentiment AnalysisFree Lesson

Advertisement

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.

TypeGranularityUse Case
DocumentOverallReview classification
SentencePer sentenceParagraph-level analysis
AspectPer aspectProduct feature analysis
EmotionFine-grainedCustomer support routing

Mathematical Formulation

Classification-Based Sentiment

Sentiment Classification

P(yx)=textsoftmax(Wh(x)+b)P(y|x) = \\text{softmax}(W h(x) + b)

Here,

  • xx=Input text
  • yy=Sentiment label
  • h(x)h(x)=Text representation
  • W,bW, b=Classification parameters

Aspect-Based Sentiment

ABSA Formulation

P(yaa,x)=textsoftmax(Wh(a,x)+b)P(y_a | a, x) = \\text{softmax}(W h(a, x) + b)

Here,

  • aa=Aspect term
  • xx=Input text
  • yay_a=Sentiment for aspect a
  • h(a,x)h(a, x)=Aspect-text representation

Multi-Label Emotion Detection

Multi-Label Emotion Classification

P(y1,ldots,yKx)=prodk=1Ksigma(Wkh(x)+bk)P(y_1, \\ldots, y_K | x) = \\prod_{k=1}^{K} \\sigma(W_k h(x) + b_k)

Here,

  • yky_k=Binary indicator for emotion k
  • KK=Number of emotion categories
  • σ\sigma=Sigmoid function
  • Wk,bkW_k, b_k=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

textF1textweighted=sumk=1KfracnkncdottextF1k\\text{F1}_{\\text{weighted}} = \\sum_{k=1}^{K} \\frac{n_k}{n} \\cdot \\text{F1}_k

Here,

  • KK=Number of classes
  • nkn_k=Number of samples in class k
  • nn=Total number of samples
  • F1k\text{F1}_k=F1 score for class k

Aspect-Based Metrics

MetricDescriptionUse Case
Aspect AccuracyCorrect aspect classificationAspect extraction
Sentiment AccuracyCorrect sentiment per aspectAspect sentiment
Macro F1Average F1 across aspectsBalanced evaluation
Micro F1Global F1 across all aspectsImbalanced 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:
    1. "Overheats constantly" - indicates hardware quality issue
    2. "Keyboard feels cheap" - indicates poor build quality
    3. 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

  1. Clear instructions: Specify the sentiment categories and format
  2. Context provision: Include relevant domain context
  3. Examples: Provide example analyses for consistency
  4. Nuance handling: Allow for mixed or complex sentiments

Quality Assurance

  1. Inter-annotator agreement: Ensure consistent human labeling
  2. Regular calibration: Validate against human judgments
  3. Error analysis: Identify and address systematic errors
  4. 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

  1. Comparison: Compare zero-shot and few-shot sentiment analysis on a benchmark dataset. How many examples are needed for competitive performance?

  2. Aspect Extraction: Build an aspect extraction system for restaurant reviews. What aspects are most commonly discussed?

  3. Sarcasm Detection: Analyze how LLMs handle sarcastic text. What patterns help detect sarcasm?

  4. 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.

Advertisement

Need Expert LLM Help?

Get personalized tutoring, RAG system design, or production LLM consulting.

Advertisement