2 回答
TA贡献1860条经验 获得超9个赞
我有类似的答案给你。你也可以试试这个。您不需要计数器和退出语句。您可以将 while 语句本身定义为看门人。
我做了一些更多的改进。虽然这不会给你一个完美的聊天机器人,但它更接近了。
question = []
answer = []
q = input("Ask me anything: ")
while q.lower() != 'stop':
i = -1
z = q.lower().split()
z.sort()
for x in question:
y = x.split()
y.sort()
if all(elem in y for elem in z):
i = question.index(x)
if i >= 0:
print(answer[i])
else:
question.append(q.lower())
a = input("How should i answer that? ")
answer.append(a)
q = input("Ask me anything: ")
输出:
Ask me anything: What is your Name
How should i answer that? Joe
Ask me anything: What your name
Joe
Ask me anything: name
Joe
Ask me anything: your name
Joe
Ask me anything: what name
Joe
Ask me anything: what is name
Joe
如您所见,当您询问“姓名是什么”时,它仍然假定您是在询问您的姓名。您需要使用它来获得更复杂的机器人。希望这可以帮助您朝着正确的方向前进。
我之前的回答也贴在这里。由于我们将字符串与列表进行比较,因此它必须完全匹配。检查有问题的 q 并不能真正给你带来优势。您将需要拆分单词并进行比较。这就是我在新回复中所做的(见上文)
question = []
answer = []
q = input("Ask me anything: ")
while q.lower() != 'stop':
if q.lower() in question:
i = question.index(q.lower())
print (answer[i])
else:
question.append(q.lower())
a = input("How should i answer that? ")
answer.append(a)
q = input("Ask me anything: ")
TA贡献1830条经验 获得超9个赞
使模糊查找器通过替换为此if q == question[i]不if q in question[i]查找特定单词但查找关键字来执行此操作
question = []
answer = []
qcount = 0
stop = 0
b = 0
while stop == 0:
b = 0
q = input("Ask me anything: ")
if q == "stop":
exit()
for i in range(qcount):
if q in question[i]: # HERE IS THE ANSWER
b = 1
print(answer[i])
if b == 0:
question.append(q)
qcount = qcount + 1
a = input("How should i answer that? ")
answer.append(a)
添加回答
举报