pyparsing.ParseException 使用 parseString 时(searchString 有效)

pyparsing.ParseException when using parseString (searchString works)

我正在尝试使用 pyparsing 解析一些交通违规句子,当我使用 grammar.searchString(sentence) 时没问题,但是当我使用 parseString 时抛出 ParseException。谁能帮我说说我的代码有什么问题吗?

from pyparsing import Or, Literal, oneOf, OneOrMore, nums, alphas, Regex, Word, \
    SkipTo, LineEnd, originalTextFor, Optional, ZeroOrMore, Keyword, Group
import pyparsing as pp

from nltk.tag import pos_tag

sentences = ['Failure to control vehicle speed on highway to avoid collision','Failure to stop at stop sign', 'Introducing additives into special fuel by unauthorized person and contrary to regulations', 'driver fail to stop at yield sign at nearest pointf approaching traffic view when req. for safety', 'Operating unregistered motor vehicle on highway', 'Exceeding maximum speed: 39 MPH in a posted 30 MPH zone']


for sentence in sentences:
    words = pos_tag(sentence.split())
    #print words
    verbs = [word for word, pos in words if pos in ['VB','VBD','VBG']]
    nouns = [word for word, pos in words if pos == 'NN']
    adjectives = [word for word, pos in words if pos == 'JJ']

    adjectives.append('great')  # initializing  
    verbs.append('get') # initializing 


    object_generator = oneOf('for to')
    location_generator = oneOf('at in into on onto over within')
    speed_generator = oneOf('MPH KM/H')

    noun = oneOf(nouns)
    adjective = oneOf(adjectives)

    location = location_generator + pp.Group(Optional(adjective) + noun)

    action = oneOf(verbs)
    speed = Word(nums) + speed_generator

    grammar =  action | location | speed

    parsed = grammar.parseString(sentence)

    print parsed

错误回溯

Traceback (most recent call last): File "script3.py", line 35, in parsed = grammar.parseString(sentence) File "/Users/alana/anaconda/lib/python2.7/site-packages/pyparsing‌​.py", line 1032, in parseString raise exc pyparsing.ParseException: Expected Re:('control|avoid|get') (at char 0), (line:1, col:1)

searchString 有效,因为它会跳过与语法不完全匹配的文本。 parseString 更为特殊,需要完整的语法匹配,从输入字符串的第一个字符开始。在您的情况下,语法有点难以确定,因为它是根据输入句子的 NLTK 分析自动生成的(顺便说一句,这是一种有趣的方法)。如果您只是打印语法本身,它 可能 让您深入了解它正在寻找的字符串。例如,我猜 NLTK 会将您的第一个示例中的 'Failure' 解释为名词,但语法中的 3 个表达式中的 none 以名词开头 - 因此, parseString 将失败.

您可能需要根据 NLTK 发现的内容对名词、形容词和动词列表进行更多的内部打印,然后查看它如何映射到您生成的语法。

您还可以尝试使用 Python 的 sum() 内置函数将句子中的多个匹配结果组合起来:

grammar =  action("action") | Group(location)("location") | Group(speed)("speed")

#parsed = grammar.parseString(sentence)
parsed = sum(grammar.searchString(sentence))
print(parsed.dump())