CFG in NLTK

Download the file with a sample grammar. Go to the shall in the directory where you have saved the file, and give the following command to import the grammar:

import nltk
grammar = nltk.data.load('file:sample_grammar.cfg')


To parse the sentence "John saw Mary" use the following commands if you want to use the top-down vs. the bottom up parser, respectively:

TOP-DOWN:

sentence = "John left"
tokens = sentence.split()
rd_parser = nltk.RecursiveDescentParser(grammar, trace=2)
trees = rd_parser.parse(tokens)




BOTTOM-UP

sentence = "John left"
tokens = sentence.split()
sr_parser = nltk.ShiftReduceParser(grammar, trace=2)
trees = sr_parser.parse(tokens)



Observe the behaviour of the parser by giving the command:


for tree in trees:
        print(tree)


Exercise 1: Extend the grammar so to recognize as grammatical the sentences below, and check their grammaticality using the top-down parsers.

  1. Jonh left
  2. Jonh knows Mary
  3. They know Mary
  4. They know her
  5. She knows them
  6. Everybody likes Boston
  7. A student likes Boston
  8. John read a book
  9. John gave Mary a red shirt
  10. A student read a book

Check whether your grammar overgenerates by giving an example of ungrammatical sentence that it recognizes as grammatical.

Solution: grammar exercise 1

Exercise 2 Extend the grammar so that it can recognize the following sentences.

  1. John saw a man with a telescope
  2. The woman saw a man on Thursday
  3. Mary knows old men and women
  4. Mary introduced John to Carlos

Check if the top-down parser terminates. If not, try to think of which is the problem.

Check the bottom-up parser if it does not recognize some of the sentences as grammatical, try to think of which is the reason of its failure.

Solution: Grammar exercise 2

Exercise 3: Extend the grammar so to recognize as grammatical the following phrases as NP.

  1. the flight to Boston
  2. the nice flight to Boston
  3. the flight to Boston from Miami
  4. the flight to Boston from Miami in February
  5. the flight to Boston from Miami in February on Friday

Exercise 4: Write a CFG for your native language and use either the top-down or the bottom-up parser to check the grammatility of some sentences.


Last modified: Mon Oct 9 15:23:21 CEST 2017