我正试图创建一个与uniform的关系,例如:
男孩(迈克(-->迈克是个男孩
女孩(丽莎(-->丽莎是个女孩
is sister(Mike,Lisa(-->Lisa是Mike的妹妹
这是我的代码:
from fact import Fact
from logic import get, unify, var
from itertools import chain
import regex as re
facts = {}
pattern = r"(w+)((?:([^,)]+),(?:([^,)]+)))+).?"
rule = input('Ingrese un hecho o consulta:')
while rule != '!':
match = re.match(pattern, rule)
print(match)
name = match.captures(1)[0]
argument1 = match.captures(2)
argument2 = match.captures(3)
if rule.endswith('.'):
if not name in facts:
facts[name] = Fact()
facts[name].append(argument1[0])
facts[name].append(argument2[0])
else:
X = var()
Y = var()
for _ in facts[name](X,Y): print(get(X),get(Y))
rule = input('Ingrese un hecho o consulta:')
我想要的是,当我问isSister(?,Lisa(时,它会回复Mike。这就是我得到的:
Traceback(最近一次调用最后一次(:文件"main.py",第17行,在name=match.captures(1([0]AttributeError:"NoneType"对象没有属性"captures">
match
在没有匹配的情况下返回None
,这似乎发生在while
循环中的某个地方。你需要以某种方式处理这个案件,要么通过:
if match == None:
# handle
或:
try:
name=match.captures(1)[0]
except AttributeError as e:
# handle
这取决于您选择适合您的解决方案的处理方式,但这是我通常选择的两种模式。我肯定还有其他的,但我建议从这里开始。