Watson / Alchemy / Twitter analysis – US presidential election

What’s about using some cool new technologies, to see how the US presidential election is going on?
Well as a non-american citizen, that is exactly what I’ve asked myself.
Let’s see, how we can implement such a system relatively easily in a short time.

Last year IBM has acquired AlchemyAPI (www.alchemyapi.com) which includes a bunch of very nice and powerful text analysis functions.
In this example I’m using the AlchemyAPI to analyze the sentiment of a couple of Twitter Tweets.


As a basic IT infrastructure, I’ve decided to create a simple C# backend to collect Tweets, to analyze the sentiment and to store the information into a private MSSQL Database.
A client software is then able to visualize the output of the analyzed Tweets in realtime.

Therefore we have two external APIs we can use in that scenario:

  1. AlchemyAPI – Sentiment Analysis
    • Analyze Tweets
  2. Twitter Streaming API
    • Collect Tweets

The first thing we have to do is, to collect a lot of relevant Tweets from the Twitter Streaming API and to store the Tweets temporarily in our private Database.
For this purpose I’m using the C# tweetinvi library (Tweetinvi on GitHub)

var stream = Stream.CreateFilteredStream();
stream.Credentials = Auth.SetUserCredentials(TWITTER_DEVELOPER_INFORMATION_GOES_HERE);


stream.MatchingTweetReceived += (sender, args) =>
//Handle tweet data here


Now we can use Alchemy in order to analyze the sentiment of each single Tweet. There already exists a basic, but absolutely sufficient Alchemy .NET SDK, which can be used in your code.
Just a few lines are needed, to do the analysis:

AlchemyAPI.AlchemyAPI alchemyObj = new AlchemyAPI.AlchemyAPI();
string xmlResult= alchemyObj.TextGetTextSentiment(tweet.TweetText);

You can decide which output format the result should have.
Usually the XML format is predefined. If you’d like to go for a JSON format, I’ve to tell you, that this is currently not working, because of some internal issues.
If you really want to use JSON, than you have to use the WebApi of Alchemy and implement your own C# Alchemy client.

Well perfect.
Basically this is all what we’ve to do so far. Ok, to be honest, you’ve also to write some code to handle your database connection as well as to develop a good looking client interface which’ll visualize all the data.

In my case, I’ve choosen to visualize two different aspects of the current US presidential election:

  1. Popularity of the candidates on Twitter
  2. Tweet Volume about the candidates

In this scenario, I’ve analyzed more than 50,000 Tweets (all Tweets were collected within 2 hours of the same day).
The results really suprise me. But take a look for yourself.

How to interpretate the results, is up to you ;-).


Post author

I'm an executive M.Sc. with over 12 years experience in cloud architecture / software development and software engineering.

Leave a Reply