Taxonomy Icon

Artificial Intelligence

Devices that run Java are everywhere these days, and IBM has a Java SDK that lets you integrate and use IBM Watson services on a Java device.

Learning objectives

This guide is for new developers who want to understand more about the NLU REST API call and how you can use it inside your Java code to call a microservice.

Prerequisites

Before beginning this tutorial, you’ll need:

  • An IBM Cloud account: If you do not have an IBM Cloud account, you can create one.
  • A Java IDE: I used Eclipse.

Estimated time

Completing this how-to should take approximately 15 minutes.

Steps

Create the Java project

  1. Download the Watson Java SDK. You can get more information about the Watson SDK on the watson-developer-cloud/java-sdk site.

  2. Open your IDE, and open new project.

    createproject1

  3. Name the project, and click Finish (make sure you’re using Java version 1.8 or higher).

    createproject2

  4. Right-click on the new project and create a new class.

    createproject2

  5. Name the class. Make sure you check the public static void main(String[] args).

    createproject2

  6. Insert the Watson SDK .jar file that you downloaded previously and use Watson premade classes by right-clicking on the project and going to Configure Build Path….

    createproject2

  7. Select Add External JARs….

    createproject2

  8. Select the .jar file you downloaded.

    createproject2

  9. Click OK, and you’re done. Now you can insert some sample code. Copy and paste the following code to your class.

    
     //Import gives you access to the classes inside the jar file you used.
     import com.ibm.watson.developer_cloud.natural_language_understanding.v1.NaturalLanguageUnderstanding;
     import com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalysisResults;
     import com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.AnalyzeOptions;
     import com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.EntitiesOptions;
     import com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.Features;
     import com.ibm.watson.developer_cloud.natural_language_understanding.v1.model.KeywordsOptions;
    
     public class Main {
    
       public static void main(String[] args) {
         //Creating the class of the service. Make sure to insert you service username and password.
         NaturalLanguageUnderstanding service = new NaturalLanguageUnderstanding(
               "2018-03-16",
               "username",
               "password"
             );
    
         //The text we want to analyze. You can insert any other text you like.
         String text = "IBM is an American multinational technology " +
           "company headquartered in Armonk, New York, " +
           "United States, with operations in over 170 countries.";
    
         //Entities and keywords are parameters you get back from the service about your text.
         EntitiesOptions entitiesOptions = new EntitiesOptions.Builder()
           .emotion(true)
           .sentiment(true)
           .limit(2)
           .build();
    
         KeywordsOptions keywordsOptions = new KeywordsOptions.Builder()
           .emotion(true)
           .sentiment(true)
           .limit(2)
           .build();
    
         Features features = new Features.Builder()
           .entities(entitiesOptions)
           .keywords(keywordsOptions)
           .build();
    
         AnalyzeOptions parameters = new AnalyzeOptions.Builder()
           .text(text)
           .features(features)
           .build();
    
         //Take the parameters and send them to your service for resutls.
         AnalysisResults response = service
           .analyze(parameters)
           .execute();
    
         //print the result
         System.out.println(response);
       }
    
     }
    

Create the NLU service

To create the NLU service:

  1. Go to the NLU Service and click Create. If you get errors about missing spaces, try to change the regions. If you don’t have any space, you need to create new space.

    createproject2

Input credentials

  1. After you create the service, select Service credentials from the left menu.

    createproject2

  2. Click New credential to create a username and password, and then click Add.

    createproject2

  3. Click View credentials and copy the username and password to the code.

    createproject2

    createproject2

Test the application

createproject2

Summary

Now that you’re familiar with the Waston Java SDK, you can use other Watson services. Try to call another Watson service: Language Translator. Natural Language Classifier. Personality Insights. Text To Speech. Visual Recognition. Tone Analyzer.