Exercise 1


In [1]:
import nltk, re, pprint

In [2]:
read_expr = nltk.sem.Expression.fromstring
read_expr('Asi -> -Bsu') # Asi: "Agnus sings", Bsu: "Bertie sulks"


Out[2]:
<ImpExpression (Asi -> -Bsu)>

In [3]:
read_expr('Cr & Cb') # Cr: "Cyril runs", Cb: "Cyril barks"


Out[3]:
<AndExpression (Cr & Cb)>

In [4]:
read_expr('-r -> s') # r: "rain", s: "snow"


Out[4]:
<ImpExpression (-r -> s)>

In [5]:
read_expr('-((Oc | Tc) -> Ih)') # Oc: "Olive comes", Tc: "Tofu comes", Ih: "Irene is happy"


Out[5]:
<NegatedExpression -((Oc | Tc) -> Ih)>

In [6]:
read_expr('-(Pc | Ps)') # Pc: "Pat coughed", Ps: "Pat sneezed"


Out[6]:
<NegatedExpression -(Pc | Ps)>

In [7]:
read_expr('(Ica -> -Yco) -> (Yca -> -Ico)') # Ico: "I come", Yco: "you come", Ica: "I call", Yca: "You call"


Out[7]:
<ImpExpression ((Ica -> -Yco) -> (Yca -> -Ico))>

Exercise 2


In [8]:
read_expr('like(angus,cyril) & hate(irene,cyril)')


Out[8]:
<AndExpression (like(angus,cyril) & hate(irene,cyril))>

In [9]:
read_expr('taller(tofu,bertie)')


Out[9]:
<ApplicationExpression taller(tofu,bertie)>

In [10]:
read_expr('love(bruce,bruce) & love(pat,pat)')
# or
read_expr('love(bruce,bruce) & love(pat,bruce)')


Out[10]:
<AndExpression (love(bruce,bruce) & love(pat,bruce))>

In [11]:
read_expr('see(cyril,bertie) & -see(angus,bertie)')


Out[11]:
<AndExpression (see(cyril,bertie) & -see(angus,bertie))>

In [12]:
read_expr('fourleggedfriend(cyril)')


Out[12]:
<ApplicationExpression fourleggedfriend(cyril)>

In [13]:
read_expr('near(tofu,olive) & near(olive,tofu)')


Out[13]:
<AndExpression (near(tofu,olive) & near(olive,tofu))>

Exercise 3


In [14]:
read_expr('exists x.like(angus,x) & exists y.like(y,julia)')


Out[14]:
<AndExpression (exists x.like(angus,x) & exists y.like(y,julia))>

In [15]:
read_expr('exists x.(dog(x) & love(angus,x) & love(x,angus))')


Out[15]:
<ExistsExpression exists x.(dog(x) & love(angus,x) & love(x,angus))>

In [16]:
read_expr('-exists x.smile(x,pat)')


Out[16]:
<NegatedExpression -exists x.smile(x,pat)>

In [17]:
read_expr('exists x.(cough(x) & sneeze(x))')


Out[17]:
<ExistsExpression exists x.(cough(x) & sneeze(x))>

In [18]:
read_expr('-exists x.(cough(x) | sneeze(x))')


Out[18]:
<NegatedExpression -exists x.(cough(x) | sneeze(x))>

In [19]:
read_expr('exists x.(love(bruce,x) & -equal(x,bruce))')


Out[19]:
<ExistsExpression exists x.(love(bruce,x) & -equal(x,bruce))>

In [20]:
read_expr('-exists x.(love(x,pat) & -equal(x,matthew))')


Out[20]:
<NegatedExpression -exists x.(love(x,pat) & -equal(x,matthew))>

In [21]:
read_expr('all x.(like(cyril,x) & -equal(x,irene))')


Out[21]:
<AllExpression all x.(like(cyril,x) & -equal(x,irene))>

