Today, I'll walk you through what sentiment analysis is, why it matters, and how to implement it practically using Python.
Sentiment analysis, also known as opinion mining, is the computational study of opinions, sentiments, and emotions expressed in text. At its core, it's about determining whether a piece of text expresses positive, negative, or neutral sentiment.
I've implemented sentiment analysis across various domains:
The applications are virtually limitless, which is why it's become such a cornerstone technique in modern data science.
Before diving into implementation, let's understand the main approaches:
These rely on predefined dictionaries of words with associated sentiment scores. Simple but effective for many use cases.
These use trained models to classify sentiment based on patterns learned from labelled data. More sophisticated but require training data.
Combining rule-based and machine learning methods often yields the best results, particularly when dealing with domain-specific language.
Let's start with a practical example using Python. I'll demonstrate both rule-based and machine learning approaches.
First, install the required libraries:
pip install textblob vaderSentiment transformers torchVADER (Valence Aware Dictionary and sEntiment Reasoner) is excellent for social media text and informal language:
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import pandas as pd
# Initialize the analyser
analyzer = SentimentIntensityAnalyzer()
# Sample texts
texts = [
"I absolutely love this product! It's fantastic.",
"This is terrible. Worst purchase ever.",
"It's okay, nothing special but works fine."
]
# Analyse sentiment
results = []
for text in texts:
scores = analyzer.polarity_scores(text)
results.append({
'text': text,
'positive': scores['pos'],
'negative': scores['neg'],
'neutral': scores['neu'],
'compound': scores['compound']
})
# Convert to DataFrame for easy viewing
df = pd.DataFrame(results)
print(df)TextBlob provides a simple interface for sentiment analysis:
from textblob import TextBlob
def analyse_sentiment_textblob(text):
blob = TextBlob(text)
# Get polarity (-1 to 1) and subjectivity (0 to 1)
polarity = blob.sentiment.polarity
subjectivity = blob.sentiment.subjectivity
# Classify sentiment
if polarity > 0.1:
sentiment = 'Positive'
elif polarity < -0.1:
sentiment = 'Negative'
else:
sentiment = 'Neutral'
return {
'sentiment': sentiment,
'polarity': polarity,
'subjectivity': subjectivity
}
# Test the function
sample_text = "The customer service was helpful, but the delivery was delayed."
result = analyse_sentiment_textblob(sample_text)
print(f"Text: {sample_text}")
print(f"Sentiment: {result['sentiment']}")
print(f"Polarity: {result['polarity']:.2f}")
print(f"Subjectivity: {result['subjectivity']:.2f}")For more sophisticated analysis, particularly with domain-specific text, I recommend using pre-trained transformer models:
from transformers import pipeline
# Load pre-trained sentiment analysis pipeline
sentiment_pipeline = pipeline("sentiment-analysis")
# Analyse multiple texts
texts = [
"The new software update has significantly improved performance.",
"I'm frustrated with the constant bugs in this application.",
"The interface is clean and user-friendly."
]
# Get predictions
predictions = sentiment_pipeline(texts)
for text, pred in zip(texts, predictions):
print(f"Text: {text}")
print(f"Sentiment: {pred['label']} (Confidence: {pred['score']:.2f})")
print("---")After implementing sentiment analysis across 19 different SaaS platforms, here are my key recommendations:
Sentiment analysis is just the beginning. In practice, I've found the most value comes from combining it with other techniques like topic modelling, named entity recognition, and temporal analysis to understand not just how people feel, but what they feel strongly about and when.
Start with these examples, experiment with your own data, and gradually build more sophisticated pipelines. The key is to begin simple, validate your results, and iterate based on your specific use case requirements.
Get notified when I publish new articles.