赋予chatbot名称有什么用?

时间:2018-06-28 11:11:34

标签: chatterbot

我开始为自己的聊天机器人编写脚本,但我有一个基本问题,使用现有文档无法解决。在定义聊天机器人实例时,我们给出聊天机器人的名称。 (如下所示):

bot = ChatBot(         '约翰',         logic_adapters = [                  'chatterbot.logic.BestMatch']

在这里,我们是否可以像使用其调用某些函数或将其显示在某处一样使用该名称“ John”?还是仅供参考?请告知。

1 个答案:

答案 0 :(得分:0)

看看我的代码,它做你想要的:

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)