如何格式化watson对话的响应以显示用户?

时间:2018-03-19 16:08:45

标签: python-2.7 watson-conversation

我在pycharm工作,只是与沃特森对话服务进行简单的互动,你提出问题并回复,但是沃森的回复也发送了我不想要的各种其他信息要显示,我只想显示答案,然后让用户输入另一个问题。代码如下,任何人都可以帮忙吗?

import json
import watson_developer_cloud

conversation = watson_developer_cloud.ConversationV1(username='', password='', version='2018-03-16')
question = raw_input("what is youre question?\n")
response = conversation.message( workspace_id='*', input={ 'text': question }) 
print(json.dumps(response, indent=2))

Watson随后回复:

{ "entities": [], "intents": [ { "confidence": 1, "intent": "hello" } ], "output": { "text": [ "Good day to you.." ], "log_messages": [], "nodes_visited": [ "node_12_1519477177015" ] }, "context": { "conversation_id": "", "system": { "dialog_stack": [ { "dialog_node": "root" } ], "dialog_request_counter": 1, "dialog_turn_counter": 1, "branch_exited": true, "_node_output_map": { "node_12_1519477177015": [ 0 ] }, "branch_exited_reason": "completed" } }, "input": { "text": "hello" } }   
Process finished with exit code 0

我可以看到响应在那里,但它似乎不在我可以使用print命令调用的变量中。

1 个答案:

答案 0 :(得分:0)

结果位于response,这是一个对象。

您可以通过以下方式访问Watson中的文字:

print('\n'.join(response['output']['text']))