How to parse a sentence using grammars provided by nltk?

Discussion in 'Python' started by Sohail, Aug 12, 2010.

  1. Sohail

    Sohail Guest

    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)?

    >>> groucho_grammar = nltk.parse_cfg("""

    .... 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
    Sohail, Aug 12, 2010
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    350
    Douglas Alan
    Mar 8, 2006
  2. Jon Slaughter

    Grammars for C++

    Jon Slaughter, Jul 5, 2005, in forum: C++
    Replies:
    6
    Views:
    446
  3. Passer By

    NLTK, Random Sentence Generators?

    Passer By, Apr 10, 2007, in forum: Python
    Replies:
    8
    Views:
    1,945
    Oleg Alexander
    Apr 12, 2007
  4. Michael Judge

    Grammars (mini-scripting languages)

    Michael Judge, Feb 2, 2006, in forum: Ruby
    Replies:
    5
    Views:
    157
    Harley Pebley
    Feb 6, 2006
  5. Phil Tomson
    Replies:
    3
    Views:
    100
    Clifford Heath
    Feb 29, 2008
Loading...

Share This Page