Tuesday, June 17, 2014

IDOL OnDemand API call using JAVA

IDOL OnDemand API Call using Java
IDOL OnDemand delivers a rich set of web service APIs to enable developers to create ground-breaking data driven apps.To view complete set of API here
In this tutorial we are going to cover only 3 APIs,
  • Find Similar
  • OCR Document
  • Sentiment Analysis
Find Similar API : Finds documents that are conceptually similar to your text or a document. Returns documents in the IDOL OnDemand databases that are similar to text or a document that we provide. we can either submit text, an index reference, a file, an object store reference, or a URL.
In this tutorial, Find Similar API call from Wikipedia on the phrase 'Hello World'.

OCR Document API : The OCR Document API extracts text from an image that we provide. The API returns the extracted text, along with information about the location of the detected text in the original image.
In this tutorial, OCR Document API will call from the following image: 'http://www.java-made-easy.com/images/hello-world.jpg' and display the extracted text.

Sentiment Analysis API : The Sentiment Analysis API analyzes text to return the sentiment as positive, negative or neutral. It contains a dictionary of positive and negative words of different types, and defines patterns that describe how to combine these words to form positive and negative phrases.
In this tutorial, Sentiment Analysis API call with the text from the Hello World response (i.e reference link ). Display the Sentiment Score along with the Sentiment Rating

Java Application, in web.xml



in test.isp

<div class="section1">
<h3>In this tutorial</h3>

<div class="block">
<img src="images/similar.jpg" alt="" width="40" height="55" />
<h6>  <a href="findsimilar?api=api1"> servlet </a>  .</h6>

<div class="block">
<img src="images/ocr.jpg" alt="" width="40" height="55"/>
<h6>  <a href="findsimilar?api=api2"> servlet </a>  </h6>

<div class="block">
<img src="images/senti.jpg" alt="" width="40" height="55"/>
<h6> <a href="findsimilar?api=api3"> servlet </a>   </h6>


In servlet HelloWorld.java
String reference = "";	
String url3 = "https://api.idolondemand.com/1/api/sync/analyzesentiment/v1?url="+reference+"&apikey=dac630d2-4aed-45b7-8fc7-99fa87858460";
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String url = "";  	
        String url1 = "https://api.idolondemand.com/1/api/sync/findsimilar/v1?text=Hello+World&indexes=wiki_eng&apikey=dac630d2-4aed-45b7-8fc7-99fa87858460";
        String url2 = "https://api.idolondemand.com/1/api/sync/ocrdocument/v1?url=http%3A%2F%2Fwww.java-made-easy.com%2Fimages%2Fhello-world.jpg&apikey=dac630d2-4aed-45b7-8fc7-99fa87858460";
       	String api = request.getParameter("api");	
		if (api.equalsIgnoreCase("api1")){
			url = url1;
		else if(api.equalsIgnoreCase("api2")){
			url = url2;
			url = url3;			
           Client1 cl1 = new Client1(); 
			String str =  cl1.run(url);			
			if (api.equalsIgnoreCase("api1")){
				 String []a =  str.split("reference");
				 String []b = a[1].split("weight");
				 reference =  b[0].substring(b[0].indexOf("http"), (b[0].indexOf(",",b[0].indexOf("http")))-1 );				 
				 url3 = "https://api.idolondemand.com/1/api/sync/analyzesentiment/v1?url="+reference+"&apikey=dac630d2-4aed-45b7-8fc7-99fa87858460";
				 request.setAttribute("ref", reference);
			if (api.equalsIgnoreCase("api2")){			     
				 String []a = str.split("text", 3);
				 String []b = a[2].split("left");				
				 str =  b[0].substring((b[0].indexOf("="))+1, (b[0].indexOf(",",b[0].indexOf("left")))-1 );				 				
			if (api.equalsIgnoreCase("api3")){
			     String []a = str.split("aggregate", 3);			     
			     if (a.length !=1){
				 str = a[1];}				 
			request.setAttribute("servletName", str); 		    

in HelloWorld.java, we are using reference global variable and it stores link from the Find Similar API, which will be used for sentiment analysis API,

in Client.java

public String run(String url){
		String body = "";
		 HttpClient httpclient = new DefaultHttpClient();
		 HttpGet httpget = new HttpGet(url);
		 System.out.println("httpget : "+httpget);		 
		 BasicHttpResponse response = null;
		 try {
			response = (BasicHttpResponse) httpclient.execute(httpget);			
			StatusLine statusLine = response.getStatusLine();					
			HttpEntity entity = response.getEntity(); 
		    if (entity != null) {		    	
		    	body = EntityUtils.toString(entity);		        	
		 }catch (Exception e) {
			// TODO: handle exception
		 finally {
			    System.out.println("Finally !!!");
		return body;	 

<div class="section2">
<h3> API Response is </h3>
<div style="height: 227px; overflow:scroll;width:632px">

Output of Sentiment Analysis API

: {
    "sentiment": "neutral",
    "score": -0.06540204218347706
Post a Comment