Using Machine Learning APIs to get Average Twitter Sentiment in Java

Some call it ‘The Orb of Destiny’, others ‘The Orb of Happiness’, or as I prefer to call it: ‘The Orb of Doom’.

Martin Kearn, Amy Nicholson and Andrew Spooner put together this amazing sentiment application last year for the Reasons To event. The Orb uses the Text Analytics API to get the average sentiment around a specific hashtag, and the Orb’s colour magically changes based on the average sentiment. It works through using the Twitter Streaming API, Text Analytics API, Event Hubs and Stream Analytics and it’s pretty cool how it all works!

Amy has written a blog post that perfectly explains how all of those technologies were put together to create the great and mighty Orb of Doo… Happiness! Check it out here

So taking this project a step further, and in preparation for the Devoxx conference, I rebuilt the project adding in Android, Java and Spencer the Sphero.

Here’s the original architecture of the Orb Project:

Orb1

 

And here’s what I (roughly) did to it:

Orb2

 

How does it work?

Java Client 1:

  • Uses the Twitter Streaming API to listen for the latest tweet at a given hashtag
  • Calls the Text Analytics API using the text of the latest Tweet, which returns the sentiment score
  • Put the Tweet and Score into JSON format and send to an event hub (let’s call this Event Hub ‘Input’)

Stream Analytics:

  • Get the latest messages from the event hub
  • Over a 30 second sliding window, calculate the average sentiment score, then output the average score to an event hub (let’s call this Event Hub ‘Output’)

Java Client 2:

  • Listens for all messages going into the ‘Output’ event hub then does a HTTP POST request to Martin’s API, containing the latest Average Sentiment Score

Android Application:

  • Every 10 seconds, it does a HTTP GET request to Martin’s API to get the latest sentiment score
  • Updates the colour of the Orb animation based on the score: 0-35 RED, 36-65 YELLOW, 66-100 GREEN
  • Also, using Bluetooth, if the Sphero is available and paired – it will change the colour of the Sphero to match the animation being displayed on the Android application

FAQs

Why do we have an API in between:

We can use it everywhere: in the android app, iOS app, web app, windows app – you can get the calculated average score from anywhere.

Backend:

The backend of the app was previously developed in C#, and the clients were deployed as web jobs in Azure. I wanted to do everything in Java. As for deploying Java clients to Azure, I will post about this soon!

Why not do all the logic in Android:

I did this too. I cut out Azure completely and did everything in my Android application. I used Twitter to get the latest 5 tweets at the given hashtag (every 10 seconds), then called the Text Analytics API for each tweet, then calculated the average and changed the colour of the Orb, all in Java, all in Android: It is more than possible.

However, it wasn’t practical. Getting the latest 5 tweets every 10 seconds was not a very accurate representation of the average sentiment.

It wasn’t scalable. If I got thousands of tweets… Oh dear..

Running a Java client on Azure and using Event hubs (which can handle millions of messages) made the app more efficient and easier to handle.

Next steps: Using the Emotions API

Now that the emotions API has been released, we can go a step further can change the Orb to use it! It’s as simple as changing the input from tweets (string input) to faces (image input). We can build an app or website that users can use to take a photo of themselves, pass that image to the Emotions API to get a happiness score, then use the same cloud structure to get the average happiness score over a period of time. This will then drive the colour of the Orb.

Wanna check out the code and try this yourself?

Here’s the GitHub repo, go wild: https://github.com/liliankasem/JavaTwitterSentimentApp

Resources and useful blog posts:

Java based client tutorial for Event Hubs: https://azure.microsoft.com/en-gb/documentation/articles/event-hubs-java-storm-getstarted/

Text Analytics link: https://azure.microsoft.com/en-gb/marketplace/partners/amla/text-analytics/

Project Oxford link: https://www.projectoxford.ai/

Amy’s original blog post: http://blogs.technet.com/b/amykatenicho/archive/2015/09/07/the-current-level-of-reasonsto-happiness.aspx

I plan on writing up how I did each aspect of this application in separate blog posts, so keep a look out if interested!