inteligencia artificial
TRANSCRIPT
![Page 1: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/1.jpg)
Inteligencia Artificial Interpretación semántica
Primavera 2009
profesor: Luigi Ceccaroni
![Page 2: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/2.jpg)
CFG recognition using difference lists
• An efficient implementation of CFGs can be obtained by making use of difference lists: a sophisticated Prolog technique.
• The key idea underlying difference lists is to represent the information about grammatical categories not as a single list, but as the difference between two lists.
• For example, instead of representing a woman shoots a man as [a,woman,shoots,a,man] we might represent it as the pair of lists
[a,woman,shoots,a,man] [ ].
![Page 3: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/3.jpg)
CFG recognition using difference lists
• Think of the first list as what needs to be consumed (or if you prefer: the input list), and the second list as what we should leave behind (or: the output list).
• Viewed from this (rather procedural) perspective the difference list
[a,woman,shoots,a,man] [ ].
• represents the sentence a woman shoots a man because it says: If I consume all the symbols on the left, and leave behind the symbols on the right, I have the sentence I am interested in.
![Page 4: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/4.jpg)
CFG recognition using difference lists
• The sentence we are interested in is the difference between the contents of the two lists.
• Difference representations are not unique. • In fact, we could represent a woman shoots a man in
infinitely many ways. • For example, we could also represent it as
[a,woman,shoots,a,man,ploggle,woggle] [ploggle,woggle].
• Again the point is: if we consume all the symbols on the left, and leave behind the symbols on the right, we have the sentence we are interested in.
![Page 5: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/5.jpg)
CFG recognition using difference lists
• If we bear the idea of consuming something, and leaving something behind in mind, we obtain the following recognizer (Prolog notation):
The s rule says: I know that the pair of lists X and Z represents a sentence if (1) I can consume X and leave behind a Y, and the pair X and Y represents a noun phrase, and (2) I can then go on to consume Y leaving Z behind, and the pair Y Z represents a verb phrase.
s(X,Z) :- np(X,Y), vp(Y,Z). np(X,Z) :- det(X,Y), n(Y,Z). vp(X,Z) :- v(X,Y), np(Y,Z). vp(X,Z) :- v(X,Z).
det([the|W],W).det([a|W],W). n([woman|W],W).n([man|W],W). v([shoots|W],W).
![Page 6: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/6.jpg)
CFG recognition using difference lists
• The idea underlying the way we handle the words is similar.
• The code n([man|W],W).
• means we are handling man as the difference between [man|W] and W.
• Intuitively, the difference between what I consume and what I leave behind is precisely the word man.
![Page 7: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/7.jpg)
CFG recognition using difference lists
• How do we use such grammars? Here's how to recognize sentences:
s([a,woman,shoots,a,man],[]).yes
• This asks whether we can get an s by consuming the symbols in [a,woman,shoots,a,man], leaving nothing behind.
![Page 8: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/8.jpg)
CFG recognition using difference lists
• Similarly, to generate all the sentences in the grammar, we ask
s(X,[]).
• This asks: what values can you give to X, such that we get an s by consuming the symbols in X, leaving nothing behind?
• The queries for other grammatical categories also work the same way.
• For example, to find out if a woman is a noun phrase we ask:
np([a,woman],[]).
![Page 9: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/9.jpg)
CFG recognition using difference lists
• And we generate all the noun phrases in the grammar as follows:
np(X,[]).
• It has to be admitted that this recognizer is not as easy to understand, at least at first, and it's a pain having to keep track of all those difference list variables.
• This is where DCGs come in.
![Page 10: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/10.jpg)
Formalismos de unificación
• La unificación se usa como mecanismo básico de composición entre constituyentes en gramáticas lógicas.
• Història:– Q-Systems (Colmerauer, 1972)– Prolog (Colmerauer, 1973)– Gramàtiques de Metamorfosi (Colmerauer,
1975)– Gramàtiques de Clàusules Definides (DCGs)
(Pereira, Warren, 1980)
![Page 11: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/11.jpg)
Análisis gramatical con unificación
• Gramática(1) oració (X,Y) :- gnom(X,Z), gver(Z,Y)(2) gnom(X,Y) :- art(X,Z), nom(Z,Y)(3) gver(X,Y) :- ver(X,Y)
• Lexicón(4) art(X,Y) :- el(X,Y)(5) nom(X,Y) :- gos(X,Y)(6) ver(X,Y) :- borda(X,Y)
![Page 12: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/12.jpg)
Análisis gramatical con unificación
(7) el(1,2)
(8) gos(2,3)
(9) borda(3,4)
Frase a analizar: oració(1,4)
el gos borda1 2 3 4
![Page 13: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/13.jpg)
Análisis gramatical con unificación
oració(1,4)(R1) (X 1, Y 4) por unificación
gnom (1,Z), gver(Z,4)(R2) aplicada a gnom(1,Z)
art(1,U), nom(U,Z), gver(Z,4)
el gos borda1 2 3 4
![Page 14: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/14.jpg)
Análisis gramatical con unificación
art(1,U), nom(U,Z), gver(Z,4)(R4) aplicada a art(1,U)
el(1,U), nom(U,Z), gver(Z,4)(R7) (U 2)
el(1,2), nom(2,Z), gver(Z,4)
nom(2,Z), gver(Z,4)
el gos borda1 2 3 4
![Page 15: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/15.jpg)
Análisis gramatical con unificación
nom(2,Z), gver(Z,4)(R5)
gos(2,Z), gver(Z,4)(R8) (Z 3)
gos(2,3), gver(3,4)
gver(3,4)
el gos borda1 2 3 4
![Page 16: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/16.jpg)
Análisis gramatical con unificación
gver(3,4)(R3)
ver(3,4)(R6)
borda(3,4)(R9): Fin
el gos borda1 2 3 4
![Page 17: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/17.jpg)
Análisis semántico
• Consiste en construir una representación de las frases en algún sistema formal.
• En general es un problema intratable.
• Se simplifica suponiendo que la semántica de una frase se pueda construir a partir de la semántica de sus partes: semántica compositiva.
• Algunas características del lenguaje se tienen que tratar a parte: referencias, omisiones, contexto…
![Page 18: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/18.jpg)
Estrategias de análisis
• Dos maneras de plantear la interpretación semántica:
– Secuencial (sintáctica → semántica)– Paralela (sintáctica + semántica)
![Page 19: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/19.jpg)
Interpretación secuencial
• Problemas de ambigüedad– Es posible que haya más de una
interpretación sintáctica.– Hay que considerarlas todas, para poder
comprobar sucesivamente cuáles son las semánticamente posibles.
• Principal ventaja– El análisis semántico parte de un análisis
sintáctico correcto.
![Page 20: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/20.jpg)
Interpretación paralela
• Principal problema– No se sabe si la interpretación sintáctica es
correcta hasta el final.
• Principal ventaja– Poder descartar interpretaciones sintácticas
correctas (o parcialmente correctas) que no tengan interpretación semántica asociada.
• Las reglas sintácticas incluyen la información semántica asociada.– Se obtiene como resultado un árbol de
análisis y una o varias interpretaciones.
![Page 21: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/21.jpg)
Sistema de representación
• El sistema de representación tiene que permitir:– Manejar cuantificación, predicación,
negación, modalidad (creencias)– Resolver la ambigüedad tanto léxica
(polisemia) como sintáctica– Manejar inferencias (herencia, razonamiento
por omisión)•Importante a la hora de resolver problemas
que involucren el contexto o el conocimiento del dominio.
![Page 22: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/22.jpg)
Sistema de representación
• Los sistemas que no se basan en la sintaxis para la interpretación suelen utilizar sistemas tipo ontologías.
• Por lo general, se utiliza una variedad de la lógica de primer orden adecuada al dominio de aplicación.
• El elemento básico de representación es el lexema: raíz de un grupo de palabras que son diferentes formas de “la misma palabra” (ej.: ir, ido, yendo).
![Page 23: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/23.jpg)
Sistema de representación
• Ejemplos– Los nombres propios corresponden a constantes.– Los verbos intransitivos a predicados unarios:
Juan ríe ríe(juan)– Los verbos transitivos a predicados de aridad
superior:
Juan lee el Quijote lee(juan, quijote)– Los nombres genéricos a predicados sobre
variables:
El hombre hombre(X)– Los adjetivos a predicados unarios:
La casa grande grande(X) casa(X)∧
![Page 24: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/24.jpg)
Sistema de representación
La representación consiste normalmente en un árbol de análisis y una función de composición que construye la interpretación de los elementos asociados.
semS = fS (semNP , semVP)
semNP = fNP (semART , semN)
semART = fART (sem el)
S
NP
NAME
Adrià
VP
V
menja
NP
ART N
bacallàel
![Page 25: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/25.jpg)
La representación del significado: ejemplo
entrada:Qui dirigeix el PSOE?
forma lògica:(pregunta (referent (X)) (X instancia (X, persona)
(el1 (Y instancia(Y, partit_polític)nom(Y, "PSOE"))
(Z instancia(Z, dirigir)present(Z)valor_prop(Z, agent, X)valor_prop(Z, pacient,Y)))))
![Page 26: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/26.jpg)
La representación del significado
• En la anterior forma lógica aparecen 4 tipos diferentes de información:– Estructura lógica– Contenido conceptual (semántico)– Indicación de los actos del discurso– Anotaciones pragmáticas
• El formalismo de representación debería proporcionar una capacidad expresiva suficiente para garantizar la descripción de estos 4 tipos de información.
![Page 27: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/27.jpg)
Posibles representaciones del significado
• Lógica de predicados de primer orden• Otros formalismos lógicos• Ontologías
27
![Page 28: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/28.jpg)
Representaciones basadas en lógica
• Un vocabulario de predicados de los que hay que indicar la aridad (el número de argumentos y su tipo)
• Un vocabulario de constantes y variables.• Un conjunto de conectores lógicos.• Un vocabulario de funciones de las que hay
que indicar la aridad• Un conjunto de cuantificadores que actúan
sobre los predicados que pueden ser cuantificados. 28
![Page 29: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/29.jpg)
Semántica y forma lógica
• Semantics = theories of meaning• Introduction of a level of context-
independent meaning called the logical form.– It can be produced directly from the syntactic
structure of a sentence.– It does not contain the results of any analysis
that requires interpretation of the sentence in context.
29
![Page 30: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/30.jpg)
Semántica y forma lógica
• Precisely defining the notions of semantics and meaning is surprisingly difficult.– The terms are used for several different
purposes in natural and technical usage.– There is a use of the verb mean that has
nothing to do with language:
This fire means someone camped here last night.
30
![Page 31: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/31.jpg)
Semántica y forma lógica
This fire means someone camped here last night.
• In this example, the fire is evidence for (or implies) the conclusion.
• The meaning we are interested in is not this one.
• It is closer to the usage when defining a word.
31
![Page 32: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/32.jpg)
Semántica y forma lógica
“Amble” means to walk slowly.
• This defines the meaning of a word in terms of other words.
• We are interested in specifying meaning without having to refer back to natural language itself.
• But even if we can do this, defining a notion of sentence meaning is difficult.
32
![Page 33: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/33.jpg)
Semántica y forma lógica
A guard at the airport: “Do you know what gate you are going to?”
• If we interpret this as asking whether we know where we are going, we answer yes.
• But the response can be based on a misunderstanding, if the guard then asks:
“Which gate is it?”33
![Page 34: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/34.jpg)
Semántica y forma lógica
“Do you know what gate you are going to?”
• This sentence then appears to mean different things in different contexts.
• Can we define a notion of sentence meaning that is independent of context?
• Is there a level at which this sentence has a single meaning, but may be used for different purposes?
34
![Page 35: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/35.jpg)
Semántica y forma lógica
• Separating sentence meaning from sentence usage is complex, but there are many advantages, primarily modularity.
• If sentences have no context-independent meaning, then we may not be able to separate:– the study of language– the study of general human reasoning and
context35
![Page 36: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/36.jpg)
Semántica y forma lógica
• We use the term meaning in a context-independent sense.
• We use the term usage for the context-dependent aspects.
• The representation of context-independent meaning is called the logical form.
36
![Page 37: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/37.jpg)
Semántica y forma lógica
• The process of mapping a sentence to its logical form is called semantic interpretation.
• The process of mapping the logical form to the final knowledge representation language is called contextual interpretation.
37
![Page 38: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/38.jpg)
Semántica y forma lógica
38
Syntactic analysis
semantic interpretation
Logical form (ROJA1 <LA b1 PELOTA>)
contextual interpretation
Final representation Roja(BO73)
S
NP VP
V
es
ADJP
ART N
pelotala
roja
![Page 39: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/39.jpg)
Interpretación semántica: función de composición
• Interpretación a través de evaluaciones lambda:
(lambda (x) (. . .)) =
= ((x) (. . .))• Gramática:
Oracio GN FV (2 1)
GN np (1)
FV vi (1)
Fv vt GN (1 2)
• Lexicón:
Pere np, pere
Maria np, maria
riu vi, ((x) (riu(x))
estima vt , (((x) ((y), estima(y, x))))
39
![Page 40: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/40.jpg)
Función de composición: ejemplo
40
![Page 41: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/41.jpg)
Función de composición: ejemplo
41
![Page 42: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/42.jpg)
Gramáticas de cláusulas definidas (DCGs)
• Las DCGs permiten escribir gramáticas en forma de programas como, por ejemplo, Prolog.
• Prolog es un lenguaje de reglas que usa razonamiento hacia atrás como método de resolución.
• Se define una sintaxis especial que permite diferenciar los elementos gramaticales de los procedimientos que se usan para tener en cuenta el contexto.
42
![Page 43: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/43.jpg)
Gramáticas de cláusulas definidas (DCGs): sintaxis
• Las reglas usan variables para pasarse información, hacer las comprobaciones que exige la gramática o construir un resultado.
• Se puede introducir código Prolog, entre llaves:
aaa(W) --> [W], bbb(W), {number(W)}
43
![Page 44: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/44.jpg)
Gramáticas de cláusulas definidas (DCGs): sintaxis
• Para ejecutar una DCG se llama al símbolo principal de la gramática con dos parámetros: una lista con las palabras de la frase y una lista vacía:
frase([el,gat,menja,bacalla],[])
44
![Page 45: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/45.jpg)
Gramáticas de cláusulas definidas (DCGs): ejemplo
analisis(X,Y):- asercion(X,Y).
asercion --> sn, verb,compl.
compl --> [].
compl --> prep,sn.
compl--> sn.
sn--> npr.
sn--> det,n.
verb--> [W],{verbo(W)}.
npr--> [W],{npropio(W)}.
n--> [W],{nombre(W)}.
det--> [W],{determ(W)}.
prep--> [W],{prepo(W)}.
npropio(clara).npropio(maria).npropio(barcelona).nombre(hombre).nombre(profesor).nombre(libro).determ(un).determ(el).verbo(esta).verbo(rie).verbo(piensa).verbo(habla).verbo(lee).prepo(en).prepo(con).prepo(de). 45
![Page 46: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/46.jpg)
What are definite clause grammars?
• Definite Clause Grammars (DCGs) are convenient ways to represent grammatical relationships for various parsing applications.
• They can be used for natural language work, for creating formal command and programming languages.
• Quite simply, they are a nice notation for writing grammars that hides the underlying difference list variables.
![Page 47: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/47.jpg)
DCGs
• A little grammar written as a DCG: s --> np, vp.np --> det, n.vp --> v, np.vp --> v.det --> [the].det --> [a].n --> [woman].n --> [man].v --> [shoots].
• How do we use this DCG? In fact, we use it in exactly the same way as we used the difference list recognizer.
![Page 48: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/48.jpg)
DCGs
• For example, to find out whether a woman shoots a man is a sentence, we pose the query:
s([a,woman,shoots,a,man],[]).
• That is, just as in the difference list recognizer, we ask whether we can get an s by consuming the symbols in [a,woman,shoots,a,man], leaving nothing behind.
![Page 49: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/49.jpg)
DCGs
• Similarly, to generate all the sentences in the grammar, we pose the query:
s(X,[]).
• This asks what values we can give to X, such that we get an s by consuming the symbols in X, leaving nothing behind.
• Moreover, the queries for other grammatical categories also work the same way. For example, to find out if a woman is a noun phrase we pose the query:
np([a,woman],[]).
![Page 50: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/50.jpg)
DCGs
• We generate all the noun phrases in the grammar as follows:
np(X,[]).
• Quite simply, this DCG is a difference list recognizer!
• That is, DCG notation is essentially syntactic sugar: user friendly notation that lets us write grammars in a natural way.
![Page 51: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/51.jpg)
DCGs
• The Prolog language can translate this notation into the kinds of difference lists discussed before.
• So we have the best of both worlds: – a nice simple notation for working with – the efficiency of difference lists
![Page 52: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/52.jpg)
DCGs
• To see what Prolog translates DCG rules into:– let Prolog consult the rules of this DCG, then if
you pose the query: listing(s)
– you will get the response:s(A,B) :-
np(A,C), vp(C,B).
• This is what Prolog has translated s --> np,vp into.
• Note that this is exactly the difference list rule we used in the recognizer.
![Page 53: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/53.jpg)
DCGs
• Similarly, if you pose the query:listing(np)
• you will get: np(A,B) :-
det(A,C), n(C,B).
• This is what Prolog has translated np --> det,n into.
• Again (apart from the choice of variables) this is the difference list rule we used in the recognizer.
![Page 54: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/54.jpg)
DCGs
• To get a complete listing of the translations of all the rules, simply type:listing.
![Page 55: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/55.jpg)
Separating rules and lexicon
• By separating rules and lexicon we mean that we want to eliminate all mentioning of individual words in the DCGs and instead record all the information about individual words separately in a lexicon.
• To see what is meant by this, let's return to the basic grammar, namely:
np - - > det, n.vp - - > v, np.vp - - > v.det - - > [the].det - - > [a].n - - > [woman].n - - > [man].v - - > [shoots].
![Page 56: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/56.jpg)
Separating rules and lexicon
• We are going to separate the rules form the lexicon.
• That is, we are going to write a DCG that generates exactly the same language, but in which no rule mentions any individual word.
• All the information about individual words will be recorded separately.
![Page 57: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/57.jpg)
Separating rules and lexicon
• Here is an example of a (very simple) lexicon.
• Lexical entries are encoded by using a predicate lex/2 whose first argument is a word, and whose second argument is a syntactic category:
lex(the, det).lex(a, det).lex(woman, n).lex(man, n).lex(shoots, v).
![Page 58: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/58.jpg)
Separating rules and lexicon
• A simple grammar that could go with this lexicon will be very similar to the basic DCG.
• In fact, both grammars generate exactly the same language.
• The only rules that change are those that mention specific words, i.e. the det, n, and v rules.
det --> [Word], {lex(Word, det)}.n --> [Word], {lex(Word, n)}.v --> [Word], {lex(Word, v)}.
![Page 59: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/59.jpg)
Separating rules and lexicon
Grammar:
np - - > det, n.vp - - > v, np.vp - - > v.det --> [Word], {lex(Word, det)}.n --> [Word], {lex(Word, n)}.v --> [Word], {lex(Word, v)}.
![Page 60: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/60.jpg)
Separating rules and lexicon
• Consider the new det rule:det --> [Word], {lex(Word, det)}.
• This rule says “a det can consist of a list containing a single element Word” (note that Word is a variable).
• The extra test adds the crucial condition: “as long as Word matches with something that is listed in the lexicon as a determiner”.
![Page 61: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/61.jpg)
Separating rules and lexicon
• With our present lexicon, this means that Word must be matched either with the word “a” or “the”:
lex(the, det).lex(a, det).
• So this single rule replaces the two previous DCG rules for det.
![Page 62: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/62.jpg)
Separating rules and lexicon
• This explains the how of separating rules from lexicon, but it doesn't explain the why.
• Is it really so important? • Is this new way of writing DCGs really
that much better?
![Page 63: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/63.jpg)
Separating rules and lexicon
• The answer is yes! for a theoretical reason:– Arguably rules should not mention specific lexical
items. – The purpose of rules is to list general syntactic facts,
such as the fact that a sentence can be made up of a noun phrase followed by a verb phrase.
– The rules for s, np, and vp describe such general syntactic facts, but the old rules for det, n, and v don't.
– Instead, the old rules simply list particular facts: that a is a determiner, that the is a determiner, and so on.
– From a theoretical perspective it is much neater to have a single rule that says “anything is a determiner (or a noun, or a verb,...) if it is listed as such in the lexicon”.
![Page 64: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/64.jpg)
Separating rules and lexicon
• Now, our little lexicon, with its simple lex entries, is a toy.
• But a real lexicon is (most emphatically!) not.
• A real lexicon is likely to be very large (it may contain hundreds of thousands, or even millions, of words) and moreover, the information associated with each word is likely to be very rich.
![Page 65: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/65.jpg)
Separating rules and lexicon
• Our lex entries give only the syntactical category of each word.
• A real lexicon will give much more, such as information about its phonological, morphological, semantic, and pragmatic properties.
• Because real lexicons are big and complex, from a software engineering perspective it is best to write simple grammars that have a well-defined way of pulling out the information they need from vast lexicons.
![Page 66: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/66.jpg)
Separating rules and lexicon
• That is, grammars should be thought of as separate entities which can access the information contained in lexicons.
• We can then use specialized mechanisms for efficiently storing the lexicon and retrieving data from it.
• The new rules really do just list general syntactic facts, and the extra tests act as an interface to our (admittedly simple) lexicon that lets the rules find exactly the information they need.
![Page 67: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/67.jpg)
Grammar 1: a trivial grammar for a fragment of language
s np, vp. % A sentence (s) is a noun phrase (np) plus a verb phrase (vp)
np det, n. % A noun phrase is a determiner plus a noun
np n. % ... or just a noun. vp v, np. % A verb phrase is a verb and
its direct object, which is an np vp v. % ... or just the verb (for
intransitives).
det [Word], {lex(Word, det)}.n [Word], {lex(Word, n)}.v [Word], {lex(Word, v)}.
![Page 68: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/68.jpg)
Grammar 1: a trivial grammar for a fragment of language
lex(the, det). % ‘the’ is a determiner
lex(mary, n). % ‘mary’ is a noun.
lex(john, n).
lex(woman, n).
lex(apple, n).
lex(man, n).
lex(loves, v). % ‘loves’ is a verb.
lex(eats, v).
lex(sings, v).
![Page 69: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/69.jpg)
Sentences for Grammar 1
• mary loves john• the woman eats the apple• the man sings• mary eats
![Page 70: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/70.jpg)
Grammar 2: restrictions in argument selection
s np, vp.
compl([ ]) [ ].
compl([arg(X)]) p(X), np.
compl([ ]) np.
np name.
np det, n.
vp v(X), compl(X). % A vp is a verb plus a verbal complement (compl)
![Page 71: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/71.jpg)
Grammar 2: restrictions in argument selection
v(A) [Word], {lex(Word, v, A)}.
name [Word], {lex(Word, name)}.
n [Word], {lex(Word, n)}.
det [Word], {lex(Word, det)}.
p(Word) [Word], {lex(Word, p)}.
![Page 72: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/72.jpg)
Grammar 2: restrictions in argument selection
lex(piensa, v, [arg(en)]).
lex(está, v, [arg(en)]).
lex(ríe, v, [ ]).
lex(habla, v, [arg(con)]).
lex(lee, v, [ ]).
lex(el, det). % ‘el’ is a determiner
![Page 73: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/73.jpg)
Grammar 2: restrictions in argument selection
lex(un, det).
lex(mary, name).
lex(john, name).
lex(profesor, n).
lex(en, p)
![Page 74: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/74.jpg)
Sentences for Grammar 2
• mary piensa en john• john habla con mary• john ríe• un profesor habla con mary
![Page 75: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/75.jpg)
Extension of Grammar 2
John habla de Clara con Mary
• Needed modifications:compl([arg(X) | Y]) p(X), np, compl(Y).
lex(habla, v, [arg(de), arg(con)]).
lex(clara, name).
![Page 76: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/76.jpg)
Grammar 3: logical representation of sentences
s(F) np(S), v(S, X, F), compl(X).
compl([ ]) [ ].
compl([arg(X,O) | Y]) p(X), np (O), compl(Y).
compl([arg(null, O) | Y]) np(O), compl(Y).
np(S) name(S).
np(S) det, n(S).
![Page 77: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/77.jpg)
Grammar 3: logical representation of sentences
v(S,A,F) [Word], {lex(Word, v, S, A, F)}.
name(Word) [Word], {lex(Word, name)}.
n(Word) [Word], {lex(Word, n)}.
det [Word], {lex(Word, det)}.
p(Word) [Word], {lex(Word, p)}.
![Page 78: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/78.jpg)
Grammar 3: logical representation of sentences
lex(clara, name).
lex(maria, name).
lex(juan, name).
lex(barcelona, name).
lex(libro, n).
lex(hombre, n).
lex(profesor, n).
![Page 79: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/79.jpg)
Grammar 3: logical representation of sentences
lex(el, det).
lex(un, det).
lex(en, p).
lex(con, p).
lex(de, p).
![Page 80: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/80.jpg)
Grammar 3: logical representation of sentences
lex(ríe, v, S, [ ], reir(S)).
lex(piensa, v, S, [arg(en, O)], pensar_en(S, O)).
lex(habla, v, S, [arg(de, O),arg(con, O1)], comunica(S,O, O1)).
lex(habla, v, S, [arg(con, O),arg(de, O1)], comunica(S,O1, O)).
lex(está, v, S, [arg(en, O)], locativo(S, O)).
lex(lee, v, S, [arg(null, O)], leer(S, O)).
![Page 81: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/81.jpg)
Sentences for Grammar 3
• unary predicate (ríe, v, S, [ ], reir(S)).• binary predicate (piensa, v, S, [arg(en, O)],
pensar_en(S, O)).• ternary predicate (habla, v, S, [arg(con,O),
arg(de, O1)], comunica(S, O1, O)).• Example:
Juan piensa en Maria = pensar_en(juan, maria).
![Page 82: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/82.jpg)
Prolog input and output
analysis(F, X, [ ]):- s(F, X, [ ]).
| ?- analysis(F, [juan, está, en, barcelona], [ ]).
F = locativo(juan, barcelona) ?
yes
| ?- analysis(F, [juan, piensa, en, maria], [ ]).
F = pensar_en(juan, maria) ?
yes
![Page 83: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/83.jpg)
Prolog input and output
| ?- analysis(F, [el, libro, está, en, barcelona], [ ]).
F = locativo(libro, barcelona) ?
yes
| ?- analysis(F, [juan, lee, un, libro], [ ]).
F = leer(juan, libro) ?
yes
![Page 84: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/84.jpg)
Prolog input and output
| ?- analysis(F, [el, hombre, habla, de, juan, con, maria], [ ]).
F = comunica(hombre, juan, maria) ?
yes
| ?- analysis(F, [el, hombre, ríe], [ ]).
F = reir(hombre) ?
yes
![Page 85: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/85.jpg)
Prolog input and output
| ?- analysis(F, [el, profesor, piensa, en, un, libro], [ ]).
F = pensar_en(profesor, libro) ?
yes
![Page 86: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/86.jpg)
Grammar 4: quantification
• It exists X and X is a libro:el libro = e(X, libro(X)).
• All X such that X is a libro:todo libro = a(X, libro(X)).
![Page 87: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/87.jpg)
Grammar 4: quantification
el libro cae = e(X, and(libro(X), cae(X)))
Juan piensa en el libro = e(X, and(libro(X), piensa(juan, X)))
todo hombre piensa en el libro = a(X, implies(hombre(X), e(Y, and(libro(Y), piensa(X,Y)))
![Page 88: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/88.jpg)
Grammar 4: quantification
lex(el,det,K,S1,S2,e(K,and(S1,S2))).
lex(un,det,K,S1,S2,e(K,and(S1,S2))).
lex(los,det,K,S1,S2,a(K,implies(S1,S2))).
lex(todo,det,K,S1,S2,a(K,implies(S1,S2))).
np(K,S2,F) det(K,S1,S2,F), n(K,S1).
np(K,F,F) name(K).
![Page 89: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/89.jpg)
Grammar 4: quantification
compl([ ],S,S) [ ].
compl([arg(X,K) | Y],S1,S) p(X), np(K,S2,S), compl(Y,S1,S2).
s(S) np(K,S2,S), v(K,X,S1), compl(X,S1,S2).
n(K,F) [Word],{lex(Word,n,K,F)}.
lex(libro,n,K,libro(K)).
![Page 90: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/90.jpg)
Prolog input and output
| ?- analysis(F,[el,hombre,ríe],[ ]).
F = e(_A,and(hombre(_A),reir(_A))) ?
yes
| ?- analysis(F,[el,profesor,piensa,en,un,libro],[ ]).
F = e(_B,and(profesor(_B),e(_A,and(libro(_A),pensar_en(_B,_A))))) ?
yes
![Page 91: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/91.jpg)
Prolog input and output
| ?- analysis(F,[el,hombre,habla,de,juan,con,maria],[ ]).
F = e(_A,and(hombre(_A),comunica(_A,juan,maria))) ?
yes
| ?- analysis(F,[todo,hombre,piensa,en,un,libro],[ ]).
F = a(_B,implies(hombre(_B),e(_A,and(libro(_A),pensar_en(_B,_A))))) ?
yes
![Page 92: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/92.jpg)
Prolog input and output
| ?- analysis(F,[todo,libro,esta,en,barcelona],[ ]).
F = a(_A,implies(libro(_A),locativo(_A,barcelona))) ?
yes
![Page 93: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/93.jpg)
Extension of Grammar 4
El hombre bueno:
e(X, and(hombre(X), bueno(X)))
![Page 94: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/94.jpg)
Grammar 5: semantic constraints
• Argument selection:– *Juan lee el hombre
– El gato come pescado
– El perro corre por el camino
• Semantic categories:– human: Juan, María, Clara, hombre, profesor
– animate: perro, gato
– inanimate: libro, pescado, bocadillo, silla
– locative: Barcelona, camino
![Page 95: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/95.jpg)
Nouns’ semantic categories
lex(clara, name, human).
lex(maria, name, human).
lex(juan, name, human).
lex(barcelona, name, locative).
![Page 96: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/96.jpg)
Nouns’ semantic categories
lex(libro, n, K, libro(K), inanimate).
lex(hombre, n, K, hombre(K), human).
lex(profesor, n, K, profesor(K), human).
lex(perro, n, K, perro(K), animate).
lex(gato, n, K, gato(K), animate).
lex(camino, n, K, camino(K), locative).
![Page 97: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/97.jpg)
Verbs’ semantic categories
lex(piensa, v, S, [arg(en,O)], pensar_en(S,O), sem(human, [X])).
lex(habla, v, S, [arg(de,O),arg(con,O1)], comunica(S,O,O1), sem(human, [X,human])).
lex(está, v, S, [arg(en,O)], locative(S,O), sem(X, [locative])).
lex(lee, v, S, [arg(null,O)], leer(S,O), sem(human, [inanimate])).
![Page 98: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/98.jpg)
Grammar 5: semantic constraints
v(S,A,F,sem) [Word],{lex(Word,v,S,A,F,sem)}.
name(Word,sem) [Word],{lex(Word,name,sem)}.
n(Word,sem) [Word],{lex(Word,n,sem)}.
np(K,S2,F,sem) det(K,S1,S2,F), n(K,S1,sem).
np(K,F,F,sem) name(K,sem).
![Page 99: Inteligencia artificial](https://reader036.vdocuments.net/reader036/viewer/2022062418/556f1c56d8b42ac8048b5215/html5/thumbnails/99.jpg)
Grammar 5: semantic constraints
compl([ ],S,S,[ ]) [ ].
compl([arg(X,K) | Y],S1,S,[sem | sem2]) p(X), np(K,S2,S,sem), compl(Y,S1,S2,sem2).
s(S) np(K,S2,S,sem1), v(K,X,S1,sem(sem1,list-sem)), compl(X,S1,S2,[list-sem]).