Lógica de primer orden

  • 30 min | Última modificación: Diciembre 10, 2020

http://www.nltk.org/book/

Text Analytics with Python

[ ]:
## Las sentencias declaraticas son True/False en ciertas situaciones
## NP (Noun phrases) y NNS (sustantivos) se refieren a cosas en el mundo

Definición de modelo:

[1]:
import nltk

nltk.boolean_ops()
negation        -
conjunction     &
disjunction     |
implication     ->
equivalence     <->
[3]:
read_expr = nltk.sem.Expression.fromstring
[7]:
##
## Negación
##
read_expr('-P')
[7]:
<NegatedExpression -P>
[8]:
##
## Conjunción (and)
##
read_expr('P & Q')
[8]:
<AndExpression (P & Q)>
[9]:
##
## Disyunción (or)
##
read_expr('P | Q')
[9]:
<OrExpression (P | Q)>
[10]:
##
## Implicación (if ... then ...)
##
read_expr('P -> Q')
[10]:
<ImpExpression (P -> Q)>
[11]:
##
## Equivalencia (if and only if ... / iff)
##
read_expr('P <-> Q')
[11]:
<IffExpression (P <-> Q)>
[3]:
read_expr('-(P & Q)')
[3]:
<NegatedExpression -(P & Q)>
[5]:
read_expr('P | (R -> Q)')
[5]:
<OrExpression (P | (R -> Q))>
[6]:
read_expr('P <-> -- P')
[6]:
<IffExpression (P <-> --P)>
[14]:
!apt-get update -y && apt-get install -yq prover9
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:3 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [237 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:6 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [15.3 kB]
Get:7 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [1816 kB]
Get:8 http://archive.ubuntu.com/ubuntu bionic/restricted amd64 Packages [13.5 kB]
Get:9 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [11.3 MB]
Get:10 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1372 kB]
Get:11 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages [1344 kB]
Get:12 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [186 kB]
Get:13 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [2244 kB]
Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2136 kB]
Get:15 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [53.8 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [266 kB]
Get:17 http://archive.ubuntu.com/ubuntu bionic-backports/main amd64 Packages [11.3 kB]
Get:18 http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [11.4 kB]
Fetched 21.5 MB in 12s (1738 kB/s)
Reading package lists... Done
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  libladr4
Suggested packages:
  ladr4-apps prover9-doc
The following NEW packages will be installed:
  libladr4 prover9
0 upgraded, 2 newly installed, 0 to remove and 23 not upgraded.
Need to get 245 kB of archives.
After this operation, 801 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/universe amd64 libladr4 amd64 0.0.200911a-2.1build1 [168 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic/universe amd64 prover9 amd64 0.0.200911a-2.1build1 [77.8 kB]
Fetched 245 kB in 1s (219 kB/s)
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76, <> line 2.)
debconf: falling back to frontend: Readline
Selecting previously unselected package libladr4.
(Reading database ... 13639 files and directories currently installed.)
Preparing to unpack .../libladr4_0.0.200911a-2.1build1_amd64.deb ...
Unpacking libladr4 (0.0.200911a-2.1build1) ...
Selecting previously unselected package prover9.
Preparing to unpack .../prover9_0.0.200911a-2.1build1_amd64.deb ...
Unpacking prover9 (0.0.200911a-2.1build1) ...
Setting up libladr4 (0.0.200911a-2.1build1) ...
Setting up prover9 (0.0.200911a-2.1build1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.2) ...
[15]:
lp = nltk.sem.Expression.fromstring
SnF = read_expr('SnF')
NotFnS = read_expr('-FnS')
R = read_expr('SnF -> -FnS')
prover = nltk.Prover9()
prover.prove(NotFnS, [SnF, R])
[15]:
True
[16]:
val = nltk.Valuation([('P', True), ('Q', True), ('R', False)])
[17]:
val['P']
[17]:
True
[18]:
dom = set()
g = nltk.Assignment(dom)

## inicialización del modelo
m = nltk.Model(dom, val)

print(m.evaluate('(P & Q)', g))
print(m.evaluate('-(P & Q)', g))
print(m.evaluate('(P & R)', g))
print(m.evaluate('(P | R)', g))
True
False
False
True
[20]:
read_expr = nltk.sem.Expression.fromstring
expr = read_expr('walk(angus)', type_check=True)
expr.argument
[20]:
<ConstantExpression angus>
[21]:
expr.argument.type
[21]:
e
[23]:
expr.function
[23]:
<ConstantExpression walk>
[22]:
expr.function.type
[22]:
<e,?>
[25]:
sig = {'walk': '<e, t>'}
expr = read_expr('walk(angus)', signature=sig)
expr.function.type
[25]:
e
[27]:
read_expr = nltk.sem.Expression.fromstring
read_expr('dog(cyril)').free()
[27]:
set()
[28]:
read_expr('dog(x)').free()
[28]:
{Variable('x')}
[29]:
read_expr('own(angus, cyril)').free()
[29]:
set()
[30]:
read_expr('exists x.dog(x)').free()
[30]:
set()
[31]:
read_expr('((some x. walk(x)) -> sing(x))').free()
[31]:
{Variable('x')}
[32]:
read_expr('exists x.own(y, x)').free()
[32]:
{Variable('y')}
[34]:
NotFnS = read_expr('-north_of(f, s)')
SnF = read_expr('north_of(s, f)')
R = read_expr('all x. all y. (north_of(x, y) -> -north_of(y, x))')
prover = nltk.Prover9()
prover.prove(NotFnS, [SnF, R])
[34]:
True
[36]:
FnS = read_expr('north_of(f, s)')
prover.prove(FnS, [SnF, R])
[36]:
False
[37]:
dom = {'b', 'o', 'c'}
[39]:
v = """
bertie => b
olive => o
cyril => c
boy => {b}
girl => {o}
dog => {c}
walk => {o, c}
see => {(b, o), (c, b), (o, c)}
"""
val = nltk.Valuation.fromstring(v)
print(val)
{'bertie': 'b',
 'boy': {('b',)},
 'cyril': 'c',
 'dog': {('c',)},
 'girl': {('o',)},
 'olive': 'o',
 'see': {('o', 'c'), ('b', 'o'), ('c', 'b')},
 'walk': {('o',), ('c',)}}
[40]:
('o', 'c') in val['see']
[40]:
True
[41]:
('b',) in val['boy']
[41]:
True
[ ]:

[ ]:

[ ]:

[ ]:

[38]:
text = '''



>>> v = """
... bertie => b
... olive => o
... cyril => c
... boy => {b}
... girl => {o}
... dog => {c}
... walk => {o, c}
... see => {(b, o), (c, b), (o, c)}
... """
>>> val = nltk.Valuation.fromstring(v)
>>> print(val)


'''


text = text.replace(">>> ", "").replace("... ", "").replace("...", "").replace("\t", "")
print(text)




v = """
bertie => b
olive => o
cyril => c
boy => {b}
girl => {o}
dog => {c}
walk => {o, c}
see => {(b, o), (c, b), (o, c)}
"""
val = nltk.Valuation.fromstring(v)
print(val)



[ ]: