CPSC 312 — Functional and Logic Programming Project #2 - get started! But what is truly distinctive and valuable about human natural language is its semantic or representational capacities — the features of language responsible for how words carry meaning, and how words can be combined into sentences to make an indefinite number of distinct, meaningful assertions about the world. Kevin deLaplante “All the Formal Logic You Need to Know for Critical Thinking” https://criticalthinkeracademy.com/courses/2514/ lectures/751606 � D. Poole 2019 c CPSC 312 — Lecture 27 1 / 21
Today lists (cont.) definite clause grammars natural language interfaces to databases � D. Poole 2019 c CPSC 312 — Lecture 27 2 / 21
Clicker Question % append(L,A,R) is true if R contains the % elements of L followed by the elements of A append([],L,L). append([H|T],L,[H|R]) :- append(T,L,R). What is the answer to query ?- append([a,b,c],R,L). A There are no proofs B L = [a, b, c|R] C L=[a ,b, c], R=[] D L=[a,b,c], R=[a,b,c] � D. Poole 2019 c CPSC 312 — Lecture 27 3 / 21
Clicker Question What is the answer to query ?- append([a,b,c],R,L), append([1,2,3],S,R). A There are no proofs B R = [1, 2, 3|S], L = [1, 2, 3, a, b, c|S]. C R = [1, 2, 3|S], L = R. D R = [1, 2, 3|S], L = [a, b, c, 1, 2, 3|S]. E R = L, L = [a, b, c, 1, 2, 3|S]. � D. Poole 2019 c CPSC 312 — Lecture 27 4 / 21
Natural Language Understanding We want to communicate with computers using natural language (spoken and written). ◮ unstructured natural language — allow any statements, but make mistakes or failure. ◮ controlled natural language — only allow unambiguous statements that have fixed meanings (e.g., in supermarkets or for doctors). There is a vast amount of information in natural language. Understanding language to answer questions is more difficult than getting extracting gestalt properties such as topic, or choosing a web page. � D. Poole 2019 c CPSC 312 — Lecture 27 5 / 21
Syntax, Semantics, Pragmatics Syntax describes the form of language (using a grammar). Semantics provides the meaning of language. Pragmatics explains the purpose or the use of language (how utterances relate to the world). Examples: This lecture is about natural language. The green frogs sleep soundly. Colorless green ideas sleep furiously. Furiously sleep ideas green colorless. � D. Poole 2019 c CPSC 312 — Lecture 27 6 / 21
Understanding needs parsing A person with a big hairy cat drank the cold milk. Who or what drank the milk? Simple parse tree: s np vp np a person drank pp the cold milk np with a big hairy cat � D. Poole 2019 c CPSC 312 — Lecture 27 7 / 21
Context-free grammar A terminal symbol is a word (perhaps including punctuation). A non-terminal symbol can be rewritten as a sequence of terminal and non-terminal symbols, e.g., sentence �− → noun phrase , verb phrase verb phrase �− → verb , noun phrase verb �− → [ drank ] Can be written as a logic program, where a sentence is a sequence of words: sentence ( S ) :- noun phrase ( N ) , verb phrase ( P ) , append ( N , P , S ) . verb phrase ( P ) :- verb ( V ) , noun phrase ( N ) , append ( V , N , VP ) . To say word “drank” is a verb: verb ([ drank ]) . � D. Poole 2019 c CPSC 312 — Lecture 27 8 / 21
Difference Lists Non-terminal symbol s becomes a predicate with two arguments, s ( T 1 , T 2 ), meaning: ◮ T 2 is an ending of the list T 1 ◮ all of the words in T 1 before T 2 form a sequence of words of the category s . Lists T 1 and T 2 together form a difference list. “the student” is a noun phrase: noun phrase ([ the , student , passed , the , course ] , [ passed , the , course ]) The words “drank” and “passed” are verbs: verb ([ drank | W ] , W ) . verb ([ passed | W ] , W ) . � D. Poole 2019 c CPSC 312 — Lecture 27 9 / 21
Definite clause grammar The grammar rule sentence �− → noun phrase , verb phrase means that there is a sentence between T 0 and T 2 if there is a noun phrase between T 0 and T 1 and a verb phrase between T 1 and T 2 : sentence ( T 0 , T 2 ) :- noun phrase ( T 0 , T 1 ) , verb phrase ( T 1 , T 2 ) . sentence � �� � T 0 T 1 T 2 � �� � � �� � noun phrase verb phrase � D. Poole 2019 c CPSC 312 — Lecture 27 10 / 21
Definite clause grammar rules The rewriting rule h �− → b 1 , b 2 , . . . , b n says that h is b 1 followed by b 2 , . . . , followed by b n : h ( T 0 , T n ) :- b 1 ( T 0 , T 1 ) , b 2 ( T 1 , T 2 ) , . . . b n ( T n − 1 , T n ) . using the interpretation h � �� � T 2 · · · T n − 1 T 0 T 1 T n � �� � � �� � � �� � b 1 b 2 b n � D. Poole 2019 c CPSC 312 — Lecture 27 11 / 21
Terminal Symbols Non-terminal h gets mapped to the terminal symbols, t 1 , ..., t n : h ([ t 1 , · · · , t n | T ] , T ) using the interpretation h � �� � t 1 , · · · , t n T Thus, h ( T 1 , T 2 ) is true if T 1 = [ t 1 , ..., t n | T 2 ]. � D. Poole 2019 c CPSC 312 — Lecture 27 12 / 21
Context Free Grammar Example see http://www.cs.ubc.ca/~poole/cs312/2019/prolog/cfg_ simple.pl What will the following query return? noun phrase ([ the , student , passed , the , course , with , a , computer ] , R ) . How many answers does the following query have? sentence ([ the , computer , science , course , with , a , computer ] , R ) . � D. Poole 2019 c CPSC 312 — Lecture 27 13 / 21
Example % a noun phrase is a determiner followed by adjectives % followed by a noun followed by a prepositional phrase. noun_phrase(L0,L4) :- det(L0,L1), adjectives(L1,L2), noun(L2,L3), pp(L3,L4). % dictionary for determiners det(L,L). det([a|L],L). det([the|L],L). % adjectives is a sequence of adjectives adjectives(L,L). adjectives(L0,L2) :- adj(L0,L1), adjectives(L1,L2). � D. Poole 2019 c CPSC 312 — Lecture 27 14 / 21
Clicker Question If the query for the grammar rule noun_phrase([the,cat,on,the,mat,sat,on,the,hat], R). returns with substitution R=[sat,on,the,hat] What is the noun-phrase it found? A the cat B the mat C the cat on the mat D sat on the hat E either “the cat”, “the mat” or “the hat”, we can’t tell � D. Poole 2019 c CPSC 312 — Lecture 27 15 / 21
Clicker Question If the query for the grammar rule noun_phrase([the,cat,on,the,mat,sat,on,the,hat], R). returns with substitution R=[on,the,mat,sat,on,the,hat] What is the noun-phrase it found? A the cat B the mat C the cat on the mat D sat on the hat E either “the cat”, “the mat” or “the hat”, we can’t tell � D. Poole 2019 c CPSC 312 — Lecture 27 16 / 21
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 ( T 0 , T 1 , O ) means ◮ T 0 − T 1 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. � D. Poole 2019 c CPSC 312 — Lecture 27 17 / 21
Example natural language to query see http: //www.cs.ubc.ca/~poole/cs312/2019/prolog/geography.pl � D. Poole 2019 c CPSC 312 — Lecture 27 18 / 21
Adjectives provide properties % adj(T0,T1,Entity) is true if T0-T1 % is an adjective that is true of Entity adj([large | T],T,Entity) :- large(Entity). adj([Lang,speaking | T],T,Entity) :- speaks(Entity,Lang). adj([Lang,-,speaking | T],T,Entity) :- speaks(Entity,Lang). % adjectives(T0,T1,Entity) is true if % T0-T1 is a sequence of adjectives that true of Entity adjectives(T0,T2,Entity) :- adj(T0,T1,Entity), adjectives(T1,T2,Entity). adjectives(T,T,_). � D. Poole 2019 c CPSC 312 — Lecture 27 19 / 21
Verbs and propositions provide relations reln ( T 0 , T 1 , Subject , Object ) T 0 − T 1 is a verb or preposition that provides a relation that true between Subject and Object reln([borders | T],T,O1,O2) :- borders(O1,O2). reln([the,capital,of | T],T,O1,O2) :- capital(O2,O1). reln([next,to | T],T,O1,O2) :- borders(O1,O2). � D. Poole 2019 c CPSC 312 — Lecture 27 20 / 21
Verbs and propositions provide relations % noun_phrase(T0,T4,Entity) means T0-T4 is true of Entity noun_phrase(T0,T4,Entity) :- det(T0,T1,Entity), adjectives(T1,T2,Entity), noun(T2,T3,Entity), mp(T3,T4,Entity). % mp(T0,T2,Subject) means T0-T4 is an optional % modifying phrase that is true of Subject mp(T0,T2,Subject) :- reln(T0,T1,Subject,Object), noun_phrase(T1,T2,Object). mp([that|T0],T2,Subject) :- reln(T0,T1,Subject,Object), noun_phrase(T1,T2,Object). mp(T,T,_). � D. Poole 2019 c CPSC 312 — Lecture 27 21 / 21
Recommend
More recommend