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

Sentiment analysis of text and files using Ruby

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 Ruby is simple using Haven OnDemand’s Sentiment Analysis API. All you need to do is install the official Ruby gem, POST a block of raw text, publicly facing URL, or local file to Haven OnDemand’s Sentiment Analysis API, and obtain the result.

 

Code

Completed code

 

First, install the official Haven OnDemand Ruby gem:

 

gem install havenondemand

 

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

 

 

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

 

 

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

 

Next, you’ll call the Sentiment Analysis API by submitting either a block of raw text, publicly facing URL, or local file:

 

 

# data = {:url => 'https://techcrunch.com', :language => 'eng'} # uncomment if using URL
# data = {:file => File.new('/path/to/file', 'rb'), :language => 'eng'} # uncomment if using file
data = {:text => 'I like cats', :language => 'eng'} # uncomment if using block of raw text
response = client.post('analyzesentiment', data)
puts response.json()

 

 

Note: the optional “Language” parameter in the request that lets you specify the language of the original text to be analyzed. 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:

 

{
  "positive": [
    {
      "sentiment": "like",
      "topic": "cats",
      "score": 0.6085845466635199,
      "original_text": "I like cats",
      "original_length": 11,
      "normalized_text": "I like cats",
      "normalized_length": 11
    }
  ],
  "negative": [],
  "aggregate": {
    "sentiment": "positive",
    "score": 0.6085845466635199
  }
}

 

Learn more about sentiment analysis and effective uses here.

Comments
AspringDS Level 2
| ‎08-21-2016 11:06

Is there a way to use the sentiment analysis API in R?

Developer Evangelist
| ‎08-21-2016 09:23

@AspringDS - Checkout our API wrapper to do this in R.

 

https://github.com/HPE-Haven-OnDemand/havenondemand-r

 

Here is what it will look like:

 

# include havenondemand library
library(havenondemand)

# initialize HOD Client
client <- HODClient(apikey = apikey)

# get response
# include havenondemand library
library(havenondemand)

# initialize HOD Client
client <- HODClient(apikey = "your-api-key")

# call that result in error ('ur' parameter is wrong, it should be 'url')

result <- tryCatch({
    client$postRequest(params = list(text = "I like cats"), hodApp = HODApp$ANALYZE_SENTIMENT, mode = HODClientConstants$REQUEST_MODE$SYNC)
}, warning = function(w) {
    print('Warning block called.')
}, error = function(e) {
    print('Error block called.')
    print(e)
}, finally = {
    print('Finally block called.')
})

# print result
print(result)
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