基本的Python聊天机器人
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()
答案 0 :(得分:0)
首先,您可以建立一个数据结构来存储<question, answer>
数据。在这种情况下,最好使用字典。
如果您需要使用数据框并添加数据,则pandas.DataFrame.append
方法会有所帮助。