I try to parse some suggestions Traffic Violations using pyparsing when I use grammar.searchString(sentence), this is normal, but when I use it gets parseStringcalled ParseException. Can someone help me please say what is wrong with my code?
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
Bug tracking
Traceback (last last call): 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)