所以我正在制作一个迷你谈话机器人,如果我问什么,除了你好,它回答"我很好"我不是最好的python所以我可能只是缺少一些东西.
码:
print("Hello, I am Squeep Bot, my current purpose is to have meaningless conversations with you, please speak to me formally, I can not understand other ways of saying things") while True: userInput = input(">>> ") if userInput in ["hi", "HI", "Hi", "hello", "HELLO", "Hello"]: print("Hello") elif ("how", "HOW", "How" in userInput)and("are", "ARE", "Are" in userInput)and("you", "YOU", "You" in userInput): print ("I'm Ok") elif ("whats", "WHATS", "Whats", "what's", "WHAT'S", "What's" in userInput)and("favorite", "FAVORITE", "Favorite" in userInput)and("colour", "COLOUR", "Colour" in userInput): print ("I like Green") elif ("what", "WHAT", "What" in userInput)and("is", "IS", "Is" in userInput)and("favorite", "FAVORITE", "Favorite" in userInput)and("colour", "COLOUR", "Colour" in userInput): print ("I like Green") else: print("I did not understand what you said")
编译:
Hello, I am Squeep Bot, my current purpose is to have meaningless conversations with you, please speak to me formally, I can not understand other ways of saying things >>> hi Hello >>> how are you I'm Ok >>> whats your favorite colour I'm Ok >>> fafasfdf I'm Ok >>>
Jean-Françoi.. 6
("how", "HOW", "How" in userInput)
没有做你认为它做的事情.
我只创建一个tuple
有3个值:
("how", "HOW", False)
(或True
)但元组是"真实的",它总是进入第一个if
.
您可以展开使用or
,但在这种情况下,最好的方法是:
if "how" in userInput.lower() ...
所以所有的外壳都要处理.
要处理多场比赛,最好的方法是all
实际使用:
ui_lower = userInput.lower() # only perform `lower` once if all(x in ui_lower for x in ("how","are","you")):
将返回True
如果所有子都userInput
(小写).
由于您似乎无法使其适应您的代码,因此这是一个没有交互式输入的简化版本:
def analyse(user_input): ui_lower = user_input.lower() if ui_lower in ["hi", "hello"]: r = "Hello" elif all(x in ui_lower for x in ("how","are","you")): r = "I'm Ok" elif all(x in ui_lower for x in ("what","favorite","color")): r = "I like Green" else: r = "I'm unable to comply" return r for s in ("what's your favorite color","show you are strong","hello","Drop dead"): print("Q: {}".format(s)) print("A: {}".format(analyse(s)))
输出:
Q: what's your favorite color A: I like Green Q: How are you today? A: I'm Ok Q: hello A: Hello Q: Drop dead A: I'm unable to comply
请注意,代码有其缺陷:它找到子字符串,因此show you are strong
匹配就像How are you today
因为它找到子字符串,即使show
不是how
,并且顺序不同.
对于"严肃"的句子分析,我建议您查看具有python接口的nltk(自然语言工具包)库.
("how", "HOW", "How" in userInput)
没有做你认为它做的事情.
我只创建一个tuple
有3个值:
("how", "HOW", False)
(或True
)但元组是"真实的",它总是进入第一个if
.
您可以展开使用or
,但在这种情况下,最好的方法是:
if "how" in userInput.lower() ...
所以所有的外壳都要处理.
要处理多场比赛,最好的方法是all
实际使用:
ui_lower = userInput.lower() # only perform `lower` once if all(x in ui_lower for x in ("how","are","you")):
将返回True
如果所有子都userInput
(小写).
由于您似乎无法使其适应您的代码,因此这是一个没有交互式输入的简化版本:
def analyse(user_input): ui_lower = user_input.lower() if ui_lower in ["hi", "hello"]: r = "Hello" elif all(x in ui_lower for x in ("how","are","you")): r = "I'm Ok" elif all(x in ui_lower for x in ("what","favorite","color")): r = "I like Green" else: r = "I'm unable to comply" return r for s in ("what's your favorite color","show you are strong","hello","Drop dead"): print("Q: {}".format(s)) print("A: {}".format(analyse(s)))
输出:
Q: what's your favorite color A: I like Green Q: How are you today? A: I'm Ok Q: hello A: Hello Q: Drop dead A: I'm unable to comply
请注意,代码有其缺陷:它找到子字符串,因此show you are strong
匹配就像How are you today
因为它找到子字符串,即使show
不是how
,并且顺序不同.
对于"严肃"的句子分析,我建议您查看具有python接口的nltk(自然语言工具包)库.