开发者

ParseException: Expected end of text

I am trying to parse text using pyparsing. My function is shown below. Firstly, I construct a list containing all the terms in my dictionary (a dictionary of commonly used terms in my website). Then I set my grammar to be this list of commonly used words. Then I 开发者_JS百科construct the ZeroOrMore object with the grammar. Finally, I parse the string and I should get the matches found in my string. However, it throws a ParseException instead complaining that end of text was expected.

def map_dict_words(self, pbody):
        dict_terms = [term.term for term in Dictionary.objects()]
        look_for_these = oneOf(dict_terms, caseless=True).setResultsName("dict_words")
        parseobj = ZeroOrMore(look_for_these)
        matches = parseobj.parseString(pbody, parseAll=True)
        print matches

According to the FAQ in pyparsing's homepage http://pyparsing-public.wikispaces.com/FAQs if I want the parser to parse the entire string I should either put StringEnd() in my grammar or use the optional arg parseAll=True. If I remove parseAll=True from my code it works but it doesn't parse the entire string.

Any ideas?


Instead of parseString, you may be more interested in using scanString or searchString. Unlike parseString, these functions skim through the input looking for matches, instead of requiring a complete match of all the content in the input string. scanString returns a generator, so for large input text, will give you matches as they are found:

for toks,start,end in look_for_these.scanString(pbody):
    print toks[0], start, end

searchString is just a simple wrapper around scanString (drops start and end locations, though):

for t in look_for_these.searchString(pbody):
    print t[0]


Think of pyparsing as a more advanced regular expression. When you pass it parseAll=True, it expects to match the entire string, qualifying each and every byte to some part of the grammar. Your grammar however only mentions some of the words that will appear in the string. You have to account for the rest of them somehow.

In other words, assuming that popular words are "parrot", "hovercraft", "eels" and "fjords", you have built an equivalent of the following regular expression:

/^(?P<dict_words>eels|fjords|hovercraft|parrot)*$/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