Sample code: Identify the context of natural language with the Watson Natural Language Classifier service
Interactive code: When you see Run at the top of a code sample, you can run the code, check the results, make changes, and run it again. When you see Deploy, you can deploy the code into the developerWorks IBM Cloud account. To deploy the code, please sign in.
About this code
This code shows you how to use the Java API for the Watson natural language classification service. Given some text and a context, Watson analyzes the text and returns a list of categories relevant to that text.
The code has three basic sections:
- Default values for the parameters. These are always used by the
public static void main(String args)method. If the call to
public static JsonObject main(JsonObject args)has no parameters or the wrong number of parameters, the default values are also used there.
public static void main(String args)method. This uses a
JsonParserto convert the default parameters into a JSON object, then it calls...
public static JsonObject main(JsonObject args)method. This is the action the IBM Cloud Functions platform invokes once the code is deployed.
Sign in to run, change, and deploy this code.
Lines 47-56 illustrate the Java API for the service. The Watson SDK wraps
the JSON returned by the service as a set of objects that are easy to work
with in Java. The results include the most likely classification (accessed
topClass() method) and a list of all classifications
that might be relevant. See the overview
of the Watson Natural Language Classifier service or the
repo for the Java API for more information.
See what else Watson can do. Change the text to see what happens.
Using this code once deployed
To use this code once you've deployed it, you must create an instance of the Watson Natural Language Classifier service:
To invoke the code, pass a JSON structure that matches the
data object as shown above with the following changes:
contextIdfield must contain the ID of a context you defined as part of the Natural Language Classifier service. This code will not work without a valid context ID. See the Natural Language Classifier documentation for details on how to create your own context.
endpointfields must be filled in with the credentials for your Natural Language Classifier service instance.
skip_authenticationfield must be
For more information, see the developerWorks tutorial Invoking actions on the IBM Cloud Functions platform.
- An overview of the Watson Natural Language Classifier service
- Create a free IBM Cloud account
- Node.js journeys on developer.ibm.com/code