2
votes

I'm trying to extract prepositional phrases from sentences using NLTK. Is there a way for me to do this automatically (e.g. feed a function a sentence and get back its prepositional phrases)?

The examples here seem to require that you start with a grammar before you can get a parse tree. Can I automatically get the grammar and use that to get the parse tree?

Obviously I could tag a sentence, pick out prepositions and the subsequent noun, but this is complicated when the prepositional complement is compound.

2
Maybe this post will help stackoverflow.com/questions/6115677/… - NLPer

2 Answers

2
votes

What you really want to do is to fully parse your sentence with a robust statistical parser (e.g. like Stanford), and then look for constituents marked with PP:

(ROOT
  (S
    (NP (NNP John))
    (VP (VBZ lives)
      (PP (IN in)
        (NP (DT a) (NN house)))
      (PP (IN by)
        (NP (DT the) (NN sea))))))

I am not sure about the parsing abilities of NLTK and how accurate is the parsing if this feature exists, but it's not much of a problem to call an external parser from Python and then process the output. Using a parser will save you much time and effort (since the parser takes care of everything), and is the only reliable way to do this job.

1
votes

I know the answer was already accepted, but a shallow parser will return the NLP chunks with minimal syntactic structure. This fairly linear result may be easier to work with. Here's an online demo of the CLiPS parser: http://www.clips.ua.ac.be/cgi-bin/webdemo/MBSP-instant-webdemo.cgi

Here's an example:

John gave the book to Mary

CliPS shallow parser result

The [PNP] is easy to extract.