Learn with our tutorials and training

developerWorks provides tutorials, articles and other
technical resources to help you grow your development skills
on a wide variety of topics and products. Learn about a specific
product or take a course and get certified. So, what do you want to learn
about?

Featured products

Featured destinations

Find a community and connect

Learn from the experts and share with other developers in one of our
dev centers. Ask questions and get answers with dW answers. Search for local events
in your area. All in developerWorks communities.

Sample code: Identify the tone of written text with the Watson Tone
Analysis 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 tone analysis
service. Given some text, Watson evaluates the tone, looking for qualities
such as the speaker's levels of anger, disgust, joy, fear, and sadness.

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 used there as well.

The public static void main(String[] args) method. This
uses a JsonParser to convert the default parameters into
a JSON object, then it calls...

...the public static JsonObject main(JsonObject args)
method. This is the action the IBM Cloud Functions platform invokes once the code is deployed.

Lines 58-68 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 indicate the emotional, language, and social
tones for the entire document. You can also get the analysis for each
sentence in the document. See the overview of the Tone Analyzer service or the github repo for the Java API for more information.

Try changing 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 Tone Analyzer service: