如何将Chat bot问题及其答案添加到pandas数据框,以便在以后问相同问题时可以检索到它

时间:2019-03-15 20:41:56

标签: python-3.x pandas python-requests chatbot

基本的Python聊天机器人

  1. 该任务的目标是创建一个基本的Python聊天机器人,其中如果任何问题询问聊天机器人是否不知道答案,它将显示要求用户提供答案的信息。收到答案后,应将其问题及其答案写入熊猫数据框。将来会提出类似的问题,聊天机器人应调查熊猫数据框并给出答案。
    1. 创建熊猫数据框的目的是,目前我没有任何现成的问题和答案,因此随着时间的推移,我将向熊猫数据框逐一添加问题和答案。

Blockquote

username = "User"
chatbotname = "<>"
chatbotnameknown = False
active = True

def saychatbot(text):
    global username
    global chatbotname
    global chatbotnameknown
    global active
    if chatbotnameknown:
        print(chatbotname + ": " + text)
    else:
        print("*: " + text)

def speak(user_entry):
    global username
    global chatbotname
    global chatbotnameknown
    global active
    if user_entry == "Hello!" or user_entry == "hello":
        saychatbot("Hi, " + username)
    elif user_entry == "How are you?":
        saychatbot("I'm fine. And you?")
        reply = input("Your answer: ")
        if reply == "Great":
            saychatbot("I'm glad to hear that.")

        else:
                saychatbot("I didn't understand you.")
    elif user_entry == "Bye":
        active = False
    else:
        saychatbot("I didn't understand you.")
        saychatbot("I am still learning, let me learn your language")
        if input("Would you like to teach me your language, Say y/n ? ") == "y":
            saychatbot("You know i am still infancy, so please teach me your language one question and its answer at a time so i will load it in my database!!")

            print("Here I would like to record the question and its answer in Pandas data frame and use that data frame as input to answer the same question in future") 
            print("Is there any way to achieve it") 


def OpenDiscussion():
    global username
    global chatbotname
    global chatbotnameknown
    global active
    print("********Python - Do you know system can speak****************")
    while active:
        if chatbotnameknown:
            speak(str(input(username + ": " + chatbotname + ", ")))
        else:
            speak(str(input(username + ": ")))
    saychatbot("Bye.")
OpenDiscussion()

1 个答案:

答案 0 :(得分:0)

首先,您可以建立一个数据结构来存储<question, answer>数据。在这种情况下,最好使用字典。

如果您需要使用数据框并添加数据,则pandas.DataFrame.append方法会有所帮助。