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

时间:2017-09-08 07:08:28

标签: python nltk chatbot

我正在研究一个有两种情况的聊天机器人。

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())

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

1 个答案:

答案 0 :(得分:1)

if __name__ == "__main__":
    demo()
else:
    response()

您没有从此响应函数调用中传递问题文本。只需将此问题作为参数传递给此响应函数调用。