0

使用 python 创建了一个聊天机器人,它的工作流程是我在发消息,据此聊天机器人正在回复。但它应该反向完成,这意味着聊天机器人应该首先通过欢迎/提问开始,然后用户会回复。请建议在代码中进行一些转换,以便它可以相应地工作。提前谢谢你。

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

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
4

1 回答 1

0

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

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

然后使用 print("chatBot: hai, what is your name?") 这样的静态语句开始您的对话,我在下面添加了示例代码片段。

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
对于培训过程,您需要传入一个语句列表,其中每个语句的顺序取决于它在给定对话中的位置。
例如,如果您要运行以下训练调用的机器人,那么生成的聊天机器人将响应“您好!”这两个语句。和“你好!” 通过说“你好”。

from chatterbot.trainers import ListTrainer

chatterbot = ChatBot("Training Example")
chatterbot.set_trainer(ListTrainer)

chatterbot.train([
    "Hi there!",
    "Hello",
])

chatterbot.train([
    "Greetings!",
    "Hello",
])

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

于 2018-06-30T06:59:48.010 回答