学习功能的Python Chatterbot问题

问题描述

我正在编写一个使用chatterbot进行交互的discord Bot。

我想创建一个使机器人通过聊天学习输入的命令。

@client.command(aliases=['tc'])
async def treinocustom(ctx,response,input_statement):
    chatbot.learn_response(response,input_statement)
    ctx.send('Thanks for the Feedback')

但是当我执行命令时,控制台上会显示错误

Ignoring exception in command treinocustom:
Traceback (most recent call last):
  File "C:\Users\user\anaconda3\envs\envbom\lib\site-packages\discord\ext\commands\core.py",line 85,in wrapped
    ret = await coro(*args,**kwargs)
  File "C:/Users/user/PycharmProjects/SamuelBot/SamuelBot.py",line 227,in treinocustom
    chatbot.learn_response(arg1,arg2)
  File "C:\Users\user\anaconda3\envs\envbom\lib\site-packages\chatterbot\chatterbot.py",line 225,in learn_response
    statement.in_response_to = prevIoUs_statement
AttributeError: 'str' object has no attribute 'in_response_to'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\user\anaconda3\envs\envbom\lib\site-packages\discord\ext\commands\bot.py",line 903,in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\user\anaconda3\envs\envbom\lib\site-packages\discord\ext\commands\core.py",line 855,in invoke
    await injected(*ctx.args,**ctx.kwargs)
  File "C:\Users\user\anaconda3\envs\envbom\lib\site-packages\discord\ext\commands\core.py",line 94,in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'in_response_to'

我该如何解决

解决方法

所以,这里的问题是,chatterbot 函数 learn_response() 不接受字符串作为参数,而是一种称为 Statement 的特殊数据类型(chatterbot 本地)。因此,要获得此数据类型的响应,您必须首先通过以下方式导入它:

from chatterbot.conversation import Statement

在代码的顶部。其次,只需对您的 treinocustom() 函数进行以下更改:

@client.command(aliases = ["tc"])
async def treinocustom(ctx,*,parts):
    if parts.count(">") == 1:
        Question,Answer= [i.strip() for i in parts.split('>')]
        #here,we are changing the strings 'Question' and 'Answer' to a Statment datatype.
        input_statement = Statement(text=Question) 
        correct_response = Statement(text=Answer)
        chatbot.learn_response(correct_response,input_statement)
        await ctx.send("Ok,I have learnt the correct response to this query!")
    else:
        await ctx.send("Please enter only two arguments!")

所以,我在您的代码中注意到,responseinput_statement 参数只会将一个词作为输入,但我们实际上需要句子作为输入。

不过,在上面的这个函数中,用户可以像这样给出命令:

[bot_prefix] tc what is your name? > My name is ChatterBot!

其次,我将 QuestionAnswer 字符串转换为 Statement,learn_response() 函数可以识别。

第三,即使用户在“>”前后输入了多少个空格,换句话说,即使是

[bot_prefix] tc what is your name? > My name is ChatterBot!

将完美运行!这是由于使用了 strip() 函数...

我遇到了和你一样的问题,但是在挖掘了 chatterbot 的 gitup repo 之后,我找到了一些非常好的示例代码,并从中找出了函数的正确用法。我你有兴趣,你可以找到learn_response()示例代码here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...