Opinion Mining (Sentiment Analysis) is a Natural Language Processing (NLP) technique used to identify, extract and analyze opinions, emotions, attitudes and sentiments expressed in textual data.
- Analyzes textual data to determine the sentiment expressed by users.
- Classifies opinions into positive, negative or neutral categories.
- Extracts emotions and attitudes from unstructured text.
- Processes data from sources such as customer reviews, social media, blogs, forums and surveys.
Example: Customer Review
"The camera quality is excellent, but the battery life is poor."
Opinion Mining Analysis:
- Opinion Object: Smartphone
- Aspect 1: Camera Quality → Positive
- Aspect 2: Battery Life → Negative
- Overall Sentiment: Mixed (Both Positive and Negative)
Components of Opinion Mining
Opinion Mining consists of several key components that work together to identify and analyze opinions expressed in textual data.
1. Opinion Holder (Source): The opinion holder is the person, group or organization that expresses an opinion.
Example:
"Rahul likes the new smartphone."
Opinion Holder: Rahul
2. Opinion Object (Entity): The opinion object is the product, service, person, event or topic about which the opinion is expressed.
Example:
"Rahul likes the new smartphone."
Opinion Object: Smartphone
3. Opinion Target (Aspect): The opinion target (or aspect) is the specific feature or attribute of the opinion object that is being evaluated.
Example:
"The camera quality is excellent."
Opinion Target: Camera Quality
4. Opinion Orientation (Sentiment): The opinion orientation represents the polarity of the opinion expressed. It is generally classified into Positive , Negative and Neutral
Example:
"The battery life is poor."
Opinion Orientation: Negative
5. Opinion Expression: The opinion expression is the word or phrase that conveys the sentiment toward the opinion target.
Example:
"The display is amazing."
Opinion Expression: Amazing
Opinion Mining Techniques
Each opinion mining technique offers a unique approach to extracting sentiment from textual data.
1. Sentiment Polarity Classification
Sentiment Polarity Classification is an opinion mining technique that determines the overall sentiment expressed in a piece of text. It classifies the sentiment into predefined categories such as Positive, Negative or Neutral and may also provide a confidence score indicating the model's certainty about its prediction.
- To implement Sentiment Polarity Classification, we first import the pipeline module from the Transformers library.
- Next, we initialize the sentiment analysis pipeline using the pretrained nlptown/bert-base-multilingual-uncased-sentiment model.
- The input text is then passed to the pipeline, which returns a label (1 to 5 stars) and a score representing the model's confidence.
- The star rating is mapped to descriptive sentiment labels such as Very Negative, Negative, Neutral, Positive and Very Positive using a custom dictionary.
from transformers import pipeline
sentiment_pipeline = pipeline('sentiment-analysis', model='nlptown/bert-base-multilingual-uncased-sentiment')
text = "The movie was absolutely fantastic! I loved every moment of it."
result = sentiment_pipeline(text)[0]
label_map = {
'1 star': 'Very Negative',
'2 stars': 'Negative',
'3 stars': 'Neutral',
'4 stars': 'Positive',
'5 stars': 'Very Positive'
}
custom_label = label_map[result['label']]
confidence_percentage = result['score'] * 100
print(f"Sentiment: {custom_label}")
print(f"Confidence: {confidence_percentage:.2f}%")
Output:
Sentiment: Very Positive
Confidence: 95.22%
2. Emotion Detection
Emotion Detection is an opinion mining technique that identifies the specific emotion expressed in a piece of text rather than simply determining whether it is positive or negative. It classifies text into emotions such as Joy, Anger, Sadness, Fear, Surprise, Disgust and Neutral, providing a deeper understanding of user feelings and intentions.
- To implement emotion detection, we first import the
pipelinemodule from the Transformers library. - Next, we initialize the text classification pipeline using the pretrained j-hartmann/emotion-english-distilroberta-base model.
- A sample text is then passed to the pipeline for analysis.
- The model returns the predicted emotion label (such as Joy, Anger, Sadness, Fear, Surprise, Disgust or Neutral) along with a confidence score indicating how certain the model is about its prediction.
from transformers import pipeline
emotion_pipeline = pipeline('text-classification', model='j-hartmann/emotion-english-distilroberta-base')
text = "I am so excited about the new project!"
result = emotion_pipeline(text)[0]
custom_label = result['label'].capitalize()
confidence_percentage = result['score'] * 100
print(f"Emotion: {custom_label}")
print(f"Confidence: {confidence_percentage:.2f}%")
Output:
Emotion: Joy
Confidence: 97.58%
3. Aspect-based sentiment analysis
Aspect-Based Sentiment Analysis (ABSA) identifies specific aspects or features of an entity and determines the sentiment expressed toward each aspect individually.
- To implement ABSA, we first import the required libraries and initialize the Sentiment Intensity Analyzer (VADER) from NLTK.
- A sample review is then provided as input.
- The review is divided into individual clauses using commas so that each clause represents a specific aspect.
- Each clause is analyzed using VADER to calculate its sentiment score.
- The corresponding aspect (such as food or service) is identified from the clause and the sentiment is classified as Positive, Negative or Neutral based on the compound score.
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
nltk.download("vader_lexicon")
text = "The food was delicious, but the service was slow."
sia = SentimentIntensityAnalyzer()
clauses = [clause.strip() for clause in text.split(",")]
results = []
for clause in clauses:
score = sia.polarity_scores(clause)["compound"]
if score > 0.05:
sentiment = "Positive"
elif score < -0.05:
sentiment = "Negative"
else:
sentiment = "Neutral"
if "food" in clause.lower():
aspect = "food"
elif "service" in clause.lower():
aspect = "service"
else:
continue
results.append({
"aspect": aspect,
"sentiment": sentiment
})
print(results)
Output:
[{'aspect': 'food', 'sentiment': 'negative'}, {'aspect': 'service', 'sentiment': 'negative'}]
4. Multilingual Sentiment Analysis
Multilingual Sentiment Analysis analyzes the sentiment expressed in text written in different languages. It enables a single model to process multilingual data without requiring manual translation, making it suitable for applications that receive feedback from users across various regions and languages.
- To implement Multilingual Sentiment Analysis, we first import the
pipelinemodule from the Transformers library. - Next, we initialize the sentiment analysis pipeline using the pretrained
nlptown/bert-base-multilingual-uncased-sentimentmodel, which supports multiple languages. - A list of texts written in different languages is then provided as input to the pipeline.
- The model analyzes each text and returns a sentiment label (1 to 5 stars) along with a confidence score for the prediction
from transformers import pipeline
multilingual_sentiment_pipeline = pipeline('sentiment-analysis', model='nlptown/bert-base-multilingual-uncased-sentiment')
texts = [
"सेवा उत्कृष्ट थी।", # Hindi: "The service was excellent."
"సినిమా చాలా నిరాశపరిచింది." # Telugu: "The movie was very disappointing."
]
results = [multilingual_sentiment_pipeline(text) for text in texts]
for text, result in zip(texts, results):
print(f"Text: {text}\nSentiment: {result}\n")
Output:
Text: सेवा उत्कृष्ट थी।
Sentiment: [{'label': '5 stars', 'score': 0.4839303195476532}]
Text: సినిమా చాలా నిరాశపరిచింది.
Sentiment: [{'label': '3 stars', 'score': 0.3825204372406006}]
You can download the complete source code from here.
Applications of Opinion Mining
- Customer Feedback Analysis: Uses sentiment polarity and aspect-based sentiment analysis to identify customer satisfaction and feature-specific opinions from reviews.
- Social Media Monitoring: Applies sentiment polarity and emotion detection to analyze public reactions toward brands, products or events in real time.
- Brand Reputation Management: Detects positive and negative sentiments from online discussions to monitor and maintain brand reputation.
- Product and Service Improvement: Uses aspect-based sentiment analysis to identify strengths and weaknesses of specific product or service features.
- Market Research: Analyzes customer opinions and multilingual feedback to understand market trends, consumer preferences and competitor perception.
- Recommendation Systems: Incorporates sentiment extracted from user reviews to recommend products or services that better match user preferences.
Advantages of Opinion Mining
- Processes large volumes of textual data efficiently.
- Reduces the time and effort required for manual sentiment analysis.
- Identifies opinions, sentiments and emotions from unstructured text.
- Helps organizations understand customer preferences and expectations.
- Enables continuous monitoring of public opinion across multiple platforms.
Limitations of Opinion Mining
- Difficulty in detecting sarcasm, irony and humor.
- Context-dependent words may lead to incorrect sentiment classification.
- Performance depends on the quality and quantity of training data.
- Multilingual and code-mixed text can reduce prediction accuracy.