A first try at sentiment analysis: TextBlob on a Twitter stream

One weekend in January 2017 I read about TextBlob and what it offers for natural language processing, and I wanted to learn more about it, its accuracy and its tuning. This was my first try: score the sentiment of tweets as they arrive on Twitter’s live stream.

Reading the stream

I used Tweepy to get the stream, and it was really easy. It needs the four values of a Twitter application: a consumer key and secret, and an access token and secret.

import tweepy
from textblob import TextBlob
import json

# Step 1 - Authenticate
consumer_key= '...'
consumer_secret= '...'

access_token='...'
access_token_secret='...'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

Scoring each tweet

A listener receives each message from the stream, hands its text to TextBlob and prints the result.

#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(tweepy.StreamListener):
    counter = 0
    def on_data(self, data):
        tweet = json.loads(data)
        analysis = TextBlob(tweet['text'])
        print(tweet['text'])
        print(analysis.sentiment)
        print('-------------------------------------------------------------')
        return True

    def on_error(self, status):
        print(status)

    def on_status(self, status):
        global counter
        counter = counter + 1
        if counter < 5:
            return True
        else:
            return False

The stream is then filtered by keyword and by language. I tracked the usual keyword of those days, in English only.

myStream = tweepy.Stream(auth = api.auth, listener = StdOutListener())
myStream.filter(track = ["..."], languages=["en"])

What came back

For each tweet TextBlob returns two numbers, a polarity and a subjectivity:

Sentiment(polarity=0.0, subjectivity=0.0)
Sentiment(polarity=-0.35714285714285715, subjectivity=0.47857142857142854)
Sentiment(polarity=0.25, subjectivity=0.2)

The run scored 54 tweets before it stopped on a KeyError: a message arrived from the stream with no text field. Of the 54, 28 came back with zero for both numbers. 16 had a positive polarity and 8 a negative one.

Searching in place of streaming

Tweepy can also search. One call returns tweets for a keyword, and the same lines score them.

public_tweets = api.search('...')

for tweet in public_tweets:
    print(tweet.text)
    analysis = TextBlob(tweet.text)
    print(analysis.sentiment)
    print('-------------------------------------------------------------')

This returned 15 tweets. Three came back with zero for both numbers, six had a positive polarity and four a negative one.

Adapted in October 2026 from a post first published on my blog and on LinkedIn in January 2017. The code uses Tweepy and Twitter’s API as they stood then.

I’m Amir Pournasserian. I build AI and platform systems for a living, maintain FluentCMS and YeSvelte, and write here about what I find along the way.