Tuesday, July 8, 2014

Ruby call HP IDOL OnDemand

Hewlett Packard has developed a set of JSON-based REST API’s which enable “Big Data” type processing capabilities allowing developers to process information embedded in unstructured text and images in previously inaccessible formats.  This platform is called IDOL OnDemand, the APIs are published here https://www.idolondemand.com/developer/apis


In this post, I will use ruby to call HP IDOL OnDemand APIs,  3 APIs usage will be given below for demonstration, which are:

Firstly, we define a method which will be used to send request and get response from IDOL OnDemand. The method will accept the api name and parameter data. It will return the JSON object.


 def getResponse(apiName, data)  
     uri = URI("http://api.idolondemand.com/1/api/sync/%s/v1" % apiName)  
     uri.query = URI.encode_www_form(data)  
     res = Net::HTTP.get_response(uri)  
     return JSON.parse(res.body)  
 end  

In order to call the IDOL OnDemand API,  we need apply for api key since for each request sent to OnDemand, the apiKey parameter is required,  You need to sign up in IDOL OnDemand developer page(https://www.idolondemand.com/developer/apis)  to get the apiKey. In the tutorial, we will  find similar text with the words "Hello World" based on wiki page, detailed request and response information can refer to https://www.idolondemand.com/developer/apis/findsimilar , besides the apiKey parameter, we need pass the "text=Hello World" and enable "print=all" to get the text content in wiki page, here is the code:

 // get the response of Find Similar API with text "Hello World"  
 jsonResponse = getResponse("findsimilar",{:text => "Hello World", :print => "all", :apiKey => apiKey })


Now, the json response is kept in jsonResponse, for the detailed response json format, please refer to https://www.idolondemand.com/developer/apis/findsimilar#response , we can extract and print the reference and content with the following code:

 result = ""
 jsonResponse["documents"].each do |item|
   result+=item["reference"]+"\n"
   result+=item["content"][0..100]+"\n"
 end 


The calling for OCR Document and Sentiment Analysis API are similar as above.
OCR Document API will use the following url format:
http://api.idolondemand.com/1/api/sync/ocrdocument/v1?apiKey={apiKey}&url={url}
The url parameter will be an image url: http://www.java-made-easy.com/images/hello-world.jpg
The API will extract the text content from the image. Detail information about the API can refer to:

Sentiment Analysis API will use the following url format:
http://api.idolondemand.com/1/api/sync/analyzesentiment/v1?apiKey={apiKey}&url={url}
In the tutorial, the API will give us the sentiment score and rating based on the wiki page:http://en.wikipedia.org/wiki/Hello_world_program
Detail information about the API can refer to:

No comments:

Post a Comment