Exercise 1

``````

In [1]:

import nltk, re, pprint

``````
``````

In [2]:

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]:

``````
``````

Out[8]:

<AndExpression (like(angus,cyril) & hate(irene,cyril))>

``````
``````

In [9]:

``````
``````

Out[9]:

<ApplicationExpression taller(tofu,bertie)>

``````
``````

In [10]:

# or

``````
``````

Out[10]:

<AndExpression (love(bruce,bruce) & love(pat,bruce))>

``````
``````

In [11]:

``````
``````

Out[11]:

<AndExpression (see(cyril,bertie) & -see(angus,bertie))>

``````
``````

In [12]:

``````
``````

Out[12]:

<ApplicationExpression fourleggedfriend(cyril)>

``````
``````

In [13]:

``````
``````

Out[13]:

<AndExpression (near(tofu,olive) & near(olive,tofu))>

``````

Exercise 3

``````

In [14]:

``````
``````

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]:

``````
``````

Out[16]:

<NegatedExpression -exists x.smile(x,pat)>

``````
``````

In [17]:

``````
``````

Out[17]:

<ExistsExpression exists x.(cough(x) & sneeze(x))>

``````
``````

In [18]:

``````
``````

Out[18]:

<NegatedExpression -exists x.(cough(x) | sneeze(x))>

``````
``````

In [19]:

``````
``````

Out[19]:

<ExistsExpression exists x.(love(bruce,x) & -equal(x,bruce))>

``````
``````

In [20]:

``````
``````

Out[20]:

<NegatedExpression -exists x.(love(x,pat) & -equal(x,matthew))>

``````
``````

In [21]:

``````
``````

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]:

``````
``````

Out[23]:

<LambdaExpression \x.(feed(x,cyril) & give(x,cappuccino,angus))>

``````
``````

In [24]:

``````
``````

Out[24]:

<LambdaExpression \x.give(pat,warandpeace,x)>

``````
``````

In [25]:

``````
``````

Out[25]:

<AllExpression all y.\x.love(y,x)>

``````
``````

In [26]:

``````
``````

Out[26]:

<AllExpression all y.\x.(love(y,x) | detest(y,x))>

``````
``````

In [27]:

``````
``````

Out[27]:

<AllExpression all y.\x.(love(y,x) & -detest(y,x))>

``````

Exercise 5

``````

In [28]:

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)))')
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]:

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))')
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))')
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))')
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))')
e3 = nltk.sem.ApplicationExpression(e1, e2)
print(e3.simplify()) # exists y.(dog(x) & bark(x))

``````
``````

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

``````
``````

In [37]:

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)
# 2)
query = 'So he was a cock-a-doo-dle-doo'
trees = list(cp.parse(query.split()))
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()))
print(q)

``````
``````

want ( boy , fly (boy) )

``````
``````

In [41]:

# 1a)
query = 'Once upon a time there was a little boy'
trees = list(cp.parse(query.split()))
print(q)

``````
``````

be ( boy )

``````
``````

In [42]:

# 1b)
query = 'he wanted to be a cock-a-doo-dle-doo'
trees = list(cp.parse(query.split()))
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'
trees = list(cp.parse(query.split()))
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'
trees = list(cp.parse(query.split()))
print(q)

``````
``````

fly ( boy )

``````
``````

In [59]:

dom = {'b', 'c'}
v = """
boy => b
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]:

``````
``````

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 [ ]:

``````