首页 文章

如何让使用Chatterbot提问的ChatBot(版本-0.7.4)?

提问于
浏览
0

使用python创建了一个聊天机器人,它正在运行的流程是我正在使用并且根据Chatbot正在回复 . 但它应该以相反的方式完成,聊天机器人应首先通过欢迎/提问来启动,然后用户将回复 . 请建议在代码中进行一些转换,以便它可以相应地工作 . 提前感谢你 .

上面提到的代码是这样的:

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os

bot = ChatBot('Bot')
bot.set_trainer(ListTrainer)

for files in os.listdir('C:/Users/Username\Desktop\chatterbot\chatterbot_corpus\data/english/'):
    data = open('C:/Users/Username\Desktop\chatterbot\chatterbot_corpus\data/english/' + files, 'r').readlines()
    bot.train(data)

while True:
    message = input('You: ')
    if message.strip() != 'Bye'.lower():
        reply = bot.get_response(message)
        print('ChatBot:',reply)
    if message.strip() == 'Bye'.lower():
        print('ChatBot: Bye')
        break

1 回答

  • 0

    在我看来,你应该相应地训练你的机器人 . 我的意思是从一个答案开始,然后将后续的对话添加到训练数据 .
    例如,训练数据应该是这样的 .

    data = [
        "Tony",        #I started with an answer
        "that's a good name",
        "thank you",
         "you are welcome"]
    

    然后用一个像print这样的静态语句开始你的对话(“chatBot:hai,你叫什么名字?”)我在下面添加了示例代码片段 .

    data = [
         "Tony",
         "that's a good name",
        "thank you",
         "you are welcome"]
    bot.train(data)
    
    print("chatbot: what is your name?")
    message = input("you: ")
    
    while True:
        if message.strip() != 'Bye'.lower():
            reply = bot.get_response(message)
            print('ChatBot:',reply)
        if message.strip() == 'Bye'.lower():
            print('ChatBot: Bye')
            break
        message = input("you: ")
    

    来自docs
    对于培训过程,您需要传递一个语句列表,其中每个语句的顺序基于其在给定对话中的位置 .
    例如,如果您要运行以下培训调用的机器人,那么生成的聊天机器人将通过说“Hello”来响应“Hi there!”和“Greetings!”这两个语句 .

    from chatterbot.trainers import ListTrainer
    
    chatterbot = ChatBot("Training Example")
    chatterbot.set_trainer(ListTrainer)
    
    chatterbot.train([
        "Hi there!",
        "Hello",
    ])
    
    chatterbot.train([
        "Greetings!",
        "Hello",
    ])
    

    这意味着你的问题和答案的顺序很重要 . 当你训练两个句子时,机器人将第一个作为问题,第二个作为答案 .

相关问题