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

14    Natural Languages I

Solutions

14.1    Tokenizer

Write a tokenizer that produces a list of Prolog atoms from a string, e.g.

?- tokenize( "the man arrived", L).    
 ⇒  [ the, man, arrived]
?- tokenize( "who arrived?", L).
 ⇒  L = [ who, arrived] 

Use tokenize to implement an interactive shell talk, that reads sentences of the user, processes them and writes the result. An example of the processing result would be to write the atoms and the number of atoms.

?- talk.
> the man arrived
the man arrived /3 
> who arrived? 
who arrived /2
> bye

Hints:

Solution 14.1

14.2    Natural Language Database Queries

Write a simple, ad hoc front-end for natural language queries for relational databases. As an example consider a database with states and cities in Europe:

/* state( Name, Population, Area, Capital). */
state( switzerland, 6, 41, bern). /* etc. */

/* city( Name, In_state, Population */
city( zurich,  switzerland, 0.7). /* etc. */

The following queries should be possible:

Hints:

Solution 14.2

14.3    ELIZA

Write a Prolog program that simulates a psychiatrist according to the ELIZA approach. The following conversation should be possible::

> i am very unhappy 
How long have you been very unhappy ?  
> six months. 
Please go on. 
> can you help me? 
What makes you think I can help you ?  
> you remind me of my father 
Please tell me more about father .  
> i like teasing father 
Does anyone else in your family like teasing father ? 
> no, only me
Please go on. 
> bye
Goodby. I hope I have helped you. 

The programm tries to find a suitable answer by pattern matching:

pattern( [i, am, 1], ['How long have you been', 1, ?]).

The number 1 is a place holder, that stands for arbitrary many words.

Solution 14.3