python/chatterbot: get_response 打印和保存在字典中的不同行为
python/chatterbot: get_response different behaviour on print and saving in dict
目前,我在使用 Flask 和聊天机器人设置简单的 REST 服务时遇到了问题。你可以看到完整的代码here.
目标是,服务 return 是一个 json,其中包含聊天机器人对给定请求的响应。
问题是,当我想将聊天机器人的响应保存在字典中时:
dialog = {
"id": 1,
"usersay": request,
# chatterbot function to get a response from the bot
"botsay": chatbot.get_response(request)
}
它将被保存为 chatterbot special Statement Object 样,然后看起来像这样:
"botsay": <Statement text:bot response>
当我尝试json使用此对象验证一个字典时,出现以下错误:
TypeError: Can't convert 'Statement' object to str implicitly
我在网上搜索了一个解决方案,但没有找到任何有用的东西。此外,我对 python 没有经验。
对我来说绝对无法解释的是,当我使用
>>> request = "Hi"
>>> print(chatbot.get_response(request))
我会得到正确的输出
> Hello
我只想将简单的响应保存在字典中,这样我就可以 return 将其作为 json 发送给客户端。
谁能解释一下这个问题?
提前致谢!
问题通过使用 .符号 (see heere).
>>> response = chatterbot.get_response("Hi")
>>> dialog = { ..., "botsay" = response.text, ... }
>>> print dialog
{ ..., "botsay": "Hello", ...}
目前,我在使用 Flask 和聊天机器人设置简单的 REST 服务时遇到了问题。你可以看到完整的代码here.
目标是,服务 return 是一个 json,其中包含聊天机器人对给定请求的响应。
问题是,当我想将聊天机器人的响应保存在字典中时:
dialog = {
"id": 1,
"usersay": request,
# chatterbot function to get a response from the bot
"botsay": chatbot.get_response(request)
}
它将被保存为 chatterbot special Statement Object 样,然后看起来像这样:
"botsay": <Statement text:bot response>
当我尝试json使用此对象验证一个字典时,出现以下错误:
TypeError: Can't convert 'Statement' object to str implicitly
我在网上搜索了一个解决方案,但没有找到任何有用的东西。此外,我对 python 没有经验。 对我来说绝对无法解释的是,当我使用
>>> request = "Hi"
>>> print(chatbot.get_response(request))
我会得到正确的输出
> Hello
我只想将简单的响应保存在字典中,这样我就可以 return 将其作为 json 发送给客户端。
谁能解释一下这个问题?
提前致谢!
问题通过使用 .符号 (see heere).
>>> response = chatterbot.get_response("Hi")
>>> dialog = { ..., "botsay" = response.text, ... }
>>> print dialog
{ ..., "botsay": "Hello", ...}