Monday, July 7, 2014

Objective C 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 Objective C to call HP IDOL OnDemand APIs,  3 APIs usage will be given below for demonstration, which are:

Firstly, create an Objective C interface called IdolCall, which contains  the method to send request and get response from IDOL OnDemand as well as an utility method to encode parameters in URL.


 @interface IdolCall:NSObject   
  - (NSDictionary *)getResponse:(NSString *)url;   
  - (NSString *)getEncodedParam:(NSString *)paramStr;   
  @end  
  @implementation IdolCall  
 // send request to given url and get the json response  
  - (NSDictionary *)getResponse:(NSString *)urlStr {   
   NSURL *url = [NSURL URLWithString:urlStr];   
   NSData *data = [NSData dataWithContentsOfURL:url];   
   NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];   
   return dic;   
  }  
 // encode the parameter string  
  - (NSString *)getEncodedParam:(NSString *)paramStr {   
   return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge CFStringRef)paramStr, NULL, (__bridge CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", kCFStringEncodingUTF8));   
  }   
  @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. Now we define the apiKey and API urls as below:

 // your apiKey goes here  
 NSString * apiKey = @"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";  
 // define the API urls which will be used later.  
 NSString * findSimilar = @"http://api.idolondemand.com/1/api/sync/findsimilar/v1";  
 NSString * ocrDocument = @"http://api.idolondemand.com/1/api/sync/ocrdocument/v1";  
 NSString * analyzeSentiment = @"http://api.idolondemand.com/1/api/sync/analyzesentiment/v1";  
 // create IdolCall  
 IdolCall *idolCall = [[IdolCall alloc]init];  


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:

 // encode the parameter  
 NSString * encoded = [idolCall getEncodedParam:@"Hello World"];  
 // construct the url  
 NSString * url = [NSString stringWithFormat:@"%@?apiKey=%@&text=%@&print=all", findSimilar, apiKey, encoded];  
 // send the request to IDOL OnDemand  
 NSDictionary *dic = [idolCall getResponse:url];  

Now, the json response is kept in the NSDictionary dic, 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:

 NSArray *documents = [dic objectForKey:@"documents"];  
 for(NSDictionary *doc in documents) {  
   NSString *ref = [doc objectForKey:@"reference"];  
   NSString *content = [doc objectForKey:@"content"];  
   NSLog(@"%@",ref);  
   NSLog(@"%@", [content substringToIndex:100]);  
 }  

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