Search our Blogs
Showing results for 
Search instead for 
Do you mean 
 

Up and running in 5 minutes with Haven OnDemand using Node.js, Ruby, and Python

It’s super easy to get up and running with each of your favorite software languages and web frameworks. In this blog post, I discuss how to download the client library for each respective language, how to include it in your app, and then how to call our Sentiment Analysis API on some hard coded text within the app.


This blog post is divided into individual sections for each of the software languages, so click the language in the table of contents below to bring you to that section. Each section is prefaced with a TL;DR commentated video of what is written here and a Github link with the completed app. Enjoy, and please leave us feedback in the comments. :)

 

Node.js

Ruby

Python

 

Node.js

Video

Github

 

First what you want to do is open up your terminal, create a new directory, and 'cd' into it. We’ll call it 'example_app'.

 

mkdir example_app
cd example_app

 

Next, create a package.json file by running

 

npm init

 

in the terminal. Click through the options until it prompts you for a ‘yes’ or ‘no’, and type ‘yes’. Next, what you want to do is look at the client library on Github. Then, install the client library and save it to the package.json file by running

 

npm install --save havenondemand

 

Next, create an index.js file

 

touch index.js

 

and now you can begin coding! Open up the index.js file, include the client library, and initialize it

 

var havenondemand = require('havenondemand')
var client = new havenondemand.HODClient('API_KEY', 'v1')

 

where you can find your API key here; the second parameter specifies the API version and is optional - defaults to 'v1'.

 

Then, let’s create some text and store it in an object which will be POSTed to the Sentiment Analysis API. Note - the client library accepts the parameters as a key value pair in a JSON object.

 

var text = 'I love puppies'
var data = {text: text}

 

Next, let’s call the Sentiment Analysis API and log the text, sentiment, and score to the console

 

 client.call('analyzesentiment', data, function(err, resp, body){
   var sentiment = body.aggregate.sentiment
   var score = body.aggregate.score
   console.log(text " | " + sentiment + " | " + score)
 })

 

Now that we have everything set, let’s go back to our terminal and run the code

 

node index.js

 

The text that was POSTed to the Sentiment Analysis API, the sentiment of it, and its score will be logged to the terminal

 

I love puppies | positive | 0.805340693105

 

 

Ruby

Video

Github

 

First what you want to do is open up your terminal, create a new directory, and 'cd' into it. We’ll call it ‘example_app’.

 

mkdir example_app
cd example_app

 

Next thing to do is look at the gem on Github. Then, install the Haven OnDemand gem by running

 

gem install havenondemand

 

Next, create an index.rb file

 

touch index.rb

 

and now you can begin coding! Open up the index.rb file and require the gem and initialize it

  

require 'havenondemand'
client = HODClient.new('API_KEY', 'v1')

 

where you can find your API key here; the second parameter specifies the API version and is optional - defaults to 'v1'.

 

Then, let’s create some text and store it in a hash which will be POSTed to the Sentiment Analysis API. Note - the gem accepts the parameters as a key value pair in a hash.

 

text = "I love puppies"
data = {text: text}

 

 Next, let’s call the Sentiment Analysis API and log the text, sentiment, and score to the console

 

r = client.post('analyzesentiment', data)
sentiment = r.response.body["aggregate"]["sentiment"]
score = r.response.body["aggregate"]["score"]
puts "#{text} | #{sentiment} | #{score}"

 

Now that we have everything set, let’s go back to our terminal and run the code

 

ruby index.rb

 

The text that was POSTed to the Sentiment Analysis API, the sentiment of it, and its score will be logged to the terminal

 

I love puppies | positive | 0.805340693105

 

 

Python

Video

Github

 

First what you want to do is open up your terminal, create a new directory, and 'cd' into it. We’ll call it ‘example_app’.

 

mkdir example_app
cd example_app

 

Next thing to do is look at the client library on Github. Then, install the client library from Github

 

pip install git+https://github.com/HPE-Haven-OnDemand/havenondemand-python

 

Next, create an index.py file

 

touch index.py

 

and now you can begin coding! Open up the index.py file and import the library and initialize it

  

from havenondemand.hodindex import HODClient
client = HODClient(apikey='API_KEY', apiversiondefault=1)

 

where you can find your API key here; the second parameter specifies the API version and is optional - defaults to '1'.

 

Then, let’s create some text and store it in a dictionary which will be POSTed to the Sentiment Analysis API. Note - the library accepts the parameters as a key value pair in a dictionary.

 

text = "I love puppies"
data = {'text': text}

 

Next, let’s call the Sentiment Analysis API and log the text, sentiment, and score to the console

 

r = client.post('analyzesentiment', data)
sentiment = r.json()['aggregate']['sentiment']
score = r.json()['aggregate']['score']
print(text + " | " + sentiment + " | " + str(score))

 

Now that we have everything set, let’s go back to our terminal and run the code

 

python index.py

 

The text that was POSTed to the Sentiment Analysis API, the sentiment of it, and its score will be logged to the terminal

 

I love puppies | positive | 0.805340693105

 

Comments
NiallMaher Level 4
‎10-02-2015 09:28 - edited ‎10-02-2015 09:29

Hi, 

 

If i wanted to change your code to Highlight Text instead of Sentiment Analysis. 

using python

 

What would I need to change ?

Developer Evangelist
| ‎11-03-2015 06:48

 Hi NiallMaher,

 

Use the following line of code when calling the client:

 

r = client.post('highlighttext', data={'highlight_expression': 'phrase_you_wish_to_highlight'},files={'file':open('path/to/file.pdf')})

For more parameters you can add in the data object, check this out.

NiallMaher Level 4
| ‎11-03-2015 06:52

Hi,

I figured this out a few day after asking the question.


But thanks for your responce.

 

Social Media
About the Author
Topics
† The opinions expressed above are the personal opinions of the authors, not of HPE. By using this site, you accept the Terms of Use and Rules of Participation