使用 java 从 Watson 对话中获取所有输出

Get all the output from Watson conversation using java

如果我有一个 IBM bluemix Watson 对话对话输出 JSON 如:

"output": {
    "text": {
        "values": [
            "What is your name?",
            "Name of the person?",
            "Please specify the name of the person."
        ],
        "selection_policy": "random",
        "append": true
    }
}

如何从输出响应中获取所有建议?

您可以使用上下文变量来保存用户使用 <? input.text ?> 所说的内容。尝试遵循这个简单的例子:

在上面这个Node中创建一个子节点,并添加:

{
  "context": {
    "userTypes": "<? input.text ?>"
  },
  "output": {
    "text": {
      "values": [
        "All you said here: $userTypes."
      ],
      "selection_policy": "sequential"
    }
  }
}

因此,在您的 Java 示例中,您可以使用以下方法获取此上下文变量的值:

private Map<String, Object> context = new HashMap<>();

 ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2016_09_20);
 service.setUsernameAndPassword('Your Watson service UserName', 'Your watson service PassWord');
 MessageRequest newMessage = new MessageRequest.Builder().inputText(inputmessage).context(context).build();
 MessageResponse response = service.message('Your Workspace Id', newMessage).execute();

   //Passing Context of last conversation
    if(response.getContext() !=null)
      {
        context.clear();

        context = response.getContext();

     }