如何解决学习功能的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!")
所以,我在您的代码中注意到,response
和 input_statement
参数只会将一个词作为输入,但我们实际上需要句子作为输入。
不过,在上面的这个函数中,用户可以像这样给出命令:
[bot_prefix] tc what is your name? > My name is ChatterBot!
其次,我将 Question
和 Answer
字符串转换为 Statement
,learn_response() 函数可以识别。
第三,即使用户在“>”前后输入了多少个空格,换句话说,即使是
[bot_prefix] tc what is your name? > My name is ChatterBot!
将完美运行!这是由于使用了 strip()
函数...
我遇到了和你一样的问题,但是在挖掘了 chatterbot 的 gitup repo 之后,我找到了一些非常好的示例代码,并从中找出了函数的正确用法。我你有兴趣,你可以找到learn_response()
示例代码here。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。