Watson自然语言理解Java示例

时间:2017-03-23 16:31:52

标签: java nlp watson

有没有人有一个使用Java调用Watson Natural Language Understanding的例子? API文档仅显示Node。但是SDK中有一个类支持它 - 但是没有关于如何构建所需的'功能''AnalyzeOptions'或'Builder'输入的文档。

这是一个抛出'特征不能为空'的片段 - 我现在只是在黑暗中摸索

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

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

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

1 个答案:

答案 0 :(得分:1)

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

我已经使用了一个文本字符串,并且查看上面的测试,使用URL或HTML(将AnalyzeOptions构建器调用从text()更改为html)不会太多( )例如)。

代码示例:

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);

确保使用默认标头(setDefaultHeaders())填充NLU服务。我从WatsonServiceTest中提取了这些内容(我发布链接但我的代表太低了。只需在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;