Building a Sentiment Ticker with Raspberry Pi and NLTK
I recently got my hands on a GrovePi+ Starter Kit and the latest Raspberry Pi. This toolkit is specifically designed to do IoT computing with a Raspberry Pi. It gave me some inspiration to do something a little bit more outside-the-box than the kinds of projects they provide in the starter kit booklet – a little bit more art than science.
Visualizing Color and Text with Grove’s LCD
One of the components you get is an RGB LCD. Once you’ve plugged it into the Grove and have everything connected to the Pi, you can hop into a terminal on your Pi and use the libraries that the Grove comes with to control the screen.
The libraries are impressively easy. Sending text to the screen is as easy as:
import grove_rgb_lcd as screen
screen.setRGB(255, 0, 0) # all red, for instance
screen.setText("I'm going to show up on the screen!")
So what are some of the things you can do with a screen like this? Grove has some examples related to its other sensors, such as temperature and humidity, which is cool. I’m definitely interested in integrating my Sonos with the screen as a “now playing” display. But I wanted to find a more interesting way to play with the three dimensions of color.
Finding and Visualizing Sentiment
This is where sentiment analysis comes in. There are a handful of different ways to analyze sentiment. For instance, Stanford CoreNLP’s deep learning model charts sentiment on a one to five score. Last time I checked, it was among the best in class. But there’s a different kind of sentiment analysis that evaluates text along three different axes of intensity: positive language, neutral language, and negative language. Each value will range from zero to one. This is great, because an RGB screen has three different values we can play with, a minimum of zero to a maximum of 255. I used CJ Hutto’s Vader Sentiment Analysis Project, a reasonably recent and easy-to-use project geared towards social media text. Another perk of this project is that it has been incorporated into NLTK, which tends to be my go-to NLP toolkit for hobbyist stuff like this.
Using NLTK, I can get the sentiment data for a bit of text like this:
# assuming you already have the vader data downloaded
# otherwise, you will need to call nltk.download('vader')
from nltk.sentiment.vader import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()
analyzer.polarity_scores("This is the text I want to analyze")
# will return a dict with intensity of 0.0 to 1.0 for:
# 'neg' (negative)
# 'pos' (positive)
# 'neu' (neutral)
Twitter Streaming
Now I need some kind of textual source to analyze, of course. The Python Twitter Client is perfect for this kind of a project. In particular, I found it very easy to work though the issues related to OAuth2 when attempting to initiate an authenticated session.
Assuming you have handled authentication correctly, you
can use a TwitterStream
instance to sample random
tweets or filter against a specific topic. For my
project, I’m filtering for the term “Atlanta”, the
city I just moved to. Assuming you have an OAuth
object correctly configured, that’s as easy as:
for tweet in TwitterStream(auth=oauth_object).statuses.filter(track="atlanta"):
print tweet['text'] # or anything else you want to do with a tweet!
Bringing It All Together
I’ve posted some code called the Grove Sentiment Scanner that shows exactly how we combine these parts all together. We search for the term “Atlanta” in the Twitter stream, and for each tweet, we can parse the sentiment. We take the zero to one intensity values for each axis, and transpose them to zero to 255 values corresponding to red (negative), green (positive), or blue (neutral).
Artist’s Statement
I’m hesitant to say there’s a ton of intrinsic utility in a project like this. But it’s a lot of fun, and I think there’s a bit of art involved, here.
Generating Empathy
There is an internally consistent sense of synesthesia here between what we see and what a bit of text is intended to make us feel. An approach like this has the capacity to encourage empathy in unexpected ways, as we adapt the visual components of our mind to reason over emotional and verbally symbolic components at the same time. We can use the colors that we see to understand the underlying emotion of the text before we finish processing a sentence. This could serve as a tool of disambiguation emotional content in a text for individuals who have higher than average difficulty divining emotion from text.
Color and Meaning
We are grounded in some very basic sense of semiotics by mapping positivity to green and negativity to red. The stop light most immediately comes to mind as an artifact we use every day that takes advantage of this opposition of color and emotional intent. Blue, as being part of how RGB generates the range of colors it does, is in many regards incidental. But we can look at how blue is used and understand that it does often communicate neutrality quite effective. This is the reason, for instance, why blue is so often used as the color scheme for retail locations.