Five Levels of Sentiment Analysis with Hugging Face: Unlocking Emotions

Article Image

Five Levels of Sentiment Analysis with Hugging Face: Unlocking Emotions

Every tweet, review, or conversation contains hidden emotional depths waiting to be unlocked. What if you could turn this emotional data into actionable insights with just a few lines of code?

Sentiment Analysis Defined

In an era where billions of data points are generated every second, sentiment analysis has become a vital tool for businesses, researchers, and organizations. Sentiment analysis—or opinion mining—is the process of identifying and categorizing emotions, opinions, or attitudes expressed in text, audio, or visual data. It bridges the gap between unstructured data and structured insights, helping us understand customer satisfaction, public sentiment, or even market trends.

From gauging social media reactions to improving customer experiences, sentiment analysis has grown into a cornerstone of decision-making across industries. However, it's no longer limited to classifying text as "positive" or "negative." The field has evolved to explore more nuanced and complex aspects, including multi-modal sentiment analysis that combines data from text, audio, and images for a richer emotional understanding.

The Hugging Face Advantage

Enter Hugging Face, the open-source juggernaut of natural language processing (NLP). Known for its robust Transformer-based models and seamless API integration, Hugging Face democratizes access to cutting-edge machine learning tools. Whether you're a seasoned data scientist or a developer taking your first steps in AI, Hugging Face's open-weight models empower you to explore and harness sentiment analysis at various levels.


Beyond Basic Sentiment Analysis


Imagine being able to not only classify feedback as "good" or "bad" but also identify the emotions behind it—joy, anger, surprise—or even the specific aspects of a product or service people love or dislike. Sentiment analysis at deeper levels lets businesses anticipate needs, personalize services, and design solutions that resonate emotionally with their audience. This article dives into the five progressive levels of sentiment analysis, showcasing how Hugging Face's open-weight models transform raw data into valuable insights.

Let’s unlock emotions, explore Hugging Face's powerful ecosystem, and revolutionize the way we interpret human expression.

Level 1: Polarity Detection

Polarity detection is the most basic form of sentiment analysis. It determines whether a piece of text expresses a positive, negative, or neutral sentiment. While this seems straightforward, it's the foundation of more advanced sentiment tasks.


nlptown/bert-base-multilingual-uncased-sentiment


This versatile model provides a multilingual capability for sentiment analysis. It classifies text into sentiment scores ranging from 1 star (most negative) to 5 stars (most positive).


Use Case Example

Consider monitoring customer reviews for an e-commerce platform. A simple polarity detection system can categorize reviews into positive and negative, helping businesses understand overall satisfaction trends.

from transformers import pipeline

# Load the sentiment analysis pipeline
sentiment_pipeline = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")

# Example sentences
texts = [
  "I absolutely love this product!",
  "It was okay, but nothing special.",
  "Terrible experience, would not recommend."
]

# Perform sentiment analysis

for text in texts:
    result = sentiment_pipeline(text)
    print(f"Text: {text}")
    print(f"Sentiment: {result}\n")

Expected Output

Text: I absolutely love this product!
Sentiment: [{'label': '5 stars', 'score': 0.95}]

Text: It was okay, but nothing special.
Sentiment: [{'label': '3 stars', 'score': 0.89}]

Text: Terrible experience, would not recommend.
Sentiment: [{'label': '1 star', 'score': 0.98}]

Alternative Tool
TextBlob, for a simpler, lexicon-based comparison:

from textblob import TextBlob

texts = [
    "I absolutely love this product!",
    "It was okay, but nothing special.",
    "Terrible experience, would not recommend."
]
for text in texts:
    blob = TextBlob(text)
    print(f"Text: {text}")
    print(f"Polarity: {blob.sentiment.polarity}\n")  # Polarity ranges from -1 (negative) to 1 (positive)


Use Cases of Polarity Detection

  1. Social Media Monitoring: Track the sentiment of brand mentions or trending topics in real time.
  2. Customer Feedback Analysis: Identify overall satisfaction from reviews, surveys, or support tickets.
  3. Brand Reputation Management: Flag negative comments or reviews for faster response.

Challenges:

  • Sarcasm: "Oh great, another delayed delivery!" might sound positive but expresses frustration.
  • Context Dependence: Words like "cold" could imply negative sentiment for a restaurant but positive sentiment for a beer.
  • Neutral Sentiment: Sometimes, the model struggles to detect truly neutral tones, leading to misclassification.


Level 2: Emotion Detection


