DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Boosting Application Performance With MicroStream and Redis Integration
  • Five Java Books Beginners and Professionals Should Read
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices
  • Clear Details on Java Collection ‘Clear()’ API

Trending

  • Boosting Application Performance With MicroStream and Redis Integration
  • Five Java Books Beginners and Professionals Should Read
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices
  • Clear Details on Java Collection ‘Clear()’ API
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Working with the Bluemix Personality Insights Service

Working with the Bluemix Personality Insights Service

Raymond Camden user avatar by
Raymond Camden
·
Apr. 02, 15 · Interview
Like (0)
Save
Tweet
Share
3.16K Views

Join the DZone community and get the full member experience.

Join For Free

as i continue to play around with ibm bluemix , this week i spent some time playing with the personality insights service. this service uses ibm watson to analyze textual input and try to determine personality aspects of the author. it focuses on three areas of analysis:

  • determining what the needs of the author are. these needs are narrowed to twelve main areas and rated on a 0-100 percentile scale. the needs are: excitement, harmony, curiosity, ideal, closeness, self-expression, liberty, love, practicality, stability, challenge, and structure.
  • determining what the values of the author are. these are things watson believe will be important to the author. as with needs, values are focused on a set of core items: self-transcendence / helping others, conservation / tradition, hedonism / taking pleasure in life, self-enhancement / achieving success, and open to change / excitement.
  • finally, the pi service reports on the “big five” – this is a personality model that tries to describe how a person interacts with the world.

as you can imagine, this is pretty deep stuff, and frankly, my wife who is working on her sociology degree would probably have a better understanding of the results. you can check out the full docs as well as look at the api docs for more information.

for my demo, i decided to try something interesting. the pi service works best when it has at least 3500 words. a typical blog post may include five hundred or so words, and with a typical rss feed being ten items, i decided to build an application that would analyze an rss feed and try to determine the personality of the author. i called it the blog personality scan . i’ll link to the demo in a moment, but let’s look at the code first.

first, we’ll look at the app.js file for the node app. it is pretty trivial as there are only two views – the home page and the api to send an rss url.

/*jshint node:false */
/* global console,require */
var express = require('express');
var hbs = require('hbs');
var url = require('url');

hbs.registerhelper('raw-helper', function(options) {
  return options.fn();
});

var rssreader = require('./rssreader.js');
var insightsapi = require('./insights.js');

// setup middleware
var app = express();
app.use(app.router);
app.use(express.errorhandler());
app.use(express.static(__dirname + '/public')); //setup static public directory
app.set('view engine', 'html');
app.engine('html', hbs.__express);
app.set('views', __dirname + '/views'); //optional since express defaults to cwd/views

// render index page
app.get('/', function(req, res){
	res.render('index');
});

app.get('/parse', function(req, res) {
	var url_parts = url.parse(req.url, true);
	var query = url_parts.query;
	if(!query.rss) {
		res.json({error:"invalid data sent."});
		return;
	}
	rssreader.parse(query.rss, function(err,content) {
		if(err) {
			res.json(err);
		} else {
			console.log('bak with content, len is '+content.length);
			insightsapi.parse(query.rss, query.rss, content, function(data) {
				console.log('back from iapi');
				//console.log(json.stringify(data));
				res.json(data);	
			});
		}
	});
});

// there are many useful environment variables available in process.env.
// vcap_application contains useful information about a deployed application.
var appinfo = json.parse(process.env.vcap_application || "{}");
// todo: get application information and use it in your app.

// vcap_services contains all the credentials of services bound to
// this application. for details of its content, please refer to
// the document or sample of each service.
if(process.env.vcap_services) {
	var services = json.parse(process.env.vcap_services || "{}");
	console.log(services);
	var apiurl = services.personality_insights[0].credentials.url;
	var apiusername = services.personality_insights[0].credentials.username;
	var apipassword = services.personality_insights[0].credentials.password;
} else {
	var credentials = require('./credentials.json');
	var apiurl = credentials.apiurl;
	var apiusername = credentials.apiusername;
	var apipassword = credentials.apipassword;
}
insightsapi.setauth(apiurl, apiusername, apipassword);
					
// the ip address of the cloud foundry dea (droplet execution agent) that hosts this application:
var host = (process.env.vcap_app_host || 'localhost');
// the port on the dea for communication with the application:
var port = (process.env.vcap_app_port || 3000);
// start server
app.listen(port, host);
console.log('app started on port ' + port);

not terribly exciting and i wouldn’t share it normally, but i specifically wanted to call out the bits that look at process.env.vcap_services . this is how my app picks up the api credentials when running in the bluemix environment.