In [22]:
read_expr('exists x.asleep(x) & all y.(-equal(x,y) & -asleep(y))')


Out[22]:
<AndExpression (exists x.asleep(x) & all y.(-equal(x,y) & -asleep(y)))>

Exercise 4


In [23]:
read_expr(r'\x.(feed(x,cyril) & give(x,cappuccino,angus))')


Out[23]:
<LambdaExpression \x.(feed(x,cyril) & give(x,cappuccino,angus))>

In [24]:
read_expr(r'\x.give(pat,warandpeace,x)')


Out[24]:
<LambdaExpression \x.give(pat,warandpeace,x)>

In [25]:
read_expr(r'all y.(\x.love(y,x))')


Out[25]:
<AllExpression all y.\x.love(y,x)>

In [26]:
read_expr(r'all y.(\x.(love(y,x) | detest(y,x)))')


Out[26]:
<AllExpression all y.\x.(love(y,x) | detest(y,x))>

In [27]:
read_expr(r'all y.(\x.(love(y,x) & -detest(y,x)))')


Out[27]:
<AllExpression all y.\x.(love(y,x) & -detest(y,x))>

Exercise 5


In [28]:
read_expr = nltk.sem.Expression.fromstring
e1 = read_expr(r'\x.(exists y.love(x,y))')
e2 = read_expr('pat')
e3 = nltk.sem.ApplicationExpression(e1, e2)
print(e3.simplify())


exists y.love(pat,y)

In [29]:
# Translation: Pat loves someone.

In [30]:
e1 = read_expr(r'\x.(exists y.(love(x,y) | love(y,x)))')
e2 = read_expr('pat')
e3 = nltk.sem.ApplicationExpression(e1, e2)
print(e3.simplify())


exists y.(love(pat,y) | love(y,pat))

In [31]:
# Translation: Pat loves someone or someone loves Pat.

In [32]:
e1 = read_expr(r'\x.(walk(fido))')
e2 = read_expr('pat')
e3 = nltk.sem.ApplicationExpression(e1, e2)
print(e3.simplify())


walk(fido)

Exercise 6


In [33]:
e1 = read_expr(r'\P.\x.all y.(dog(y) -> P(x,pat))')
e2 = read_expr('chase')
e3 = nltk.sem.ApplicationExpression(e1, e2)
print(e3.simplify()) # \x.all y.(dog(y) -> chase(x,pat))


\x.all y.(dog(y) -> chase(x,pat))

In [34]:
e1 = read_expr(r'\P.\x.exists y.(dog(y) & P(pat,x))')
e2 = read_expr('chase')
e3 = nltk.sem.ApplicationExpression(e1, e2)
print(e3.simplify()) # \x.exists y.(dog(y) & chase(pat,x))


\x.exists y.(dog(y) & chase(pat,x))

In [35]:
e1 = read_expr(r'\P x0 x1.exists y.(present(y) & P(x1,y,x0))')
e2 = read_expr('give')
e3 = nltk.sem.ApplicationExpression(e1, e2)
print(e3.simplify()) # \x0 x1.exists y.(present(y) & give(x1,y,x0))


\x0 x1.exists y.(present(y) & give(x1,y,x0))

Exercise 7


In [36]:
e1 = read_expr(r'\P.exists y.(dog(x) & P(x))')
e2 = read_expr('bark')
e3 = nltk.sem.ApplicationExpression(e1, e2)
print(e3.simplify()) # exists y.(dog(x) & bark(x))


exists y.(dog(x) & bark(x))

In [37]:
e1 = read_expr(r'\P.P(fido)')
e2 = read_expr('bark')
e3 = nltk.sem.ApplicationExpression(e1, e2)
print(e3.simplify()) # bark(fido)


bark(fido)

In [38]:
e1 = read_expr(r'\P. all x.(dog(x) -> bark(x))')
e2 = read_expr('\\P. all x.(dog(x) -> P(x))')
e3 = nltk.sem.ApplicationExpression(e1, e2)
print(e3.simplify()) # all x.(dog(x) -> bark(x))


