A sentiment analyser that scores text as positive, neutral, or negative — tested on real product review data.
TextBlob returns polarity (−1 to +1) and subjectivity (0 to 1).
from textblob import TextBlob
texts = [
"I absolutely love this product — it changed my life!",
"The delivery was OK but the packaging was damaged.",
"Terrible quality. Would not recommend to anyone.",
]
for text in texts:
blob = TextBlob(text)
pol = blob.sentiment.polarity
label = "Positive" if pol > 0.1 else ("Negative" if pol < -0.1 else "Neutral")
print(f"{label:8s} ({pol:+.2f}) {text[:50]}")
Positive (+0.65) I absolutely love this product — it changed my life! Neutral (+0.08) The delivery was OK but the packaging was damaged. Negative (-0.70) Terrible quality. Would not recommend to anyone.
Load a list of reviews and count positive/neutral/negative.
reviews = [
"Fantastic! Exceeded all expectations.",
"It's fine, nothing special.",
"Broke after one week. Very disappointed.",
"Best purchase I've made this year.",
"Average product, average price.",
"Absolutely awful. Returning immediately.",
]
counts = {"Positive": 0, "Neutral": 0, "Negative": 0}
for r in reviews:
pol = TextBlob(r).sentiment.polarity
key = "Positive" if pol > 0.1 else ("Negative" if pol < -0.1 else "Neutral")
counts[key] += 1
print(counts)
{'Positive': 2, 'Neutral': 2, 'Negative': 2}A simple pie chart of sentiment distribution.
import matplotlib.pyplot as plt
plt.pie(counts.values(), labels=counts.keys(), autopct='%1.0f%%',
colors=['#22c55e', '#94a3b8', '#ef4444'], startangle=90)
plt.title('Review Sentiment Distribution')
plt.savefig('sentiment.png', dpi=150)
print("Saved sentiment.png")
You now have a working sentiment pipeline. Apply it to real product reviews, tweets, or customer feedback to surface insights automatically.
Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.