How to parse a sentence using grammars provided by nltk?

S

Sohail

Hi,

When I define my own production rules for the grammar the code below
runs fine. Can anyone tell me how to use the built in grammars of nltk
(if there are any)?
.... S -> NP VP
.... PP -> P NP
.... NP -> Det N | Det N PP | 'I'
.... VP -> V NP | VP PP
.... Det -> 'an' | 'my'
.... N -> 'elephant' | 'pajamas'
.... V -> 'shot'
.... P -> 'in'
.... """)
sent = ['I', 'shot', 'an', 'elephant', 'in', 'my', 'pajamas']
parser = nltk.ChartParser(groucho_grammar)
trees = parser.nbest_parse(sent)
for tree in trees:
.... print tree

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top