【问题标题】:python/chatterbot: get_response different behaviour on print and saving in dictpython/chatterbot:get_response 打印和保存在 dict 中的不同行为
【发布时间】:2017-05-19 15:51:02
【问题描述】:

目前我在使用烧瓶和聊天机器人设置简单的 REST 服务时遇到问题。可以查看完整代码here

目标是,服务返回一个 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>

当我尝试用这个对象对字典进行 jsonify 时,我收到以下错误:

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

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

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

我会得到正确的输出

> Hello

我只想将纯响应保存在 dict 中,以便将其作为 json 返回给客户端。

谁能解释一下这个问题?

提前致谢!

【问题讨论】:

    标签: python chatbot


    【解决方案1】:

    通过使用 .符号(see heere).

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多