Exercise 1


In [1]:
import nltk, re, pprint
from nltk import load_parser
grammar8 = load_parser('file:chapter_9_ex1_a.fcfg')
sent = "I am happy".split()
trees = grammar8.parse(sent)
for tree in trees:
    print tree


(S[]
  (NP_SG_1[] (Pro_SG_1[] I))
  (VP_SG_1[] (Cop_SG_1[] am) (Adj[] happy)))

In [2]:
sent = "she is happy".split()
trees = grammar8.parse(sent)
for tree in trees:
    print tree


(S[]
  (NP_SG_3[] (Pro_SG_3[] she))
  (VP_SG_3[] (Cop_SG_3[] is) (Adj[] happy)))

In [3]:
sent = "you is happy".split()
trees = grammar8.parse(sent)
for tree in trees:
    print tree

In [4]:
sent = "they am happy".split()
trees = grammar8.parse(sent)
for tree in trees:
    print tree

In [5]:
grammar20 = load_parser('file:chapter_9_ex1_b.fcfg')
sent = "I am happy".split()
trees = grammar20.parse(sent)
for tree in trees:
    print tree


(S[]
  (NP[AGR=[NUM='sg', PER=1]] (Pro[AGR=[NUM='sg', PER=1]] I))
  (VP[AGR=[NUM='sg', PER=1], TENSE='pres']
    (Cop[AGR=[NUM='sg', PER=1], TENSE='pres'] am)
    (Adj[] happy)))

In [6]:
sent = "she is happy".split()
trees = grammar20.parse(sent)
for tree in trees:
    print tree


(S[]
  (NP[AGR=[NUM='sg', PER=3]] (Pro[AGR=[NUM='sg', PER=3]] she))
  (VP[AGR=[NUM='sg', PER=3], TENSE='pres']
    (Cop[AGR=[NUM='sg', PER=3], TENSE='pres'] is)
    (Adj[] happy)))

In [7]:
sent = "you is happy".split()
trees = grammar20.parse(sent)
for tree in trees:
    print tree

In [8]:
sent = "they am happy".split()
trees = grammar20.parse(sent)
for tree in trees:
    print tree

Exercise 2


In [9]:
grammar = load_parser('file:chapter_9_ex2.fcfg')
sent = "the boy sings".split()
trees = grammar.parse(sent)
for tree in trees:
    print tree


(S[]
  (NP[NUM='sg'] (Det[] the) (N[NUM='sg', +count] boy))
  (VP[NUM='sg', TENSE='pres'] (IV[NUM='sg', TENSE='pres'] sings)))

In [10]:
sent = "boy sings".split()
trees = grammar.parse(sent)
for tree in trees:
    print tree

In [11]:
sent = "the boys sing".split()
trees = grammar.parse(sent)
for tree in trees:
    print tree


(S[]
  (NP[NUM='pl'] (Det[] the) (N[NUM='pl', +count] boys))
  (VP[NUM='pl', TENSE='pres'] (IV[NUM='pl', TENSE='pres'] sing)))

In [12]:
sent = "boys sing".split()
trees = grammar.parse(sent)
for tree in trees:
    print tree


(S[]
  (NP[NUM='pl'] (N[NUM='pl', +count] boys))
  (VP[NUM='pl', TENSE='pres'] (IV[NUM='pl', TENSE='pres'] sing)))

In [13]:
sent = "the water is precious".split()
trees = grammar.parse(sent)
for tree in trees:
    print tree


(S[]
  (NP[NUM='sg'] (Det[] the) (N[NUM='sg', -count] water))
  (VP[NUM='sg', TENSE='pres']
    (Cop[NUM='sg', TENSE='pres'] is)
    (Adj[] precious)))

In [14]:
sent = "water is precious".split()
trees = grammar.parse(sent)
for tree in trees:
    print tree


