我目前正在python中创建我的第一个项目,构建一个聊天机器人,可以从问题中学习和训练自己。我该怎么做呢?
以下是我一直使用的示例代码。
import random
greetings = ['helo','heyhey','yooyoo', 'hello', 'hi', 'Hi', 'hey!','hey']
random_greeting = random.choice(greetings)
question = ['How are you?','How are you doing?','you good?','hows it going','you alright','you okay']
responses = ['Okay you?', "I'm fine wbu", 'not good rough day...', 'good what about you', 'tired :(, yourself?']
random_response = random.choice(responses)
while True:
userInput = input(">>> ")
if userInput in greetings:
print(random_greeting)
elif userInput in question:
print(random_response)
else:
print("I did not understand what you said")
答案 0 :(得分:3)
一个简单的算法是,当它没有问题的数据库“回答”时,它可以将问题回复给用户,然后将用户的答案添加到数据库中。