[Tutor] Pyparsing question: marking matches in Pyparsing

spir denis.spir at free.fr
Fri Apr 24 23:39:13 CEST 2009


Le Fri, 24 Apr 2009 16:20:02 -0400,
Emad Nawfal (عماد نوفل) <emadnawfal at gmail.com> s'exprima ainsi:

> Hello Tutors,
> I've used Pyparsing to write a Noun Phrase Extractor that extracts noun
> phrases from a Part-of-Speech tagged file. My question is: how can I mark,
> instead of extract, the phrases. For example, In the sentence:
> 
> The DET  big  ADJ woman NOUN saw VERB and CONJ greeted  VERB the DET green
> ADJ  man NOUN
> 
> both  "The big woman" and "the green man" are noun phrases.
>  I need the results to look like this:
> 
> <NP> The big woman </NP> saw and greeted  <NP>the green man </NP>
[...]

Paul MacGuire may have a better solution, but I would use what in pyparsing is called (I guess) a post-parse action. I don't remember the exact syntax, sorry, anyway the method is simply to define a func that will transform the parse result (node) produced by a pattern, then set it on the pattern. 
For instance:

NOUN_PHRASE = <your format here>
def markNounPhrase(result):
   <define new result value>
NOUN_PHRASE.setParseAction(markNounPhrase)

!!! Not sure of the syntax. !!!
Beware of the internal structure of the result (possibly the actual value you need to catch is one or more levels nested in the result produced by the pattern). You'd better print it before writing the func.

Denis
------
la vita e estrany


More information about the Tutor mailing list