2017-03-23 57 views
1

有没有人有使用Java对Watson自然语言理解进行调用的示例? API文档仅显示节点。然而,SDK中有一个类支持它 - 但没有关于如何构建所需的'Features''AnalyzeOptions'或'Builder'输入的文档。Watson自然语言理解Java示例

下面是抛出一个“功能不能为空”的一个片段 - 我只是在黑暗中在这一点上

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

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

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

回答

0

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

确保您使用默认标题填入您的NLU服务(setDefaultHeaders())。我把这些从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;