Python 聊天机器人无法正常工作

问题描述 投票:0回答:2

我正在开发一个有两种场景的聊天机器人。

1: 当用户键入问题时,如果该问题在训练数据集中可用,它会从训练数据集中选择该问题的答案。

2: 如果用户输入的问题在数据集中不可用,系统将根据代码中定义的默认答案给出响应。

我的问题是,当系统得到一个不在训练数据中的问题陈述时,它会从代码中随机选择一个答案(很好)。但从那时起,无论我们问哪个问题,它都会开始给出默认答案。尽管在训练数据中有特定问题及其答案,但它从不从训练数据中选择答案。

整个代码太大,无法粘贴到这里。我只写那些出现问题的函数。希望有人能帮我解决这个问题。

def response(sentence, userID='123', show_details=False):
    results = classify(sentence)
    # if we have a classification then find the matching intent tag
    if results:
        # loop as long as there are matches to process
        while results:
            #some if else statements to match the question
            results.pop(0)
        while not results:
            pairs = (
                (r'I need (.*)',
                 ("Why do you need %1?",
                  "Would it really help you to get %1?",
                  "Are you sure you need %1?")),

                (r'Why don\'t you (.*)',
                 ("Do you really think I don't %1?",
                  "Perhaps eventually I will %1.",
                  "Do you really want me to %1?"))

            )

            aida_chatbot = Chat(pairs, reflections)

            def aida_chat():
                aida_chatbot.converse()

            def demo():
                aida_chat()

            if __name__ == "__main__":
                demo()
            else:
                response()
                # sentence = sys.stdin.readline()
    sys.stdout.write("> ")
    sys.stdout.flush()
    classify(sentence=sys.stdin.readline())
    while True:
        response(sentence=sys.stdin.readline())

当我将 pairs 放在 while 语句之外时(例如,在 if 语句关闭后的 else 语句中),程序永远不会进入 else 语句并且这部分代码永远不会执行。 有人能帮我吗?

python nltk chatbot
2个回答
1
投票
if __name__ == "__main__":
    demo()
else:
    response()

您没有传递来自此响应函数调用的问题文本。只需将您的问题作为此响应函数调用的参数传递即可。


0
投票

我完成了这个社交媒体聊天机器人项目。你可以检查一下。我希望它能帮助你! https://darekdari.com/python-project-social-media-bot/

© www.soinside.com 2019 - 2024. All rights reserved.