import random
import re
rules=[["(.*)hello(.*)",["Hi there. Please state your problem"]],
["(.*)name(.*)",["Great, good to know","I am not interested
in names"]],
["(.*)sorry(.*)",["please don't apologize","Apologies are not necessary","What feelings you have when you apologize?"]],
["(.*)",["Very interesting","I am not sure I understand you fully","Please continue",
"Do you feel strongly about discussing such things?","\\2"]]]
grammar = {
"am": "are",
"was": "were",
"i": "you",
"i'd": "you would",
"i've": "you have",
"i'll": "you will",
"my": "your",
"are": "am",
"you've": "I have",
"you'll": "I will",
"your": "my",
"yours": "mine",
"you": "me",
"me": "you"
}
def correction(word):
character=word.lower().split()
for i, j in enumerate(character):
if j in grammar:
character[i]=grammar[j]
return " ".join(character)
def test(sentence):
for pattern, message in rules:
match=re.match(pattern,sentence.rstrip(".!"))
if match:
response = random.choice(message)
temp = " " + correction(match.group())
response2 = re.sub(r"\\2",temp,response)
return response2
else:
recall=random.choice(message)
return recall
while True:
sentence =input("You: ")
print("JBot: " + test(sentence))
if sentence == "quit":
break
在这个简单的eliza
实现中,有一个名为rule的列表,其中包含一组模式和相应的响应。如果匹配了模式或输入了不属于规则(最后一个规则)的任何其他内容,则该代码应该获得随机响应。
代码现在仅输出,"Hi, there. Please state your problem"
用于所有输入语句。任何帮助,为什么会这样?
如果您输入的规则中匹配的句子,则它将以相应的响应进行回复。假设规则如下:'(.*)
就像(.*)'
,[如果您输入的是{{,您在{0}
和{1}
之间看到什么样的共性?]]] 1}}的回应应该像是,您在猫和狗之间看到什么样的相似之处?因此,它需要从比赛中分组并放置在相应的响应中。
答案 0 :(得分:1)
我已经修复了您的代码,现在应该可以正常工作了:
注释:
else
函数循环中的test
将在每次迭代时退出循环,因此您将无法浏览所有规则(语法)。我将其放在for
之后,这将首先强制检查每个规则,然后再选择默认的随机选择答案。代码:
import random
import re
rules=[["(.*)hello(.*)",["Hi there. Please state your problem"]],
["(.*)name(.*)",["Great, good to know","I am not interested in names"]],
["(.*)sorry(.*)",["please don't apologize","Apologies are not necessary","What feelings you have when you apologize?"]],
["(.*)",["Very interesting","I am not sure I understand you fully","Please continue",
"Do you feel strongly about discussing such things?","\\2"]]]
grammar = {
"am": "are",
"was": "were",
"i": "you",
"i'd": "you would",
"i've": "you have",
"i'll": "you will",
"my": "your",
"are": "am",
"you've": "I have",
"you'll": "I will",
"your": "my",
"yours": "mine",
"you": "me",
"me": "you"
}
def correction(word):
character=word.lower().split()
for i, j in enumerate(character):
if j in grammar:
character[i]=grammar[j]
return " ".join(character)
def test(sentence):
for pattern, message in rules:
match=re.match(pattern,sentence.rstrip(".!"))
if match:
response = random.choice(message)
temp = " " + correction(match.group())
response2 = re.sub(r"\\2",temp,response)
return response2
recall=random.choice(random.choice([r[1] for r in rules]))
return recall
while True:
sentence =input("You: ")
print("JBot: " + test(sentence))
if sentence == "quit":
break
输出:
You: 'hello'
JBot: Hi there. Please state your problem
You: "i don't have a name"
JBot: Great, good to know
You: "i am so sorry"
JBot: What feelings you have when you apologize?
You: "help me"
JBot: Do you feel strongly about discussing such things?
输出是如此有趣,以至于使我成真。