我建立了2个语法,它们彼此部分重叠。我希望与所选文本最匹配的语法。因此,我用Or语句编写了第三种语法。我在互联网上进行搜索,了解到^选择返回最长文本的语法,而不是仅选择Or语句中的第一个语法。
运行代码时,它总是选择第一个语法,而不是匹配最佳语法。当我在^语句中翻转两个语法时,也是如此。有人知道为什么会这样吗?
我正在使用Python 2.7.13和PyParsing 2.1.4。
(在第三行的语法type1中可以找到Or语句)
advice1 = (ZeroOrMore(~Literal('Home') + WORD) + Literal('Home')
+ OneOrMore(~Literal('Street') + WORD)('Home').setParseAction(' '.join, standardizeStrings)
+ Literal('Street') + UPPERCASEWORDS('Facility').setParseAction(''.join, standardizeStrings)
+ SkipTo("-----") + restOfLine + EOL #1
+ OneOrMore(~AMOUNT + WORD).setParseAction(' '.join) + AMOUNT + AMOUNT('CPB').setParseAction(convertAmountString) + DATE
+ SkipTo("-----") + restOfLine + EOL #2
+ OneOrMore(~AMOUNT + WORD).setParseAction(' '.join) + AMOUNT + AMOUNT('TOI').setParseAction(convertAmountString) + Optional(DATE)
+ SkipTo("_____") + restOfLine + EOL #3
+ DATE + DATE + Word(nums) + CURRENCY + AMOUNT + RATE + AMOUNT
+ SkipTo("-----") + restOfLine + EOL #4
+ OneOrMore(~AMOUNT + WORD).setParseAction(' '.join) + AMOUNT + AMOUNT('PB').setParseAction(convertAmountString) + DATE('New_Date').setParseAction(convertDateString)
)
advice2 = (ZeroOrMore(~Literal('Home') + WORD) + Literal('Home')
+ OneOrMore(~Literal('Street') + WORD)('Home').setParseAction(' '.join, standardizeStrings)
+ Literal('Street') + UPPERCASEWORDS('Facility').setParseAction(''.join, standardizeStrings)
+ SkipTo("-----") + restOfLine + EOL #1
+ OneOrMore(~AMOUNT + WORD).setParseAction(' '.join) + AMOUNT + AMOUNT('CPB').setParseAction(convertAmountString) + DATE
+ SkipTo("-----") + restOfLine + EOL #2
+ (OneOrMore(~AMOUNT + WORD).setParseAction(' '.join) ^ AMOUNT) + AMOUNT + AMOUNT('TIA').setParseAction(convertAmountString) + Optional(DATE)
+ SkipTo("-----") + restOfLine + EOL #3
+ OneOrMore(~AMOUNT + WORD).setParseAction(' '.join) + AMOUNT + AMOUNT('TOI').setParseAction(convertAmountString) + Optional(DATE)
+ SkipTo("_____") + restOfLine + EOL #4
+ DATE + DATE + Word(nums) + CURRENCY + AMOUNT + RATE + AMOUNT
+ SkipTo("-----") + restOfLine + EOL #5
+ OneOrMore(~AMOUNT + WORD).setParseAction(' '.join) + AMOUNT + AMOUNT('PB').setParseAction(convertAmountString) + DATE('New_Date').setParseAction(convertDateString)
)
type1 = (OneOrMore(~Literal('Effective') + WORD)
+ Literal('Effective') + SEP + DATE('Effective_Date').setParseAction(convertDateString)
+ OneOrMore(Group(advice1 ^ advice2))('Facility') # OR statement
+ Optional(OneOrMore(~Literal("rate is") + WORD) + Literal("rate is") + RATE('Rate').setParseAction(convertRateString))
+ OneOrMore(~DATE + WORD) + DATE('Date').setParseAction(convertDateString)
+ Optional(notifications)
+ ZeroOrMore(~Literal('Home') + WORD)
)