我有一个剪辑规则,它匹配模板事实中给定的路径,如果路径匹配,则将id和与该路径相关联的文本断言到另一个模板中。路径只是字典中的一个文本条目。路径为"//Document/Sect2/P2“。我想制定一条规则,如下所示:
Pfad "//Document/Sect[*]/P[*]"因此它可以与//Document/Sectany number here/Pany number here匹配。我找不到任何与此相关的东西,所以这是否可能,或者是否有其他选择?任何帮助都将不胜感激。谢谢!以下是我的规则代码:
rule3= """
(defrule createpara
(ROW (counter ?A)
(ID ?id)
(Text ?text)
(Path "//Document/Sect/P"))
=>
(assert (WordPR (counter ?A)
(structure ?id)
(tag "PAR")
(style "Paragraph")
(text ?text))))
"""发布于 2021-10-25 19:36:46
CLIPS不支持正则表达式,但是可以通过define_function方法自己添加对正则表达式的支持。
import re
import clips
RULE = """
(defrule example-regex-test
; An example rule using the Python function within a test
(path ?path)
; You need to double escape (\\\\) special characters such as []
(test (regex-match "//Document/Sect\\\\[[0-9]\\\\]/P\\\\[[0-9]\\\\]" ?path))
=>
(printout t "Path " ?path " matches the regular expression." crlf))
"""
def regex_match(pattern: str, string: str) -> bool:
"""Match pattern against string returning a boolean True/False."""
match = re.match(pattern, string)
return match is not None
env = clips.Environment()
env.define_function(regex_match, name='regex-match')
env.build(RULE)
env.assert_string('(path "//Document/Sect[2]/P[2]")')
env.run()$ python3 test.py
Path //Document/Sect[2]/P[2] matches the regular expression.https://stackoverflow.com/questions/69705456
复制相似问题