In November, we announced the release of the IBM Watson Salesforce SDK which helps developers working with Apex code more easily integrate Watson into their Salesforce applications. The initial release of the SDK provided support for the Watson Discovery and Watson Conversation services however we have been progressively adding other services for support.

Today, we’re happy to announce that the Watson Salesforce SDK supports all the current Watson API Platform services:

  • Discovery
  • Conversation
  • Natural Language Understanding
  • Natural Language Classifier
  • Personality Insights
  • Tone Analyzer
  • Language Translator
  • Visual Recognition
  • Speech to Text
  • Text to Speech

Text Insights Leveraging Natural Language Understanding

To showcase some of the most recent additions of SDK support, we’ll provide an example of calling the Natural Language Understanding (NLU) service. In this use case, we’re interested in analyzing a specific block of text to understand what the text is about. We’ll leverage the ‘analyze’ API and enable the ‘concepts’ feature to recognize the high-level concepts that are related to our text. Once you step through this example, you can substitute the ‘text’ parameter’s block of text below (which is currently an example about the Olympics) with any text you would like to analyze.

Note: In the Salesforce Developer Console, you can paste in the code below and execute it anonymously. We assume you have set your named credentials to store your endpoint and username/password. Otherwise, you will need to explicitly state your endpoint and credentials within your code.


IBMNaturalLanguageUnderstandingV1 naturalLanguageUnderstanding = new
IBMNaturalLanguageUnderstandingV1(IBMNaturalLanguageUnderstandingV1.VERSION_DATE_2017_02_27);
naturalLanguageUnderstanding.setEndPoint('callout:watson_natural_language_understanding_v1');

IBMNaturalLanguageUnderstandingV1Models.ConceptsOptions concepts = new
IBMNaturalLanguageUnderstandingV1Models.ConceptsOptionsBuilder()
  .limitField(40)
  .build();
concepts = concepts.newBuilder().build();

IBMNaturalLanguageUnderstandingV1Models.Features features = new IBMNaturalLanguageUnderstandingV1Models.FeaturesBuilder()
  .concepts(concepts)
  .build();
features = features.newBuilder().build();

IBMNaturalLanguageUnderstandingV1Models.AnalyzeOptions options = new
IBMNaturalLanguageUnderstandingV1Models.AnalyzeOptionsBuilder()
  .text('The modern Olympic Games or Olympics are leading international sporting events
featuring summer and winter sports competitions in which thousands of athletes
from around the world participate in a variety of competitions. The Olympic
Games are considered the world foremost sports competition with more than 200
nations participating. The Olympic Games are held every four years, with the
Summer and Winter Games alternating by occurring every four years but two years
apart.')
  .features(features)
  .clean(false)
  .fallbackToRaw(true)
  .returnAnalyzedText(true)
  .language('en')
  .limitTextCharacters(100)
  .build();

IBMNaturalLanguageUnderstandingV1Models.AnalysisResults response = naturalLanguageUnderstanding.analyze(options);
System.debug(response);

 

Note: You can alternatively pass HTML or provide a URL to NLU for analysis. To pass a URL, simply comment out your ‘text’ parameter and add this line of code to your ‘options’ object:

.url('https://en.wikipedia.org/wiki/Olympic_Games')

 

To view the results, go to the logs tab, double-click on the row you just executed and then check off the “Debug Only” option. If successful, you will see something similar to the below (data in results will vary):

 

Great work! You’ve just successfully queried the Watson Natural Language Understanding service using the Salesforce SDK.

Next Steps

The SDK contains many assets including end-to-end examples for all services, however if you are new to Watson and want a head start, check out our blogs for examples of integrating Discovery and Conversation into your Salesforce apps. You can also check out a more comprehensive Discovery lab to get you moving further.


About The Author

Marc Nehme is a Chief Architect in the Watson Embed organization at IBM. His primary focus is working with strategic partners to help them achieve significant business value by creating Watson solutions that scale across their organizations. Marc has been in the IBM Watson family since 2014 and previously focused his efforts in the IBM Watson Delivery organization where he drove various customer projects from concept to production. You can follow Marc here.

Learn more about the Watson Salesforce SDK

Join The Discussion

Your email address will not be published. Required fields are marked *