Emotion detection goes beyond basic polarity detection by categorizing text into specific emotional states such as joy, sadness, anger, fear, surprise, or disgust. It provides deeper insights into how individuals feel rather than just determining if the sentiment is positive, negative, or neutral. Emotion detection plays a vital role in understanding human behavior, building empathetic AI systems, and personalizing user experiences.

Hugging Face Model: nateraw/bert-base-uncased-emotion


This fine-tuned model detects a range of emotions, offering a granular understanding of text inputs. It can identify emotions like joy, sadness, anger, fear, surprise, and disgust, making it a powerful tool for analyzing nuanced emotional states.

Use Case Example:


Suppose you're running a mental health support chatbot. Emotion detection can help identify users who might be expressing sadness or anger and route them to appropriate resources or responses.

from transformers import pipeline
# Load the emotion detection pipeline

emotion_pipeline = pipeline("text-classification", model="nateraw/bert-base-uncased-emotion"

# Example texts

texts = [
    "I am so happy today! Everything is going perfectly.",
    "I'm really scared about the upcoming presentation.",
    "Why do things always go wrong? I'm so frustrated.",
    "This surprise party was absolutely amazing. I didn't see it coming!"
]

# Perform emotion detection

for text in texts:
    result = emotion_pipeline(text)
    print(f"Text: {text}")
    print(f"Emotion: {result}\n")

Expected Output
Text: I am so happy today! Everything is going perfectly.
Emotion: [{'label': 'joy', 'score': 0.97}]

Text: I'm really scared about the upcoming presentation.
Emotion: [{'label': 'fear', 'score': 0.92}]

Text: Why do things always go wrong? I'm so frustrated.
Emotion: [{'label': 'anger', 'score': 0.85}]

Text: This surprise party was absolutely amazing. I didn't see it coming!
Emotion: [{'label': 'surprise', 'score': 0.89}]

Alternative Tool: NRCLex
NRCLex is a lexicon-based tool for emotion detection. It matches words in a text with predefined emotional categories from a dictionary. Here’s how you can use it:

from nltk.corpus import wordnet
from collections import Counter

# A sample EmoLex-like dictionary for simplicity
emolex = {
    "happy": "joy",
    "scared": "fear",
    "wrong": "anger",
    "amazing": "surprise",
    "perfectly": "joy",
}


# Example texts
texts = [
    "I am so happy today! Everything is going perfectly.",
    "I'm really scared about the upcoming presentation.",
]
for text in texts:
    words = text.lower().split()
    emotions = [emolex.get(word, None) for word in words if word in emolex]
    emotion_count = Counter(filter(None, emotions))
    print(f"Text: {text}")
    print(f"Emotion Count: {emotion_count}\n")

Applications of Emotion Detection
  1. Mental health support: Identify users expressing sadness, anger, or fear to provide targeted help or escalate issues to human responders.
  2. Customer Service Automation: Detect anger or frustration in customer complaints and prioritize those tickets for quicker resolution.
  3. Personalized Content Recommendations: Recommend uplifting content to users who express sadness or tailor content to match the user's emotional state.
  4. Social Media Analysis: Track public sentiment on topics by monitoring emotional reactions to news, trends, or campaigns.

Visualization: Emotional Spectrum

Emotion detection often maps emotions onto a visual spectrum. A popular choice is Plutchik's Wheel of Emotions, which organizes emotions into a color-coded wheel for intuitive understanding.

Plutchik's Wheel of Emotions

  1. Primary Emotions: Focus on core emotions like joy, anger, sadness, and fear.
  2. Intensity Levels: Visualize how emotions intensify, e.g., joy → ecstasy, anger → rage.
  3. Interactive Applications: Use the wheel to map detected emotions in real-time for dashboards or reports.
Challenges of Emotion Detection
  1. Cultural Differences:
    Emotions can be expressed differently across languages and cultures, affecting model accuracy.
  2. Sarcasm and Ambiguity:
    Emotion models struggle with texts like "I'm so thrilled about failing the exam," which is sarcastic but uses positive words.
  3. Emotion Overlap:
    Some texts express multiple emotions simultaneously, e.g., "I'm excited but also nervous," requiring multi-label classification.

Level 3: Aspect-Based Sentiment Analysis

Aspect-based sentiment analysis (ABSA) adds a new layer of granularity to sentiment analysis. Instead of evaluating sentiment for an entire text, ABSA identifies and evaluates sentiment for specific aspects or features within the text. For example, in a product review, ABSA can determine the sentiment toward specific features like "battery life" or "screen resolution."

This deeper understanding helps businesses pinpoint areas for improvement, track customer preferences for particular features, and tailor their offerings based on specific feedback.

Hugging Face Model: siebert/sentiment-roberta-large-english

The siebert/sentiment-roberta-large-english model is a fine-tuned RoBERTa model that can be adapted for aspect-based sentiment analysis. By combining this model with datasets like SemEval, developers can train it to extract and classify sentiment for specific aspects.

Use Case Example

Imagine a hotel chain monitoring customer reviews to evaluate sentiment about specific features such as "cleanliness," "staff friendliness," or "location." ABSA can help highlight areas of strength or weakness, enabling targeted improvements.

Aspect-Based Sentiment Analysis
from transformers import pipeline
# Load sentiment analysis pipeline

aspect_pipeline = pipeline("text-classification", model="siebert/sentiment-roberta-large-english")

# Example text and aspects

reviews = [

    {"text": "The battery life is fantastic, but the camera quality is poor.", "aspects": ["battery life", "camera quality"]},

    {"text": "The room was spacious, but the staff was rude.", "aspects": ["room", "staff"]},
]


# Perform aspect-based sentiment analysis
for review in reviews:
    print(f"Review: {review['text']}")
    for aspect in review["aspects"]:
        aspect_text = f"The sentiment about {aspect} is: {review['text']}"
        result = aspect_pipeline(aspect_text)
        print(f"  Aspect: {aspect}")
        print(f"  Sentiment: {result}\n")

Expected Output

Review: The battery life is fantastic, but the camera quality is poor.
  Aspect: battery life
  Sentiment: [{'label': 'positive', 'score': 0.97}]

  Aspect: camera quality
  Sentiment: [{'label': 'negative', 'score': 0.89}]

Review: The room was spacious, but the staff was rude.
  Aspect: room
  Sentiment: [{'label': 'positive', 'score': 0.92}]

  Aspect: staff
  Sentiment: [{'label': 'negative', 'score': 0.87}]

Alternative Tool: SpaCy

SpaCy, combined with dependency parsing, can identify aspects and related terms in a text. While not as robust as Transformers, it's lightweight and easy to implement.

import spacy
from spacy.tokens import Doc
from collections import defaultdict

# Load SpaCy model
nlp = spacy.load("en_core_web_sm")

# Example text
text = "The battery life is fantastic, but the camera quality is poor."

# Parse the text
doc = nlp(text)

# Extract aspects and opinions
aspects = defaultdict(list)
for token in doc:
    if token.dep_ in {"amod", "acomp"}:  # Adjectives or descriptive modifiers
        aspects[token.head.text].append(token.text)

# Display aspects and opinions
print("Aspects and Opinions:")
for aspect, opinions in aspects.items():
    print(f"Aspect: {aspect}, Opinions: {opinions}")

Expected Output:
Aspects and Opinions:
Aspect: battery, Opinions: ['fantastic']
Aspect: camera, Opinions: ['poor']

Applications of Aspect-Based Sentiment Analysis
  1. Product reviews: Identify customer opinions about specific features like "design," "performance," or "usability."
  2. Hotel and Travel Feedback: Analyze sentiments about "cleanliness," "location," or "food" in guest reviews.
  3. Social Media Monitoring: Track sentiment on specific topics or features mentioned in tweets or comments.
  4. Healthcare: Analyze patient feedback for aspects like "wait time," "staff attitude," or "facility quality."

Challenges of Aspect-Based Sentiment Analysis
  1. Aspect Extraction: Extracting aspects automatically can be challenging in unstructured text. Dependency parsing or named entity recognition (NER) may help.
  2. Aspect Ambiguity: Words like "battery" or "screen" might have different meanings depending on the context, leading to misclassification.
  3. Multi-Aspect Sentiments: Sentences often express mixed sentiments about different aspects, requiring nuanced analysis.

Level 4: Intention Analysis

Intention analysis identifies the purpose or goal behind a statement, question, or request. While sentiment analysis tells us how someone feels, intention analysis reveals what they want or aim to do. For example, in a customer service context, intention analysis could classify an inquiry as a "refund request," "product complaint," or "feature inquiry."

This deeper understanding helps businesses predict customer behavior, personalize recommendations, and improve decision-making across domains like e-commerce, healthcare, and customer support.

Hugging Face Model: facebook/bart-large-mnli

The facebook/bart-large-mnli model is pre-trained for natural language inference (NLI) and can be fine-tuned for intent classification tasks. NLI tasks involve determining whether a hypothesis is entailed, contradicted, or neutral given a premise, which is conceptually similar to intent classification.

Use Case Example

Consider an e-commerce chatbot that needs to classify customer intents like:

  • "Where is my package?" → Order Tracking
  • "I want a refund for my purchase." → Refund Request
  • "Can I get details about this product?" → Product Inquiry

With intention analysis, the chatbot can automatically route queries to appropriate workflows, improving efficiency and customer satisfaction.

Colab Example: Intention Analysis Using Hugging Face

from transformers import pipeline
# Load the zero-shot classification pipeline

intent_pipeline = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")

# Example intents

queries = [
    "I want to return a defective item.",
    "Can you help me find a new laptop?",
    "What are the delivery charges for international orders?",
]

# Define possible intents

intents = ["refund request", "product inquiry", "shipping information", "complaint"]

# Perform intention analysis

for query in queries:
    result = intent_pipeline(query, candidate_labels=intents)
    print(f"Query: {query}")
    print(f"Intent: {result['labels'][0]} (Score: {result['scores'][0]:.2f})\n")

Expected Output

Query: I want to return a defective item.
Intent: refund request (Score: 0.85)

Query: Can you help me find a new laptop?
Intent: product inquiry (Score: 0.92)

Query: What are the delivery charges for international orders?
Intent: shipping information (Score: 0.88)

Fine-Tuning facebook/bart-large-mnli on Custom Data

If you have a custom dataset with labeled intents, you can fine-tune the model to improve performance for your specific use case.

from transformers import BartForSequenceClassification, BartTokenizer
from transformers import Trainer, TrainingArguments
import torch
from datasets import load_dataset

# Load dataset
dataset = load_dataset("csv", data_files={"train": "train.csv", "test": "test.csv"})

# Load tokenizer and model
tokenizer = BartTokenizer.from_pretrained("facebook/bart-large-mnli")
model = BartForSequenceClassification.from_pretrained("facebook/bart-large-mnli", num_labels=3)


# Tokenize dataset
def preprocess(data):
    return tokenizer(data["text"], padding="max_length", truncation=True)

encoded_dataset = dataset.map(preprocess, batched=True)

# Define training arguments

training_args = TrainingArguments(
    output_dir="./results",
    evaluation_strategy="epoch",
    learning_rate=2e-5,
    per_device_train_batch_size=16,
    num_train_epochs=3,
    weight_decay=0.01,
    save_strategy="epoch",
)


# Train model
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=encoded_dataset["train"],
    eval_dataset=encoded_dataset["test"],
)

