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:
- The sign ">" is the prompt of the computer to enter a sentence. bye exits the talk shell.
- Use the built-in SWI-Prolog predicate
that reads a line from the console and matches it with String.read_string( current_input, " ", "", End, String)
- Use the built-in SWI-Prolog predicate
string_codes("abc", Chars). => Chars = [97, 98, 99].
- talk is implemented by a failure-driven loop.
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:
- What is the capital of Switzerland?
- capital of Switzerland is what?
- capital of Switzerland?
- . . .
Hints:
- A simple, straightforward approach is to look for patterns "PROPERTY of OBJECT".
- Use the talk shell developed in Exercise 14.1 for the interaction with the user.
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.