有人有使用 Java 调用 Watson Natural Language Understanding 的示例吗? API 文档仅显示 Node.js。然而,SDK 中有一个类支持它 - 但没有关于如何构造所需的“功能”、“AnalyzeOptions”或“Builder”输入的文档。
这是一个抛出“功能不能为空”的代码片段 - 此时我只是在黑暗中摸索
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 一起工作并不会太多(将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);
确保使用默认 header (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;
关于java - Watson Natural Language Understanding Java 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42981868/