Artificial Intelligence and Prolog
Copyright © 2020 Jiri Kriz, www.nosco.ch

15    Natural Languages II

Solutions

15.1    Syntax and Semantics

Write a Prolog parser according to the method of the Definite Clause Grammar. The following sentences should be recognizes as grammatically correct:

The top-level predicate is s(Sentence, Tree), where Sentence is a list of words and Tree is the created syntax tree. E.g.

?- s([ a, man, arrived], Tree).  ⇒  Tree = . . .

Write a program for a pretty representation of the syntax tree and test it, e.g.:

?- s([ a, man, arrived], Tree), write_tree( Tree).

Extend the parser by an additional semantics component. The semantics of the above mentioned sentences should be correctly generated, e.g.:

s(Sentence, Tree, Sem) :- …
?- s([ a, man, arrived], Tree, Sem). 
 ⇒  Sem = exists(X, man(X), arrived(X))

Solution 15.1

15.2    Semantics Representation in a Database

Implement the predicate declare(Semantics), that stores the generated semantics in the Prolog database under the functor kb/1. You will also need to write a procedure to generate "Discourse Referents (Markers)". Example:

declare(exists(X, man(X), arrived(X))) :-
    /* generate next marker, e.g. f(3), */
    assert(kb(man(f(3))), 
	assert(arrived(f(3))).

Solution 15.2

15.3    Queries

Implement the predicate solve(Semantics), that uses the stored semantical facts kb/1 to prove the validity of statements. Example:

?- solve(exists(X, man(X), arrived(X)).
 ⇒  man(f(3))
arrived(f(3))
X = f(3)

Solution 15.3

15.4    Intelligent Dialog

Implement an intelligent dialog with the computer:

chat(Sentence).

If Sentence starts with Who/Does/Did, then it is a question that is passed (without the first word) to the procedure solve for proving.

Otherwise the Sentence is a statement that is passed to the procedure declare that stores it in the Prolog database.

The conversation should look as follows:

?- chat([john, loved, mary]).     
?- chat([who, loved, mary]).  ⇒  john
?- chat([who, did, john, love]).  ⇒  mary
?- chat([every, man, likes, mary]).    
?- chat([jack, is, a, man]).    
?- chat([mary, is, a, woman]).    
?- chat([does, jack, likes, mary]).  ⇒  yes

Solution 15.4