Watson 自然语言理解 Java 示例

Watson Natural Language Understanding Java Example

有没有人有使用 Java 调用 Watson Natural Language Understanding 的示例? API 文档仅显示 Node.然而,SDK 中有一个 class 来支持它 - 但没有关于如何构建所需的 'Features' 'AnalyzeOptions' 或 'Builder' 输入的文档。

这是一个抛出 'Features cannot be Null' 的片段 - 此时我只是在黑暗中摸索

        String response = docConversionService.convertDocumentToHTML(doc).execute();

        Builder b = new AnalyzeOptions.Builder();
        b.html(response);

        AnalyzeOptions ao = b.build();
        nlu.analyze(ao);

在 API 参考文献发布之前,您是否尝试查看 github 上的测试? See here for NaturalLanguageUnderstandingIT

我已经让它与文本字符串一起工作,并且查看上面的测试,让它与 URL 或 HTML 一起工作不会太多(改变例如从 text() 到 html() 的 AnalyzeOptions 构建器调用。

代码示例:

final NaturalLanguageUnderstanding understanding =
  new NaturalLanguageUnderstanding(
    NaturalLanguageUnderstanding.VERSION_DATE_2017_02_27);
understanding.setUsernameAndPassword(serviceUsername, servicePassword);
understanding.setEndPoint(url);
understanding.setDefaultHeaders(getDefaultHeaders());

final String testString =
  "In remote corners of the world, citizens are demanding respect"
    + " for the dignity of all people no matter their gender, or race, or religion, or disability,"
    + " or sexual orientation, and those who deny others dignity are subject to public reproach."
    + " An explosion of social media has given ordinary people more ways to express themselves,"
    + " and has raised people's expectations for those of us in power. Indeed, our international"
    + " order has been so successful that we take it as a given that great powers no longer"
    + " fight world wars; that the end of the Cold War lifted the shadow of nuclear Armageddon;"
    + " that the battlefields of Europe have been replaced by peaceful union; that China and India"
    + " remain on a path of remarkable growth.";
final ConceptsOptions concepts =
  new ConceptsOptions.Builder().limit(5).build();

final Features features =
  new Features.Builder().concepts(concepts).build();
final AnalyzeOptions parameters = new AnalyzeOptions.Builder()
  .text(testString).features(features).returnAnalyzedText(true).build();

final AnalysisResults results =
  understanding.analyze(parameters).execute();
System.out.println(results);

确保使用默认 headers (setDefaultHeaders()) 填充 NLU 服务。我从 WatsonServiceTest 中提取了这些(我 post link 但我的代表太低了。只需在 WDC github 上使用 FindFile 选项)

final Map<String, String> headers = new HashMap<String, String>();
headers.put(HttpHeaders.X_WATSON_LEARNING_OPT_OUT, String.valueOf(true));
headers.put(HttpHeaders.X_WATSON_TEST, String.valueOf(true));
return headers;