How to Calculate Sentiment Polarity Scores with TextBlob in Python

This quick tutorial shows how to calculate sentiment polarity scores with TextBlob. For a complete introduction to text analysis in Python, see our Getting Started with TextBlob in Python guide.

The Problem

Reading through customer reviews or survey responses one at a time to gauge tone doesn’t scale. You need a number that captures how positive or negative a piece of text is, so you can sort entries, compare results, or flag low-scoring items for review. TextBlob returns that number with a single attribute.

The Solution

TextBlob expresses polarity as a float from -1.0 (most negative) to 1.0 (most positive), with 0 representing neutral tone. Access it through .sentiment.polarity on any TextBlob object.

The following code scores three texts with different tones:

 
from textblob import TextBlob

texts = [
    "I absolutely love this product. It works perfectly.",
    "The experience was okay, nothing special.",
    "Worst purchase I've ever made. Total waste of money."
]

for text in texts:
    score = TextBlob(text).sentiment.polarity
    print(f"Score: {score:.2f} | {text}")

Output:

 
Score: 0.75 | I absolutely love this product. It works perfectly.
Score: 0.43 | The experience was okay, nothing special.
Score: -0.40 | Worst purchase I've ever made. Total waste of money.

Raw scores are useful, but labeled categories are often easier to act on. Add a short threshold function to convert scores into readable labels:

 
def label_polarity(score):
    if score > 0.1:
        return "positive"
    elif score < -0.1:
        return "negative"
    else:
        return "neutral"

for text in texts:
    score = TextBlob(text).sentiment.polarity
    print(f"{label_polarity(score):8} ({score:.2f}) | {text}")

Output:

 
positive (0.75) | I absolutely love this product. It works perfectly.
positive (0.43) | The experience was okay, nothing special.
negative (-0.40) | Worst purchase I've ever made. Total waste of money.

The ±0.1 band around zero captures mildly worded text that isn’t clearly positive or negative. Adjust those thresholds based on your data and how precisely you need to separate the categories.

A Little Something Extra

TextBlob calculates polarity by scoring each word against a fixed lexicon and averaging the results. Short texts with one strong word, like “terrible” or “exceptional,” tend to produce more extreme scores. Longer passages with mixed positive and negative language tend to land closer to zero because the words offset each other. Before settling on thresholds, check the score distribution across your full dataset. That view will show you where the natural breakpoints are and help you avoid mislabeling borderline entries.

Conclusion

Polarity scoring gives you a fast numeric read on text sentiment. Combine it with a simple threshold function to convert scores into labeled categories for sorting, filtering, or further analysis.

Leave a Reply

Your email address will not be published. Required fields are marked *