Monday, November 6, 2017

Integrating Watson Alchemy API with IBM Data Science Experience

This blog describes - Integrating the "Watson Alchemy API" service in IBM Bluemix with RStudio in IBM Data Science Experience.

Here, we use different NLP API's provided by Alchemy API from R Studio in IBM Data Science Experience. We use the rJava library to get sentiment, entities, relation etc on unstructured data.


1) Setup the "Watson Alchemy API" service in IBM Bluemix


Login to https://console.ng.bluemix.net/ and create an account then create a Alchemy Service.

Get the apikey from service credentials as below.

















2) Build a Java Application

 
Download Watson Developer Cloud Java SDK

Create a Java Project with the below code

package com.bluemix;
import java.util.HashMap;
import java.util.Map;

import com.ibm.watson.developer_cloud.alchemy.v1.AlchemyLanguage;
import com.ibm.watson.developer_cloud.alchemy.v1.model.DocumentSentiment;
import com.ibm.watson.developer_cloud.alchemy.v1.model.Entities;
import com.ibm.watson.developer_cloud.alchemy.v1.model.TypedRelations;

public class BlueMix_Alchemy_API {
       String Document;
      
       public static void main(String[] args) {}

       public String getSentiment(String inputStr, String ApiKey) {
             AlchemyLanguage service = new AlchemyLanguage();
           service.setApiKey(ApiKey);
        Map<String, Object> params = new HashMap<String, Object>();
           params.put(AlchemyLanguage.TEXT,inputStr);
           DocumentSentiment sentiment = service.getSentiment(params).execute();
           return sentiment.toString();
       }
      
       public String getTypedRelations(String inputStr, String ApiKey) {
             AlchemyLanguage service = new AlchemyLanguage();
           service.setApiKey(ApiKey);
        Map<String, Object> params = new HashMap<String, Object>();
           params.put(AlchemyLanguage.TEXT,inputStr);
        TypedRelations relations = service.getTypedRelations(params).execute();
           return relations.toString();

       }
      
       public String getEntities(String inputStr, String ApiKey) {
             AlchemyLanguage service = new AlchemyLanguage();
           service.setApiKey(ApiKey);
           Map<String, Object> params = new HashMap<String, Object>();
           params.put(AlchemyLanguage.TEXT,inputStr);
           Entities entities = service.getEntities(params).execute();
           return entities.toString();
       }
}

 

























Generate the java jar for the java code.

3) Setup the "IBM Data Science Experience"

 
Login to http://datascience.ibm.com and create an account.



 Open R Studio and install the R Library - rJava

Create a folder and set it as working directory. Upload the java jar & java-sdk-3.3.1-jar-with-dependencies.jar to the working directory.

R Commands:

> library(rJava)
>
> cp = c("/home/rstudio/WatsonAlchemySentimentAnalysis/WatsonAlchemySentimentAnalysis.jar",
        "/home/rstudio/WatsonAlchemySentimentAnalysis/java-sdk-3.3.1-jar-with-dependencies.jar")

>
> .jinit(classpath=cp)
>
> instance = .jnew("com.bluemix.BlueMix_Alchemy_API")
>
> sentiment <- .jcall(instance, "S", "getSentiment", "IBM Watson won the Jeopardy television show hosted by Alex Trebek","<provideTheAPIKeyFromBlumix>")
>
> cat(sentiment)
>
> entities <- .jcall(instance, "S", "getEntities", "IBM Watson won the Jeopardy television show hosted by Alex Trebek","<provideTheAPIKeyFromBlumix>")
>
> cat(entities)
>
> relation <- .jcall(instance, "S", "getTypedRelations", "IBM Watson won the Jeopardy television show hosted by Alex Trebek","<provideTheAPIKeyFromBlumix>")
>
> cat(relation)

Link to GitHub code





















































Further Reading: 

Integrating the "IBM BigInsights for Apache Hadoop" service in IBM Bluemix with RStudio in IBM Data Science Experience - Part 1 


Integrating the "IBM BigInsights for Apache Hadoop" service in IBM Bluemix with RStudio in IBM Data Science Experience - Part 2



5 comments:

Coepd BA Trainings said...

We at Coepd declared Data Science Internship Programs (Self sponsored) for professionals who want to have hands on experience. We are providing this program in alliance with IT Companies in COEPD Hyderabad premises. This program is dedicated to our unwavering participants predominantly acknowledging and appreciating the fact that they are on the path of making a career in Data Science discipline. This internship is designed to ensure that in addition to gaining the requisite theoretical knowledge, the readers gain sufficient hands-on practice and practical know-how to master the nitty-gritty of the Data Science profession. More than a training institute, COEPD today stands differentiated as a mission to help you "Build your dream career" - COEPD way.

http://www.coepd.com/AnalyticsInternship.html

Teju Teju said...

Thank you. Well it was the nice to post and very helpful information on Big data hadoop online Training

devi priya said...

I recently completed this Data science certification training at ExcelR. I found this course very demanding. I learned a lot in this course. I was particularly impressed with the trainers which is the best feature of ExcelR. There is a wide breadth of topics covered in a short period of time. Love ExcelR.
Data science certification training=https://www.excelr.com/business-analytics-training-in-hyderabad/

Muoro Infotech said...

Hi, Thank you for sharing such useful information. I have gone through your blog post it's such a useful and nice information to know about. If you are looking to know more about the Big Data Management and to get to know about the latest trends just move to www.muoro.io

aravind g said...
This comment has been removed by the author.