我开始为自己的聊天机器人编写脚本,但我有一个基本问题无法使用可用文档解决。在定义聊天机器人实例时,我们给出了聊天机器人的名称。(如下所示):
bot = ChatBot('John', logic_adapters = ['chatterbot.logic.BestMatch']
在这里,我们可以使用这个名字 'John' 来调用一些函数或者在某个地方显示它吗?还是仅供参考?请告知。
我开始为自己的聊天机器人编写脚本,但我有一个基本问题无法使用可用文档解决。在定义聊天机器人实例时,我们给出了聊天机器人的名称。(如下所示):
bot = ChatBot('John', logic_adapters = ['chatterbot.logic.BestMatch']
在这里,我们可以使用这个名字 'John' 来调用一些函数或者在某个地方显示它吗?还是仅供参考?请告知。
看看我的代码,它可以满足您的需求:
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
def bot(title, read_only = True): # , don't learn from exchanges (responses exist)
Bot = ChatBot(title,
#filters=["chatterbot.filters.RepetitiveResponseFilter"],
logic_adapters=[{
'import_path': 'chatterbot.logic.BestMatch',
"statement_comparision_function": "chatterbot.comparisions.levenshtein_distance",
"response_selection_method": "chatterbot.response_selection.get_first_response"
}])
print('Training corpus',title)
trainer=ChatterBotCorpusTrainer(Bot)
trainer.train("chatterbot.corpus.english." + title)
return Bot # trained instance
bots = {}
# check what corpora available and traina a bot for each one
for item in os.listdir('C:/Python/Python38/lib/site-packages/chatterbot_corpus/data/english/'):
corpus = item.split('.') # of the form 'item.yml'
name = corpus[0]
bots[name]= bot(name) # add bot instance to dictionary of available bots
def exchange(input): # let's get a response to our input
bot = bots[context]
response = bot.get_response(input)