(S[]
  (NP[NUM='sg'] (N[NUM='sg', -count] water))
  (VP[NUM='sg', TENSE='pres']
    (Cop[NUM='sg', TENSE='pres'] is)
    (Adj[] precious)))

Exercise 3


In [15]:
def subsumes(f1, f2):
    if len(f2.keys()) > len(f1.keys()):
        return False;
    for key in f2.keys():
        if (key not in f1.keys()) or (f1[key] != f2[key]):
            return False;
    return True

fs1 = nltk.FeatStruct(A='a')
fs2 = nltk.FeatStruct(A='a', B='b')
print subsumes(fs1, fs2)
print subsumes(fs2, fs1)


False
True

Exercise 4


In [16]:
# don't know what is expected here :/ see chapter_9_ex4.fcfg

Exercise 5


In [17]:
grammar = load_parser('file:chapter_9_ex5.fcfg')
sent = "ich sehe die Hunde".split()
trees = grammar.parse(sent)
for tree in trees:
    print tree


(S[]
  (NP[AGR=[NUM='sg', PER=1], CASE='nom']
    (PRO[AGR=[NUM='sg', PER=1], CASE='nom'] ich))
  (VP[AGR=[NUM='sg', PER=1]]
    (V[AGR=[NUM='sg', PER=1], OBJCASE='acc', SUBCAT='trans'] sehe)
    (NP[AGR=[GND='masc', NUM='pl', PER=3], CASE='acc']
      (Det[AGR=[NUM='pl', PER=3], CASE='acc'] die)
      (N[AGR=[GND='masc', NUM='pl', PER=3], CASE='acc'] Hunde))))

In [18]:
sent = "wir kommen".split()
trees = grammar.parse(sent)
for tree in trees:
    print tree


(S[]
  (NP[AGR=[NUM='pl', PER=1], CASE='nom']
    (PRO[AGR=[NUM='pl', PER=1], CASE='nom'] wir))
  (VP[AGR=[NUM='pl', PER=1]]
    (V[AGR=[NUM='pl', PER=1], SUBCAT='intrans'] kommen)))

In [19]:
sent = "ich komme die Hunde".split()
trees = grammar.parse(sent)
for tree in trees:
    print tree

Exercise 6


In [20]:
grammar = load_parser('file:chapter_9_ex6.fcfg')

def parse(sent, grammer):
    sent = sent.split()
    trees = grammar.parse(sent)
    for tree in trees:
        print tree

parse("un cuadro hermoso", grammar)


(NP[]
  (INDEF[AGR=[GND='masc', NUM='sg']] un)
  (NP[AGR=[GND='masc', NUM='sg']]
    (N[AGR=[GND='masc', NUM='sg']] cuadro)
    (ADJ[AGR=[GND='masc', NUM='sg']] hermoso)))

In [21]:
parse("unos cuadros hermosos", grammar)


(NP[]
  (INDEF[AGR=[GND='masc', NUM='pl']] unos)
  (NP[AGR=[GND='masc', NUM='pl']]
    (N[AGR=[GND='masc', NUM='pl']] cuadros)
    (ADJ[AGR=[GND='masc', NUM='pl']] hermosos)))

In [23]:
parse("una cortina hermosa", grammar)


(NP[]
  (INDEF[AGR=[GND='fem', NUM='sg']] una)
  (NP[AGR=[GND='fem', NUM='sg']]
    (N[AGR=[GND='fem', NUM='sg']] cortina)
    (ADJ[AGR=[GND='fem', NUM='sg']] hermosa)))

In [24]:
parse("unas cortinas hermosas", grammar)


(NP[]
  (INDEF[AGR=[GND='fem', NUM='pl']] unas)
  (NP[AGR=[GND='fem', NUM='pl']]
    (N[AGR=[GND='fem', NUM='pl']] cortinas)
    (ADJ[AGR=[GND='fem', NUM='pl']] hermosas)))

In [25]:
parse("un cuadro hermosas", grammar)

In [27]:
parse("unos cortinas hermosas", grammar)

In [ ]: