【发布时间】:2018-11-18 14:27:43
【问题描述】:
我正在尝试创建与统一的关系,例如:
Boy(Mike) --> Mike 是个男孩
Girl(Lisa) --> Lisa 是个女孩
isSister(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' object has no attribute 'captures'
【问题讨论】:
标签: python regex dictionary logic-programming unify