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

Sentiment analysis V2 of text and files using Node.js

The way humans communicate can be confusing and hard to analyze without the right training. What’s more, the way we store these communications as unstructured data makes analytics even harder still. Fortunately for developers, sentiment analysis from text and document files with Node.js is simple using Haven OnDemand’s Sentiment Analysis API. All you need to do is install the official Node.js module, POST a block of raw text, publicly facing URL, or local file to Haven OnDemand’s Sentiment Analysis API, and obtain the result.

 

Version 2 of the Sentiment Analysis API offers a ‘mode’ parameter, which allows a user to configure how precise the API is in determining what is considered sentimental and what is not. By default, ‘mode’ is set to ‘precision’, which tries to find the highest confidence sentiment matches. This option might give better results for longer documents. You can set ‘mode’ to ‘recall’ if you want to return more matches, which might be lower quality matches. This option might be appropriate for short documents, such as feedback surveys or tweets, because it picks up additional sentiment matches from short fragments.

 

Code

Completed code

 

First, install the official Haven OnDemand Node.js module:

 

 

npm install --save havenondemand

 

 

Next, open up the file you will write code in and require Haven OnDemand:

 

 

var havenondemand = require('havenondemand')
var client = new havenondemand.HODClient('APIKEY', 'v2')

 

 

Replace “APIKEY” with your API key, which can be found here after signing up.

 

Below this, add the built in ‘util’ module, which allows inspection of objects:

 

 

var util = require('util')

 

 

Next, you’ll call the Sentiment Analysis API by submitting either a block of raw text, publicly accessible URL, or local file. In this example, you’ll use a publicly accessible URL of the short story The Egg by Andy Weir (Great short story, by the way. Highly recommend reading it if you haven’t):

 

 

var data = {url: 'http://www.galactanet.com/oneoff/theegg_mod.html', language: 'eng', mode: 'precision'} // uncomment if using 'precision' mode
// var data = {url: 'http://www.galactanet.com/oneoff/theegg_mod.html', language: 'eng', mode: 'recall'} // uncomment if using 'recall' mode
 
client.post('analyzesentiment', data, function(err, resp, body) {
 console.log(util.inspect(resp.body, false, null))
})

 

 

Note: the optional “Language” parameter in the request that lets you specify the language of the original text to be analyzed. The ‘mode’ parameter works only with English. While there are 11 supported languages for sentiment analysis, English is used as the default if nothing is specified. You could also call the Language Identification API first to programmatically determine the language parameter to pass to the sentiment analysis API.

 

When you run the file, it will output the response of the API with the sentiment analyzed. It will look like this:

 

 

{ sentiment_analysis:
  [ { positive:
       [ { sentiment: 'Friendly',
           topic: 'Version',
           score: 0.6748984055823062,
           original_text: 'Friendly Version',
           original_length: 16,
           normalized_text: 'Friendly Version',
           normalized_length: 16,
           offset: 30 },
         { sentiment: 'right',
           topic: 'click an anchor',
           score: 0.41366197954985534,
           original_text: 'right click an anchor',
           original_length: 21,
           normalized_text: 'right click an anchor',
           normalized_length: 21,
           offset: 224 },
         ...
      negative:
       [ { sentiment: 'died',
           topic: null,
           score: -0.23474565253494276,
           original_text: 'when you died',
           original_length: 13,
           normalized_text: 'when you died',
           normalized_length: 13,
           offset: 5738 },
         { sentiment: 'fatal',
           topic: 'nonetheless',
           score: -0.6280145725181376,
           original_text: 'fatal nonetheless',
           original_length: 17,
           normalized_text: 'fatal nonetheless',
           normalized_length: 17,
           offset: 5863 },
         { sentiment: 'died',
           topic: null,
           score: -0.46377052838785104,
           original_text: 'You died',
           original_length: 8,
           normalized_text: 'You died',
           normalized_length: 8,
           offset: 6322 }
           …
],
      aggregate: { sentiment: 'positive', score: 0.2045580298139827 } } ] }



To see how the ‘mode’ affects the response from the Sentiment Analysis API, use the following for the ‘data’ variable (i.e. switch which one is commented and uncommented) to use 'recall' as the 'mode'

 

 

// var data = {url: 'http://www.galactanet.com/oneoff/theegg_mod.html', language: 'eng', mode: 'precision'} // uncomment if using 'precision' mode
var data = {url: 'http://www.galactanet.com/oneoff/theegg_mod.html', language: 'eng', mode: 'recall'} // uncomment if using 'recall' mode

 

 

and run the file again.

 

Learn more about sentiment analysis and effective uses here.

 



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