diff --git a/README.md b/README.md index 505df15..63fa1a7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # What this is -Completed code for the tutorial found in the ['Getting up and running with HPE Haven OnDemand' video](http://www.youtube.com/watch?v=https://youtu.be/8aW5XDbd4A8?t=15m32s). +This is a sample project to demonstrate the use of the Haven OnDemand client library and to help illustrate how Haven OnDemand’s powerful Text Analysis APIs can be used. + +This Node.js app receives text messages via [Twilio’s webhook service](https://www.twilio.com/platform/webhooks), analyzes the sentiment, using our [Analyze Sentiment API](https://dev.havenondemand.com/apis/analyzesentiment#overview), extracts any key concepts, using our [Concept Extraction API](https://dev.havenondemand.com/apis/extractconcepts#overview), and extracts any entities (famous people, notable places, companies, organizations), using our [Entity Extraction API](https://dev.havenondemand.com/apis/extractentities#overview). All of this information is then printed to the console. + +A tutorial can be found in the ['Getting up and running with HPE Haven OnDemand' video](http://www.youtube.com/watch?v=8aW5XDbd4A8?t=15m32s). **Update: _The code has since been changed, but the key concepts remain the same._** # What this does ![diagram](./diagram.png) @@ -10,9 +14,8 @@ Essentials you’ll need to create this app: * [Twilio account](https://www.twilio.com ) - to deliver text messages to the webhook * [ngrok](https://ngrok.com/) - to receive POST requests on local computer from external APIs -To help illustrate how Haven OnDemand’s powerful Text Analysis APIs can be used, we’re this Node.js app receives a text message via [Twilio’s webhook service](https://www.twilio.com/platform/webhooks), analyze the sentiment, using our [Analyze Sentiment API](https://dev.havenondemand.com/apis/analyzesentiment#overview), extract any key concepts, using our [Concept Extraction API](https://dev.havenondemand.com/apis/extractconcepts#overview), and extract any entities (famous people, notable places, companies, organizations), using our [Entity Extraction API](https://dev.havenondemand.com/apis/extractentities#overview), then print all of this information to the console. - Simply clone this repo onto your local machine and run: + ``` npm install ``` diff --git a/diagram.png b/diagram.png index 1c1456e..29ce010 100644 Binary files a/diagram.png and b/diagram.png differ diff --git a/index.js b/index.js index cf69c85..3593a68 100644 --- a/index.js +++ b/index.js @@ -1,42 +1,42 @@ -express = require('express') -app = express() -http = require('http').Server(app) -bodyParser = require('body-parser') -urlencoded = bodyParser.urlencoded({extended: false}) -havenondemand = require('havenondemand') -client = new havenondemand.HODClient('http://api.havenondemand.com', 'API_KEY') +'use strict'; +var expressApp = require('express')() +var urlencoded = require('body-parser').urlencoded({extended: false}) +var havenondemand = require('havenondemand') -port = process.env.PORT || 5000 +var port = process.env.PORT || 5000 +var client = new havenondemand.HODClient('API_KEY', 'v1', 'OPTIONAL_PROXY') -app.post('/text_processor', urlencoded, function(req, res){ +var outputResults = function(text, sentiment, score, concepts, entities) { + console.log("\n---------------------------------------------") + console.log("---------------------------------------------") + console.log("RECEIVED: ", text) + console.log("\nSENTIMENT: ", sentiment, " | ", score) + console.log("\nCONCEPTS:") + console.log(concepts) + console.log("\nENTITIES:") + console.log(entities) + console.log("---------------------------------------------") + console.log("---------------------------------------------\n") +} + +expressApp.post('/text_processor', urlencoded, function(req, res){ var text = req.body["Body"] - var data1 = {text: text} - var data2 = {text: text, entity_type: ['people_eng', 'places_eng', 'companies_eng', 'organizations']} - client.call('analyzesentiment', data1, function(err1, resp1, body1){ - var sentiment = resp1.body.aggregate.sentiment - var score = resp1.body.aggregate.score - client.call('extractconcepts', data1, function(err2, resp2, body2){ - var concepts = resp2.body.concepts - client.call('extractentities', data2, function(err3, resp3, body3){ + var entityData = {text: text, entity_type: ['people_eng', 'places_eng', 'companies_eng', 'organizations']} + + client.call('analyzesentiment', {text: text}, function(err1, resp1){ + client.call('extractconcepts', {text: text}, function(err2, resp2){ + client.call('extractentities', entityData, function(err3, resp3){ + var sentiment = resp1.body.aggregate.sentiment + var score = resp1.body.aggregate.score + var concepts = resp2.body.concepts var entities = resp3.body.entities - console.log("---------------------------------------------") - console.log("---------------------------------------------") - console.log(text + " | " + sentiment + " | " + score) - printStuff("Concepts", concepts) - printStuff("Entities", entities) + outputResults(text, sentiment, score, concepts, entities) + res.sendStatus(200) }) }) }) }) -printStuff = function(string, arr) { - console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") - console.log(string + ": ") - for (var i=0; i