如何使用Lark正确匹配子字符串?
我的意图(可能/不建议使用 Lark 或任何 CFG)是仅匹配和解析字符串的重要部分,而忽略其余部分。例如:
- 从“约翰”,我想将“约翰”解析为
one_person
. - 从“昨天约翰睡觉”开始,我想将“约翰”解析为
one_person
并忽略其余部分。 - 从“约翰和玛丽”开始,我想将“约翰和玛丽”解析为
two_people
. - 从“昨天约翰和玛丽接吻。”,我想将“约翰和玛丽”解析为
two_people
并忽略其余部分。
这是我的代码:
from lark import Lark, tree
grammar = """
input: not_important* important not_important*
important: one_person
| two_people
one_person: PERSON
two_people: one_person conj one_person
not_important: RANDOM_WORD
conj: CONJ
PERSON: "John" | "Mary"
CONJ: "and" | "or"
RANDOM_WORD: /\\w+/
%import common.WS
%ignore WS
"""
if __name__ == '__main__':
parser = Lark(grammar, start='input', ambiguity='explicit')
tree = parser.parse('Yesterday John and Mary kissed')
print(tree.pretty())
有用:
- 当重要部分周围没有任何内容时,例如“John”或“John and Mary”。
- 或者当重要部分只有一侧有不重要的东西时,例如“约翰睡了”或“约翰和玛丽亲吻了”。
但是当不重要的东西围绕着重要的东西时,它就不起作用了,例如“昨天约翰和玛丽接吻了”。在这个例子中,我希望得到:
input
not_important Yesterday
important
two_people
one_person John
conj and
one_person Mary
not_important kissed
但我得到:
_ambig
input
not_important Yesterday
important
one_person John
not_important and
not_important Mary
not_important kissed
input
not_important Yesterday
important
two_people
one_person John
conj and
one_person Mary
not_important kissed
not_important John
not_important and
也就是说,Lark 不仅将输入视为模棱两可,而且第二次解析也失败了,因为两个终端(“John”和“and”)被消耗了两次。