我正在尝试使用一些特殊的规则(例如相邻值和接近值)来创建布尔查询解析器。我到目前为止创建的规则是
## DEFINITIONS OF SYMBOLS ###
NEAR = CaselessLiteral('near').suppress()
NUMBER = Word(nums)
NONEDIRECTIONAL = Combine(NEAR+NUMBER)
ADJ = CaselessLiteral("ADJ").setParseAction(replaceWith('0'))
OAND = CaselessLiteral("and")
OOR = CaselessLiteral("or")
ONOT = CaselessLiteral("not")
## ----------------------- ##
## DEFINITIONS OF TERMS ###
# Do not break quoted string.
QUOTED = quotedString.setParseAction(removeQuotes)
# space-separated words are easiest to define using just OneOrMore
# must use a negative lookahead for and/not/or operators, and this must come
# at the beginning of the expression
WORDWITHSPACE = OneOrMore(~(OAND | ONOT | OOR | NONEDIRECTIONAL | ADJ) +
Word(printables, excludeChars="()"))
# use a parse action to recombine words into a single string
WORDWITHSPACE.addParseAction(lambda t: ' '.join(t))
TERM = (QUOTED | WORDWITHSPACE)
## ----------------------- ##
## DEFINITIONS OF Expresion ###
EXPRESSION = infixNotation(TERM,
[
(ADJ, 2, opAssoc.LEFT),
(NONEDIRECTIONAL, 2, opAssoc.LEFT),
(ONOT, 1, opAssoc.RIGHT),
(Optional(OAND, default='and'), 2, opAssoc.LEFT),
(OOR, 2, opAssoc.LEFT)
])
# As we can have more than one occurances of symbols together we are
# using `OneOrMore` Exprestions
BOOLQUERY = OneOrMore(EXPRESSION) + StringEnd()
## ----------------------- ##
我跑步时
(((a或b)以及(b和c))或(a和d)
效果很好
而我尝试解析
(((((智能ADJ合同*)和协议)或(强制执行Near3而不进行Near3交互))或(自动ADJ托管)或(((协议*或共识ADJ算法)near5(协议和交易)))>
代码卡住了,无法处理。
有人可以帮助我解决我的问题吗?
更新的代码:
EXPRESSION = infixNotation(TERM,
[
(ONOT, 1, opAssoc.RIGHT),
(Optional(OAND, default='and'), 2, opAssoc.LEFT),
((OOR | NONEDIRECTIONAL | ADJ), 2, opAssoc.LEFT)
])
保留可选,并且由于类似
的情况x不是y不是z
答案 0 :(得分:1)
您的程序花了很长时间,因为infixNotation
的深度为5层,并且具有可选的AND运算符。
通过启用packrat解析,我能够按原样运行。为此,请添加到脚本顶部(在导入pyparsing之后):
ParserElement.enablePackrat()
要运行您的测试,我使用了runTests
。对我来说尚不清楚为什么需要BOOLQUERY,因为您只是在解析表达式:
tests = """\
((a or b) and (b and c)) or (a and d)
((((smart ADJ contract*) and agreement) or (enforced near3 without near3 interaction) or (automated ADJ escrow)) or ((protocol* or Consensus ADJ algorithm) near5 (agreement and transaction)))
"""
EXPRESSION.runTests(tests)
礼物:
((a or b) and (b and c)) or (a and d)
[[[['a', 'or', 'b'], 'and', ['b', 'and', 'c']], 'or', ['a', 'and', 'd']]]
[0]:
[[['a', 'or', 'b'], 'and', ['b', 'and', 'c']], 'or', ['a', 'and', 'd']]
[0]:
[['a', 'or', 'b'], 'and', ['b', 'and', 'c']]
[0]:
['a', 'or', 'b']
[1]:
and
[2]:
['b', 'and', 'c']
[1]:
or
[2]:
['a', 'and', 'd']
((((smart ADJ contract*) and agreement) or (enforced near3 without near3 interaction) or (automated ADJ escrow)) or ((protocol* or Consensus ADJ algorithm) near5 (agreement and transaction)))
[[[[['smart', '0', 'contract*'], 'and', 'agreement'], 'or', ['enforced', '3', 'without', '3', 'interaction'], 'or', ['automated', '0', 'escrow']], 'or', [['protocol*', 'or', ['Consensus', '0', 'algorithm']], '5', ['agreement', 'and', 'transaction']]]]
[0]:
[[[['smart', '0', 'contract*'], 'and', 'agreement'], 'or', ['enforced', '3', 'without', '3', 'interaction'], 'or', ['automated', '0', 'escrow']], 'or', [['protocol*', 'or', ['Consensus', '0', 'algorithm']], '5', ['agreement', 'and', 'transaction']]]
[0]:
[[['smart', '0', 'contract*'], 'and', 'agreement'], 'or', ['enforced', '3', 'without', '3', 'interaction'], 'or', ['automated', '0', 'escrow']]
[0]:
[['smart', '0', 'contract*'], 'and', 'agreement']
[0]:
['smart', '0', 'contract*']
[1]:
and
[2]:
agreement
[1]:
or
[2]:
['enforced', '3', 'without', '3', 'interaction']
[3]:
or
[4]:
['automated', '0', 'escrow']
[1]:
or
[2]:
[['protocol*', 'or', ['Consensus', '0', 'algorithm']], '5', ['agreement', 'and', 'transaction']]
[0]:
['protocol*', 'or', ['Consensus', '0', 'algorithm']]
[0]:
protocol*
[1]:
or
[2]:
['Consensus', '0', 'algorithm']
[1]:
5
[2]:
['agreement', 'and', 'transaction']