trainer.train()

Alternative Tool: Scikit-learn

You can build a simple intent classifier using traditional machine learning techniques like TF-IDF and a logistic regression model.

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split

# Example dataset

data = [
    ("I need help with a return.", "refund request"),
    ("What are your delivery options?", "shipping information"),
    ("Tell me about the latest smartphones.", "product inquiry"),
    ("The product I received is defective.", "complaint"),
]
texts, labels = zip(*data)

# Split dataset

X_train, X_test, y_train, y_test = train_test_split(texts, labels, test_size=0.2, random_state=42)

# Build and train the model

pipeline = make_pipeline(TfidfVectorizer(), LogisticRegression())
pipeline.fit(X_train, y_train)

# Test the model

print(f"Predicted Intent: {pipeline.predict(['I want to return a product.'])[0]}")

Expected Output:

Predicted Intent: refund request

Applications of Intention Analysis
  1. Customer Support: Automate routing of customer queries to appropriate teams or workflows. Identify urgent cases (e.g., complaints) for priority handling.
  2. E-commerce: Classify user intents to provide personalized recommendations or targeted promotions. Analyze patient inquiries for scheduling, prescription refills, or symptom checks.
  3. Sales Automation: Detect purchase intent from user queries to drive conversions.

Challenges of Intention Analysis
  1. Ambiguity: User queries can be vague or ambiguous, making it difficult to determine intent without additional context.
  2. Overlapping Intents: Some queries may express multiple intents simultaneously, requiring multi-label classification.
  3. Domain-Specific Data: Pre-trained models may require fine-tuning on domain-specific datasets for accurate predictions.

