CPSC 312 — Functional and Logic Programming
Project #2 – get started!
“Bad reasoning as well as good reasoning is possible; and this fact is the foundation of the practical side of logic.”
Charles Sanders Peirce
CPSC 312 — Lecture 27 1 / 7
©D. Poole 2021
Plan
Last time difference lists
definite clause grammars
natural language interfaces to databases Today
natural language interfaces (cont) computer algebra and calculus Semantic web
CPSC 312 — Lecture 27
2 / 7
©D. Poole 2021
Definite Clause Grammars
A sentence consists of a noun phrase followed by a verb phrase.
sentence(L,E) is true if (L,E) forms a difference list that is a sentence
noun phrase(L,E) is true if (L,E) forms a difference list that is a noun phrase
verb phrase(L,E) is true if (L,E) forms a difference list that is a verb phrase
sentence(L_0,L_2) :-
noun_phrase(L_0,L_1),
verb_phrase(L_1,L_2).
CPSC 312 — Lecture 27 3 / 7
©D. Poole 2021
Question-answering
How can we get from natural language directly to the answer?
Goal: map natural language to a query that is asked of a knowledge base.
Add arguments representing the individual noun phrase(T0,T1,O)
means
T0 − T1 is a difference list forming a noun phrase. The noun phrase refers to the individual O.
Can be implemented by the parser directly calling the knowledge base.
CPSC 312 — Lecture 27
4 / 7
©D. Poole 2021
Example natural language to query
see
http://www.cs.ubc.ca/~poole/cs312/2021/prolog/
geography.pl
Almost impossible to debug.
What does it mean if it answers false?
We can’t tell whether it couldn’t parse the question or there were no answers to the question.
Ideq: parse the sentence first, building a query that is then asked of the database.
http://www.cs.ubc.ca/~poole/cs312/2021/prolog/
geographyq.pl
CPSC 312 — Lecture 27 5 / 7
©D. Poole 2021
Building a list of constraints on the entity (geographyq.pl)
noun phrase(L0,L4,Entity,C0,C4) is true if L0 and L4 are list of words, such that
L4 is an ending of L0
the words in L0 before L4 (written L0 − L4) form a noun phrase
Entity is an individual that the noun phrase is referring to
C0 is a list such that C4 is an ending of C0 and C0−C4 contains the constraints imposed by the noun phrase
noun_phrase(L0,L4,Entity,C0,C4) :-
det(L0,L1,Entity,C0,C1),
adjectives(L1,L2,Entity,C1,C2),
noun(L2,L3,Entity,C2,C3),
mp(L3,L4,Entity,C3,C4).
CPSC 312 — Lecture 27
6 / 7
©D. Poole 2021
Building a list of constraints on the entity (geographyq.pl)
Nouns and adjectives provide constraints:
adj([large | L],L,Entity, [large(Entity)|C],C).
adj([Lang,speaking | L],L,Entity,
[speaks(Entity,Lang)|C],C).
noun([country | L],L,Entity, [country(Entity)|C],C).
noun([city | L],L,Entity, [city(Entity)|C],C).
Verbs and propositions provide relations reln(T0,T1,Subject,Object,C0,C1)
T 0 − T 1 is a verb or preposition that provides relations in
C 0 − C 1 that is true between individuals Subject and Object
reln([borders | L],L,O1,O2,[borders(O1,O2)|C],C).
reln([the,capital,of | L],L,O1,O2,
[capital(O2,O1)|C],C).
reln([next,to | L],L,O1,O2, [borders(O1,O2)|C],C).
CPSC 312 — Lecture 27 7 / 7
©D. Poole 2021