BeaS Labs - Exploring IBM Watson™ Technology, Twitter Personality Insights Demo

By Matthias Geisler May 18, 2015
BeaS Labs - Exploring IBM Watson™ Technology, Twitter Personality Insights Demo

In the first installment of this blog post series (A Glimpse at the Dawn of Intelligence from the Cloud) we’ve leveled the ground to now dive in more depth into the emerging IBM Watson™ cloud technologies and services offered. We’ll do so, by building a little sample web application that will leverage the Watson™’s Personality Insights service. A quick GitHub search reveals an every increasing amount of project repositories that either make use of or provide library/ API abstractions for the IBM Watson™ services. With BeaS’ focus on all things social, it’s no wonder that “social coding” is at the very heart of BeaS Labs. That said, for this demo we’ll “fork” (What? —> GitHub Guide- Forking Projects and enhance the [Personality Insights Nodejs Starter Application][IBM Personality Insights Nodejs Starter Application] provided by the Watson™ Developer Cloud. While IBM’s starter Application does provide a text input field to feed arbitrary text content to IBM Watson™ Personality Insights service, we’ll create a Mashup (still remember this Web 2.0 ohish terminology? Does already sound old, no?) that will add the ability to have the results of a Twitter API query for a User’s Timeline or a Twitter Search run through Watson™’s linguistic analytics and to extract a spectrum of cognitive and social characteristics. The resulting user interface will look as follows: The source code of this demo along with a documentation on how to run this on your local computer or with a one-click deployment on IBM Bluemix can be found on BeaS’ GitHub account. A running version of the demo application can be found deployed to IBM Bluemix, here: ☛ beas-labs-watson-pi-demo.eu-gb.mybluemix.net Well then, let’s dive into the code… Please note:

  • While some familiarity w/ Node.js and Express (a Node.js web application framework) is not required it surely helps to more easily follow along the code examples.
  • Also code fragments are truncated for the sake of readability.
  • That said, especially if interested in setting up the demo one should refer to the README.
First and foremost one needs to create a Personality Insights service in IBM Bluemix and a Twitter App. For local development one can store the respective API credentials into a .env file in the project root directory:
BLUEMIX_PERSONALITY_INSIGHTS_URL=
BLUEMIX_PERSONALITY_INSIGHTS_USERNAME=
BLUEMIX_PERSONALITY_INSIGHTS_PASSWORD=
TWITTER_CONSUMER_KEY=
TWITTER_CONSUMER_SECRET=
TWITTER_ACCESS_TOKEN=
TWITTER_ACCESS_TOKEN_SECRET=
The application’s main entry point is the file app.js. In here, we’ll first create and initialize a Watson™ Personality Insights service wrapper (—> Watson™ Developer Cloud Node.js Client NPM Module watson-developer-cloud)…
var watson = require(‘watson-developer-cloud’);
// […]
var personalityInsights = watson.personality_insights({
 //[…]
});
… as well as an Twitter API client:
var Twitter = new twitter({
 //[…]
});
It’s code is living in lib/ twitter.js and is basically just a wrapper (—> Twitter API client library for node.js NPM Module twitter) exposing a convenience method that we’ll use to trigger both - a request to retrieve entries from a given Twitter User’s timeline (getUserTimeline()) as well as to search Twitter for a given query (search()):
Twitter.prototype.query = function(query, callback){
 //[…]
}
The differentiation between the two will be done by examining the input provided by the user: a query prefix with @ will trigger getUserTimeline() while any other query will trigger search() (including a # hashtag search). Secondly, we’ll add a new POST route /twitter/ that the client will use as endpoint to have the user’s Iwitter input processed:
app.post(‘/twitter/‘, function(req, res) {
 //[…]
});
In here, the original personalityInsights.profile() method, exposed by the Watson service wrapper, gets triggered from the callback of the (Twitter.query()) method - if the Twitter API call succeeded.
Twitter.query(query, function (tweets, errors) {
 if(!errors) {
 personalityInsights.profile({text: tweets}, function(err, profile) {
 //[…]
 }
 }
 //[…]
});
On the client-side of things we’ll add togglable Bootstrap tabs, labeled Text Input and Twitter User Timeline or Search. The first will hold the contents of the default text input capabilities, the later an additional input field for a Twitter query, along w/ some nicely formatted usage instructions. The glue code between the client and the server lives in the file public/js/demo.js and is essentially a jQuery XHR (a.k.a AJAX) request triggered by a click on the button Analyze:
$(‘.analysis-btn’).click(function(){
 //[…]
});
Ultimately, the Watson™ Personality Insights response will result in:
  1. a human readable summary of the personality (—> client-side JavaScript code living in public/js/textsummary.js)
  2. traits (facets, needs, values) for the data behind the personality analysis (—> client-side JavaScript code living in public/js/textsummary.js, w/ templating logic in public/js/demo.js)
  3. a D3.js-backed visualization of the personality data (—> client-side JavaScript code living in public/js/textsummary.js)
… or, again, give it a try: ☛ beas-labs-watson-pi-demo.eu-gb.mybluemix.net Take aways:
  • Linguistic Analytics Mashups FTW!
  • The Open Source and Social Coding embracing environment that IBM is building around the Watson™ services not only eases but rather truly fosters the community adoption of and tinkering with Watson™ technologies and services.
Speaking of which: ever wondered which celebrities have the most similar (and different) personalities to yours? It truly was a surprise for me ;-) Go, provide Watson™ with your Twitter handle to see at Your Celebrity Watch… With this I’ll leave you for now… #KeepCalmAndCode