Level 5: Multimodal Sentiment Analysis

Multimodal sentiment analysis represents the frontier of emotional intelligence in AI, combining insights from text, audio, and images to capture emotions more holistically. Unlike traditional sentiment analysis, which focuses on a single data type, multimodal approaches analyze multiple input sources together, such as interpreting a social media post’s text alongside its attached image and audio clip.

By integrating information from diverse modalities, multimodal sentiment analysis unlocks deeper insights into user emotions, enabling applications in entertainment, healthcare, social media, and beyond.

Hugging Face Models for Multimodal Analysis
  1. Text Analysis: nlptown/bert-base-multilingual-uncased-sentiment
    Analyze the text for its sentiment polarity and emotional tone.
  2. Audio Analysis: facebook/wav2vec2-base-960h
    Transcribe and analyze speech to infer sentiment from audio recordings.
  3. Image Analysis: google/vit-base-patch16-224
    Classify or analyze emotional elements within visual data.

Use Case Example

Imagine a customer posting a video review of a product on social media. The video includes:

  • Speech: Compliments and critiques about the product.
  • Facial Expressions: Reflecting satisfaction or disappointment.
  • Visual Context: Product images or the reviewer’s setting.

By analyzing text (transcribed speech), audio tone, and visual content together, multimodal sentiment analysis provides a comprehensive understanding of the review.

