我想用python中的字典中的clipspy来添加事实(Dict to fact)。但到目前为止,我无法做到这一点。我得到了语法错误,因为我是剪辑,规则和事实编码的初学者。如果有人能帮我解决这个问题,我要提前感谢你。以下是我的代码:
import clips
template_string = """
(deftemplate person
(slot name (type STRING))
(slot surname (type STRING)))
"""
Dict = {'name': 'John', 'surname': 'Doe' }
env = clips.Environment()
env.build(template_string)
template = env.find_template('person')
parstr = """(name%(name))(surname%(surname))"""%Dict
fact = template.assert_fact(parstr)
assert_fact = fact
env.run()
for fact in env.facts():
print(fact)这是我遇到的错误:
Traceback (most recent call last):
File "/home/aqsa/Clips/example2.py", line 13, in <module>
parstr = """(name%(name))(surname%(surname))"""%Dict
ValueError: unsupported format character ')' (0x29) at index 12发布于 2021-09-13 08:26:07
您将一个事实断言为一个字符串,但是模板assert_fact根据documentation和examples需要一个关键字参数列表。
template.assert_fact(name='John', surname='Doe')或
template.assert_fact(**Dict) # kwargs expansion您也可以将事实断言为字符串,但由于引擎必须解释它们,因此速度会稍慢一些。
env.assert_string('(person (name "John") (surname "Doe"))')https://stackoverflow.com/questions/69146864
复制相似问题