This shows you the differences between two versions of the page.
teaching:is:nl-geo-solution [2021/05/26 15:27] Franconi Enrico created |
teaching:is:nl-geo-solution [2021/05/26 15:30] (current) Franconi Enrico |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | =====Geographical knowledge base===== | + | ======Geographical knowledge base (solution)====== |
+ | |||
+ | < | ||
+ | % Prolog representation of a grammar to ask a query of a database | ||
+ | % Builds a query which can then be asked of the knowledge base | ||
+ | % This is not meant to be polished or lingustically reasonable, but purely to show what can be done | ||
+ | |||
+ | % This is expanded code of Figure 13.12 in Section 13.6.6 of | ||
+ | % Poole and Mackworth, Artificial Intelligence: | ||
+ | % computational agents, Cambridge, 2017 | ||
+ | |||
+ | % noun_phrase(L0, | ||
+ | % 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 | ||
+ | |||
+ | % A noun phrase is a determiner followed by adjectives followed | ||
+ | % by a noun followed by an optional modifying phrase: | ||
+ | noun_phrase(L0, | ||
+ | det(L0, | ||
+ | adjectives(L1, | ||
+ | noun(L2, | ||
+ | mp(L3, | ||
+ | noun_phrase(L0, | ||
+ | proper_noun(L0, | ||
+ | |||
+ | % Try: | ||
+ | %?- noun_phrase([a, | ||
+ | %?- noun_phrase([a, | ||
+ | %?- noun_phrase([a, | ||
+ | |||
+ | % Determiners (articles) are ignored in this oversimplified example. | ||
+ | % They do not provide any extra constraints. | ||
+ | det([the | L], | ||
+ | det([a | L], | ||
+ | det(L, | ||
+ | |||
+ | |||
+ | % adjectives(L0, | ||
+ | % L0-L2 is a sequence of adjectives imposes constraints C0-C2 on Entity | ||
+ | adjectives(L0, | ||
+ | adj(L0, | ||
+ | adjectives(L1, | ||
+ | adjectives(L, | ||
+ | |||
+ | % An optional modifying phrase / relative clause is either | ||
+ | % a relation (verb or preposition) followed by a noun_phrase or | ||
+ | % ' | ||
+ | % nothing | ||
+ | mp(L0, | ||
+ | reln(L0, | ||
+ | noun_phrase(L1, | ||
+ | mp([that|L0], | ||
+ | reln(L0, | ||
+ | noun_phrase(L1, | ||
+ | mp(L, | ||
+ | |||
+ | % DICTIONARY | ||
+ | % adj(L0, | ||
+ | % is an adjective that imposes constraints C0-C1 Entity | ||
+ | adj([large | L], | ||
+ | adj([Lang, | ||
+ | adj([Lang, | ||
+ | |||
+ | noun([country | L], | ||
+ | noun([city | L], | ||
+ | |||
+ | % Countries and languages are proper nouns. | ||
+ | % We could either have it check a language dictionary or add the constraints. We chose to check the dictionary. | ||
+ | proper_noun([X | L],L,X, C,C) :- country(X). | ||
+ | proper_noun([X | L],L,X,C,C) :- language(X). | ||
+ | |||
+ | reln([borders | L], | ||
+ | reln([the, | ||
+ | reln([next, | ||
+ | |||
+ | % question(Question, | ||
+ | question([' | ||
+ | noun_phrase(L0, | ||
+ | mp(L1, | ||
+ | question([' | ||
+ | mp(L0, | ||
+ | question([' | ||
+ | noun_phrase(L0, | ||
+ | question([' | ||
+ | noun_phrase(L0, | ||
+ | mp(L1, | ||
+ | |||
+ | % ask(Q,A) gives answer A to question Q | ||
+ | ask(Q,A) :- | ||
+ | get_constraints_from_question(Q, | ||
+ | prove_all(C). | ||
+ | |||
+ | % get_constraints_from_question(Q, | ||
+ | get_constraints_from_question(Q, | ||
+ | question(Q, | ||
+ | member(End, | ||
+ | |||
+ | |||
+ | % prove_all(L) is true if all elements of L can be proved from the knowledge base | ||
+ | prove_all([]). | ||
+ | prove_all([H|T]) :- | ||
+ | call(H), | ||
+ | prove_all(T). | ||
+ | |||
+ | |||
+ | % The Database of Facts to be Queried | ||
+ | |||
+ | % country(C) is true if C is a country | ||
+ | country(argentina). | ||
+ | country(brazil). | ||
+ | country(chile). | ||
+ | country(paraguay). | ||
+ | country(peru). | ||
+ | |||
+ | % large(C) is true if the area of C is greater than 2m km^2 | ||
+ | large(brazil). | ||
+ | large(argentina). | ||
+ | |||
+ | % language(L) is true if L is a language | ||
+ | language(spanish). | ||
+ | language(portugese). | ||
+ | |||
+ | % speaks(Country, | ||
+ | speaks(argentina, | ||
+ | speaks(brazil, | ||
+ | speaks(chile, | ||
+ | speaks(paraguay, | ||
+ | speaks(peru, | ||
+ | |||
+ | capital(argentina,' | ||
+ | capital(chile,' | ||
+ | capital(peru,' | ||
+ | capital(brazil,' | ||
+ | capital(paraguay,' | ||
+ | |||
+ | % borders(C1, | ||
+ | borders(peru, | ||
+ | borders(chile, | ||
+ | borders(argentina, | ||
+ | borders(chile, | ||
+ | borders(brazil, | ||
+ | borders(peru, | ||
+ | borders(argentina, | ||
+ | borders(brazil, | ||
+ | borders(brazil, | ||
+ | borders(paraguay, | ||
+ | borders(argentina, | ||
+ | borders(paraguay, | ||
+ | |||
+ | /* Try the following queries: | ||
+ | ?- ask([' | ||
+ | ?- ask([' | ||
+ | ?- ask([' | ||
+ | ?- ask([' | ||
+ | ?- ask([' | ||
+ | ?- ask([' | ||
+ | ?- ask([' | ||
+ | ?- ask([' | ||
+ | ?- ask([' | ||
+ | */ | ||
+ | |||
+ | |||
+ | % To get the input from a line: | ||
+ | |||
+ | q(Ans) :- | ||
+ | write(" | ||
+ | readln(Ln), | ||
+ | ask(Ln, | ||
+ | |||
+ | |||
+ | /* | ||
+ | ?- q(Ans). | ||
+ | Ask me: What is a country that borders chile? | ||
+ | Ans = argentina ; | ||
+ | Ans = peru ; | ||
+ | false. | ||
+ | |||
+ | ?- q(Ans). | ||
+ | Ask me: What is the capital of a spanish speaking country that borders argentina? | ||
+ | Ans = ' | ||
+ | Ans = ' | ||
+ | false. | ||
+ | |||
+ | Some more questions: | ||
+ | What is next to chile? | ||
+ | Is brazil next to peru? | ||
+ | What is a country that borders a country that borders chile. | ||
+ | What is borders chile? | ||
+ | What borders chile? | ||
+ | What country borders chile? | ||
+ | What country that borders chile is next to paraguay? | ||
+ | What country that borders chile next to paraguay? | ||
+ | |||
+ | What country borders chile? | ||
+ | What country that borders chile is next to paraguay? | ||
+ | What country that borders chile next to paraguay? | ||
+ | */ | ||
+ | |||
+ | |||
+ | </ |