Colab Example: Multimodal Sentiment Analysis

This example demonstrates how to combine Hugging Face models for multimodal sentiment analysis using text, audio, and images.


from transformers import pipeline, Wav2Vec2Processor, Wav2Vec2ForCTC, ViTForImageClassification, ViTImageProcessor
from transformers import BertForSequenceClassification, BertTokenizer

import librosa
import torch
from PIL import Image
from IPython.display import display, Audio

# -------------------------------
# 1. Text Sentiment Analysis
# -------------------------------
# Load text sentiment analysis pipeline

text_pipeline = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")

text = "The product looks amazing but broke after two days of use."
text_sentiment = text_pipeline(text)
print("Text Sentiment Analysis:", text_sentiment)


# -------------------------------
# 2. Audio Sentiment Analysis
# -------------------------------
# Load Wav2Vec2 processor and model

audio_processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
audio_model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")

# Load audio file
audio_path = "sample_audio.wav"  # Replace with your file path
audio, rate = librosa.load(audio_path, sr=16000)
input_values = audio_processor(audio, return_tensors="pt", sampling_rate=16000, padding=True).input_values

# Transcribe audio
with torch.no_grad():
    logits = audio_model(input_values).logits

predicted_ids = torch.argmax(logits, dim=-1)
transcription = audio_processor.batch_decode(predicted_ids)[0]

# Perform text sentiment analysis on transcribed audio
audio_sentiment = text_pipeline(transcription)
print("Audio Transcription:", transcription)
print("Audio Sentiment Analysis:", audio_sentiment)


# -------------------------------
# 3. Image Sentiment Analysis
# -------------------------------
# Load image processor and model
image_processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224")
image_model = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224")


# Load and preprocess image
image_path = "sample_image.jpg"  # Replace with your file path
image = Image.open(image_path)
display(image)  # Display the image in the notebook


inputs = image_processor(images=image, return_tensors="pt")

with torch.no_grad():
    logits = image_model(**inputs).logits

predicted_class_id = torch.argmax(logits, dim=-1).item()
image_class = image_model.config.id2label[predicted_class_id]

print("Image Sentiment Analysis:", image_class)


# -------------------------------
# Combine Results
# -------------------------------
multimodal_results = {
    "Text Sentiment": text_sentiment,
    "Audio Sentiment": audio_sentiment,
    "Image Sentiment": image_class,
}

print("\nMultimodal Sentiment Analysis Results:")
for modality, result in multimodal_results.items():
    print(f"{modality}: {result}")

Expected Output
Text Sentiment Analysis:{'label': '2 stars', 'score': 0.87}
Audio Transcription:"This product seemed great, but it broke within two days."
Audio Sentiment Analysis:{'label': '2 stars', 'score': 0.83}
Image Sentiment Analysis:Product Class: Fragile Items

Applications of Multimodal Sentiment Analysis
  1. Social Media Monitoring: Understand user sentiment from posts combining text, video, and images.
  2. Customer Feedback Analysis: Analyze reviews combining written feedback, recorded audio, and images.
  3. Healthcare: Evaluate patient sentiment during telemedicine consultations using text, voice, and facial cues.
  4. Entertainment: Analyze audience reactions to movies, music, or trailers through text comments, voice recordings, and visual responses.

Challenges of Multimodal Sentiment Analysis
  1. Data Alignment:
  2. Synchronizing information from multiple modalities (e.g., aligning audio transcription with facial expressions) can be computationally intensive.
  3. Complexity:
  4. Requires multiple models, increasing computational requirements and potential latency.
  5. Domain-Specific Fine-Tuning:
  6. Pre-trained models may not generalize well to specific domains without additional training on multimodal datasets.

Resources for Sentiment Analysis