User Tools

Site Tools


public:t-malv-15-3:6

Lab 6

Try to complete as many problems as you can. Hand in a python code file (fullName_lab6.py) with what you have finished in MySchool before midnight today (24 September).

If you can't manage to complete a particular problem please hand in your incomplete code – comment it out if it produces an error.

1. Parse CFG

Lets start with the CFG grammar from section 3.3 in chapter 8 and expand upon it.

grammar = nltk.CFG.fromstring("""
  S  -> NP VP
  NP -> Det Nom | PropN
  Nom -> Adj Nom | N
  VP -> V Adj | V NP | V S | V NP PP
  PP -> P NP
  PropN -> 'Buster' | 'Chatterer' | 'Joe'
  Det -> 'the' | 'a'
  N -> 'bear' | 'squirrel' | 'tree' | 'fish' | 'log'
  Adj  -> 'angry' | 'frightened' |  'little' | 'tall'
  V ->  'chased'  | 'saw' | 'said' | 'thought' | 'was' | 'put'
  P -> 'on'
  """)
 
parser = nltk.ChartParser(grammar)
sent = 'Joe saw a squirrel'.split()
for tree in parser.parse(sent):
    print(tree)
    #tree.draw()

1.1 Understand and Expand

Play around with the grammar and try to understand the production rules. Add some new words (terminals) to the productions and make up some sentences to parse.

You can use the draw method to render parse-trees.

1.2 Add Personal Pronouns

Add some personal pronouns to the grammar.

Examples: 'I saw a bear', 'he frightened me'

1.3 Transitive vs. Intransitive Verbs

Add some verbs and alter the grammar to make it differentiate between transitive and intransitive verbs.

Examples: 'the bear died', 'Joe saw the bear was dead', 'Buster gave Joe a fish'

1.4 Add Number Agreement

Alter the grammar to add number agreement, start with something like S → NPsg VPsg | NPpl VPpl and continue down the productions. The terminals could for example be something like: Nsg → 'bear' | …, Vtsg → 'eat' | 'eats' | …. Add plural forms of nouns.

Examples: 'a fish dies', 'the bears frightened a little squirrel'

What to hand in: Your CFG grammar with all the modifications. Code to print out a couple of sentence parses for each subsection here above.

2. Parse PCFG

Now create a probabilistic version of your grammar (see section 3.6 in chapter 8) by adding made up, but reasonable probabilities to your productions.

pgrammar = nltk.PCFG.fromstring("""
  S  -> NPsg VPsg [0.5] | NPpl VPpl [0.5]
  ...
  """)
 
viterbi_parser = nltk.ViterbiParser(pgrammar)
for tree in viterbi_parser.parse(['Buster', 'saw', 'a', 'bear']):
    print(tree)

Try to find ambiguous sentences to parse with the grammar. Then alter the production probabilities to change which is the most probable parsing.

What to hand in: A PCFG grammar. Code to print out a couple of sentence parses.

/var/www/cadia.ru.is/wiki/data/pages/public/t-malv-15-3/6.txt · Last modified: 2024/04/29 13:33 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki