Watson自然语言理解Java示例

Watson自然语言理解Java示例,java,nlp,watson,Java,Nlp,Watson,有人举过使用Java调用Watson Natural Language Understanding的例子吗?API文档仅显示节点。但是SDK中有一个类支持它,但没有关于如何构造所需的“功能”“AnalyzeOptions”或“Builder”输入的文档 这里有一个片段抛出了一个“Features cannot be Null”-我现在只是在黑暗中摸索 String response = docConversionService.convertDocumentToHTML(doc)

有人举过使用Java调用Watson Natural Language Understanding的例子吗?API文档仅显示节点。但是SDK中有一个类支持它,但没有关于如何构造所需的“功能”“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上的测试

我已经让它使用文本字符串,看看上面的测试,让它使用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 headers=new HashMap();
headers.put(HttpHeaders.X_WATSON_LEARNING_OPT_OUT,String.valueOf(true));
headers.put(HttpHeaders.X_WATSON_TEST,String.valueOf(true));
返回标题;
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;