Search code examples
pythonpyparsing

pyparsing: parse a sentence with special words in it


I am trying to write a program with pyparsing that parse all strings contains special words in it. I wrote following code, but it is not working:

from pyparsing import *
word = Word(alphas) 
sentence = OneOrMore(word)
day = Literal("day")
sentence_end_with_happy = sentence + day + sentence 
ret = sentence_end_with_happy.parseString("hi this is a nice day and everything is ok")

I tried to parse a sentence with special word "day" but it has errors while parsing...

pyparsing.ParseException: Expected "day" (at char 42), (line:1, col:43)


Solution

  • Use a negative lookahead when defining word; otherwise, word matches day and sentence will consume it.

    from pyparsing import *
    day = Keyword("day")
    word = ~day + Word(alphas)
    sentence = OneOrMore(word)
    sentence_end_with_happy = sentence('first') + day + sentence('last') 
    ret = sentence_end_with_happy.parseString("hi this is a nice day and everything is ok")
    print ret['first']
    print ret['last']
    print ret
    

    Output:

    ['hi', 'this', 'is', 'a', 'nice']
    ['and', 'everything', 'is', 'ok']
    ['hi', 'this', 'is', 'a', 'nice', 'day', 'and', 'everything', 'is', 'ok']