首页 文章

python / chatterbot:get_response打印和保存在dict中的不同行为

提问于
浏览
0

目前我在使用flask和chatterbot设置简单的REST服务时遇到问题 . 你可以看到完整的代码here .

目标是,服务返回一个json,其中包含来自chatbot的响应给定的请求 .

问题是,当我想在dict中保存来自聊天机器人的响应时:

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>

当我尝试用这个对象jsonify dict时,我收到以下错误:

TypeError: Can't convert 'Statement' object to str implicitly

我在网上搜索找到了解决方案但没有发现任何有用的东西 . 另外,我对python没有经验 . 对我来说绝对无法解释的是,当我使用时

>>> request = "Hi"
>>> print(chatbot.get_response(request))

我会得到正确的输出

> Hello

我只想在dict中保存简单的响应,所以我可以将它作为json返回给客户端 .

有谁能解释这个问题?

提前致谢!

1 回答

  • 0

    通过简单访问Statement对象的"text"属性解决了问题 . 符号(see heere).

    >>> response = chatterbot.get_response("Hi")
    >>> dialog = { ..., "botsay" = response.text, ... }
    >>> print dialog
    { ..., "botsay": "Hello", ...}
    

相关问题