rss reading is taken care of by the feedparser npm package. this is the same one i used for coldfusionbloggers.org . i’ll skip that code as it isn’t that exciting.

the real fun part comes in the code used to interact with the pi service:

var https = require('https');
var querystring = require('querystring');
var url = require('url');

var apiusername;
var apipassword;
var apiurl;
var apihost;
var apipath;

function setauth(apiurl, u, p) {
	apiurl = apiurl;
	apiusername=u;
	apipassword=p;
	var parts = url.parse(apiurl);
	apihost = parts.host;
	apipath = parts.pathname;
}

function sendinsights(user,source,input,cb) {
	//cb(fake);return;
	var data = {"contentitems":[]};
	var item = {};
	item.userid = user;
	item.sourceid = source;
	this.id = this.userid + '_'+this.sourceid;
	item.contenttype = "text/plain";
	//todo - remove html from input. the service does it, but we can do it ourselves
	item.language = "en";
	item.content = input;
	
	data.contentitems.push(item);
	
	var postdata = json.stringify(data);
	
	
	var options = {
		host: apihost,
		port: 443, 
		path: apipath + "/v2/profile",
		headers: {
			'authorization': 'basic ' + new buffer(apiusername + ':' + apipassword).tostring('base64'),
			'content-type':'application/json',
			'content-length': buffer.bytelength(postdata)
		},
		method:"post"
	};
	console.log(options);
	var req = https.request(options, function(resp) {
		var body = "";
		resp.on("data", function(chunk) {
			body += chunk;
		});
		
		resp.on("end", function() {
			//console.log("done");console.log(body);
			cb(json.parse(body));
		});
		
	});
	req.write(postdata);
	req.end();
	
};

var insightapi = {
	setauth:setauth,
	parse:sendinsights
};

module.exports = insightapi;

ok, perhaps “exciting” is a bit much. honestly, it is just a http hit and a json response. simple – but that’s kind of the point. a good service should be rather simple to use.

the rest was just presenting the results. the folks at bluemix created a cool demo with charts and stuff, but i decided to keep it simple and just render the values – sorted. i used handlebars to make it a bit nicer, which ended up being a bit confusing to me. it never occurred to me to consider what would happen when i used a handlebars template for the client side in a view that was being run by a node.js app using handlebars on the client as well. as you can guess, it didn’t work well at first. if you look back at that first code listing you’ll see a helper called raw-helper. i needed to add this so i could use handlebar’s syntax in my view and have the server ignore it. this is how it looks in index.html:

<script id="reporttemplate" type="text/x-handlebars-template">
	{{{{raw-helper}}}}
	<div class="row">
		<div class="col-md-4">
			<h2>values</h2>
			{{#each values}}
			<div class="row">
				<div class="col-md-6"><strong>{{name}}</strong></div>
				<div class="col-md-6">{{perc percentage}}</div>
			</div>
			{{/each}}			
		</div>
		<div class="col-md-4">
			<h2>needs</h2>
			{{#each needs}}
			<div class="row">
				<div class="col-md-6"><strong>{{name}}</strong></div>
				<div class="col-md-6">{{perc percentage}}</div>
			</div>
			{{/each}}
		</div>
		<div class="col-md-4">
			<h2>the big 5</h2>
			{{#each big5}}
			<div class="row">
				<div class="col-md-6"><strong>{{name}}</strong></div>
				<div class="col-md-6">{{perc percentage}}</div>
			</div>
				{{#each children}}
					<div class="row">
						<div class="col-md-offset12 col-md-5 text-muted">{{name}}</div>
						<div class="col-md-6 text-muted">{{perc percentage}}</div>
					</div>
				{{/each}}

			{{/each}}
		</div>
	</div>
	{{{{/raw-helper}}}}
</script>

once i got this working, i was mostly ok, but then i did stupid crap like adding a helper in the node.js app.js when i really needed it in the client-side app.js. i probably shouldn’t have named those files the same. so what do the results look like? i’m going to link to the demo of course, but here are some examples. first, my own blog:

pi_ray

next, gruber of daring fireball :

pi_df

and finally, sarah palin’s blog :

pi_sp

want to try it yourself? check out the demo here: http://bloginsights.mybluemix.net/ . you can see the entire source code for the project here: https://github.com/cfjedimaster/bloginsights .


Bluemix Insight (email client)

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Boosting Application Performance With MicroStream and Redis Integration
  • Five Java Books Beginners and Professionals Should Read
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices
  • Clear Details on Java Collection ‘Clear()’ API

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: