Exercise from http://www.nltk.org/book_1ed/ch10.html

Author : Nirmal kumar Ravi


In [5]:
import nltk
lp = nltk.sem.Expression.fromstring

Translate the following sentences into propositional logic and verify that they parse with LogicParser. Provide a key which shows how the propositional variables in your translation correspond to expressions of English.

  • If Angus sings, it is not the case that Bertie sulks.
  • Cyril runs and barks.
  • It will snow if it doesn't rain.
  • It's not the case that Irene will be happy if Olive or Tofu comes.
  • Pat didn't cough or sneeze.
  • If you don't come if I call, I won't come if you call.

In [7]:
p = 'Angus sings'
q = 'Bertie sulks'
lp('p->-q')


Out[7]:
<ImpExpression (p -> -q)>

In [8]:
p = 'Cyril runs'
q = 'Cyril barks'
lp('p & q')


Out[8]:
<AndExpression (p & q)>

In [9]:
p = 'rain'
q = 'snow'
lp('-p -> q')


Out[9]:
<ImpExpression (-p -> q)>

In [10]:
p = 'Olive comes'
q = 'Tofu comes'
r = 'Irene will be happy'
lp('(p|q)-> -r')


Out[10]:
<ImpExpression ((p | q) -> -r)>

In [11]:
p = 'Pat cough'
q = 'Pat sneeze'
lp('-p|-q')


Out[11]:
<OrExpression (-p | -q)>

In [12]:
p = 'I call'
q = 'you come'
r = 'You call'
s = 'I come'
lp('(p->-q)-> (r->-s)')


Out[12]:
<ImpExpression ((p -> -q) -> (r -> -s))>

Translate the following sentences into predicate-argument formula of first order logic.

  • Angus likes Cyril and Irene hates Cyril.
  • Tofu is taller than Bertie.
  • Bruce loves himself and Pat does too.
  • Cyril saw Bertie, but Angus didn't.
  • Cyril is a fourlegged friend.
  • Tofu and Olive are near each other.

In [13]:
lp('like(Angus,Cyril) & hate(Irene,Cyril)')


Out[13]:
<AndExpression (like(Angus,Cyril) & hate(Irene,Cyril))>

In [14]:
lp('taller(Tofu,Bertie)')


Out[14]:
<ApplicationExpression taller(Tofu,Bertie)>

In [15]:
lp('loveshimself(Bruse) & loveshimself(Pat)')


Out[15]:
<AndExpression (loveshimself(Bruse) & loveshimself(Pat))>

In [16]:
lp('saw(cyril,Bertie) & -saw(cyril,Angus)')


Out[16]:
<AndExpression (saw(cyril,Bertie) & -saw(cyril,Angus))>

In [17]:
lp('fourleggedfriend(Cyril)')


Out[17]:
<ApplicationExpression fourleggedfriend(Cyril)>

In [18]:
lp('neareachother(Tofu,Olive)')


Out[18]:
<ApplicationExpression neareachother(Tofu,Olive)>

Translate the following sentences into quantified formulas of first order logic.

  • Angus likes someone and someone likes Julia.
  • Angus loves a dog who loves him.
  • Nobody smiles at Pat.
  • Somebody coughs and sneezes.
  • Nobody coughed or sneezed.
  • Bruce loves somebody other than Bruce.
  • Nobody other than Matthew loves somebody Pat.
  • Cyril likes everyone except for Irene.
  • Exactly one person is asleep.

In [19]:
lp('(exists x. likes(Angus,x) & exists y. likes(y,Julia))')


Out[19]:
<AndExpression (exists x.likes(Angus,x) & exists y.likes(y,Julia))>

In [20]:
lp('-exists x. smiles(x,Pat)')


Out[20]:
<NegatedExpression -exists x.smiles(x,Pat)>

In [21]:
lp('-exists x. (cough(x) | sneeze(x))')


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

In [25]:
lp('exists x. (asleep(x) & all y. (asleep(y) -> (y = x)))')


Out[25]:
<ExistsExpression exists x.(asleep(x) & all y.(asleep(y) -> (y = x)))>

Translate the following verb phrases using λ abstracts. quantified formulas of first order logic.

  • feed Cyril and give a capuccino to Angus
  • be given 'War and Peace' by Pat
  • be loved by everyone
  • be loved or detested by everyone
  • be loved by everyone and detested by no-one

In [26]:
lp('\\x. all y. love(y,x)')


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

In [27]:
lp('\\x. all y. (love(y,x) | detested(y,x))')


Out[27]:
<LambdaExpression \x.all y.(love(y,x) | detested(y,x))>

In [28]:
lp('\\x. (all y. love(y,x) & -exists z. detested(z,x))')


Out[28]:
<LambdaExpression \x.(all y.love(y,x) & -exists z.detested(z,x))>