all x.(dog(x) -> bark(x))

Exercise 11 (unfinished)


In [39]:
# Sentences:
# 1) Once upon a time there was a little boy, and he wanted to be a cock-a-doo-dle-doo.
# 2) So he was a cock-a-doo-dle-doo.
# 3) And he wanted to fly up into the sky.
# 4) So he did fly up into the sky.

# Should translate to:
# 1) be(boy) & want(boy, be(boy,cock-a-doo-dle-doo))
# 2) be(boy,cock-a-doo-dle-doo)
# 3) want(boy, fly(boy))
# 4) fly(boy)
from nltk import load_parser
cp = load_parser('file:chapter_10_ex11.fcfg')
# 2)
query = 'So he was a cock-a-doo-dle-doo'
trees = list(cp.parse(query.split()))
answer = trees[0].label()['SEM']
answer = [s for s in answer if s]
q = ' '.join(answer)
print(q)


be ( boy , cock-a-doo-dle-doo )

In [40]:
# 3)
query = 'And he wanted to fly up into the sky'
trees = list(cp.parse(query.split()))
answer = trees[0].label()['SEM']
answer = [s for s in answer if s]
q = ' '.join(answer)
print(q)


want ( boy , fly (boy) )

In [41]:
# 1a)
query = 'Once upon a time there was a little boy'
cp = load_parser('file:chapter_10_ex11.fcfg')
trees = list(cp.parse(query.split()))
answer = trees[0].label()['SEM']
answer = [s for s in answer if s]
q = ' '.join(answer)
print(q)


be ( boy )

In [42]:
# 1b)
query = 'he wanted to be a cock-a-doo-dle-doo'
cp = load_parser('file:chapter_10_ex11.fcfg')
trees = list(cp.parse(query.split()))
answer = trees[0].label()['SEM']
answer = [s for s in answer if s]
q = ' '.join(answer)
print(q)


want ( boy , be (boy, cock-a-doo-dle-doo ) )

In [43]:
# 1)
query = 'Once upon a time there was a little boy and he wanted to be a cock-a-doo-dle-doo'
cp = load_parser('file:chapter_10_ex11.fcfg')
trees = list(cp.parse(query.split()))
answer = trees[0].label()['SEM']
answer = [s for s in answer if s]
q = ' '.join(answer)
print(q)


be ( boy ) & want ( boy , be (boy, cock-a-doo-dle-doo ) )

In [44]:
# 4)
query = 'So he did fly up into the sky'
cp = load_parser('file:chapter_10_ex11.fcfg')
trees = list(cp.parse(query.split()))
answer = trees[0].label()['SEM']
answer = [s for s in answer if s]
q = ' '.join(answer)
print(q)


fly ( boy )

In [59]:
dom = {'b', 'c'}
v = """
boy => b
cockadoodledoo => c
fly => {b}
be => {(b)}, (b, c)}
"""
# don't know how to add the want with the nested phrase :/
val = nltk.Valuation.fromstring(v)
print(val)


{'fly': set([('b',)]), 'boy': 'b', 'cockadoodledoo': 'c', 'be': set([('b', 'c'), ('b',)])}

In [60]:
m = nltk.Model(dom, val)
g = nltk.Assignment(dom)
m.evaluate('fly ( boy )', g)


Out[60]:
True

In [61]:
m.evaluate('fly ( cockadoodledoo )', g)


Out[61]:
False

In [62]:
m.evaluate('-fly ( boy )', g)


Out[62]:
False

In [63]:
m.evaluate('be ( boy , cockadoodledoo )', g)


Out[63]:
True

In [64]:
m.evaluate('be ( boy )', g)


Out[64]:
True

In [65]:
m.evaluate('be ( cockadoodledoo , boy )', g)


Out[65]:
False

In [ ]: