P. 1
Prolog and Natural-Language Analysis

Prolog and Natural-Language Analysis

|Views: 136|Likes:
Published by manfromglad
Fernando C. N. Pereira and Stuart M. Shieber
Fernando C. N. Pereira and Stuart M. Shieber

More info:

Published by: manfromglad on Jun 22, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





Using these commands, we can develop a simple user interface for the talk pro-
gram. First, we need a program to read sentences from the terminal. The predicate
read_sentwill returna list ofwords whichweretypedseparatedbyspaces andended
by a newline character.

read_sent(Words) :-


read_sent(Char, Words).

It gets the next character from the input stream as a lookahead and, depending on the
lookahead character, either continues reading or stops. If the lookahead is a newline,
input is ended.

read_sent(C, []) :- newline(C), !.

5.3. User Interaction

This digital edition of Prolog and Natural-Language Analysis is distributed
at no charge for noncommercial use by Microtome Publishing.


If the lookahead is a space, it is ignored, as spaces are not part of the next word.

read_sent(C, Words) :- space(C), !,


read_sent(Char, Words).

Any other character is assumed to start a word. The auxiliary predicate read_word is
called to retrieve the characters that constitute the next word from the input. Then the
built-in predicate name packs this list of characters into an atom. Finally, more words
are read from the input.

read_sent(Char, [Word|Words]) :-

read_word(Char, Chars, Next),

name(Word, Chars),

read_sent(Next, Words).

Reading a word from the input stream proceeds similarly. The predicate
read_word takes the lookahead character and builds a list of characters starting with
the lookaheadthat comprise the word. The new lookahead,the delimiter following the
word, is matched with the third argument to read_word. Newlines and spaces delimit
the words.

read_word(C, [], C) :- space(C), !.

read_word(C, [], C) :- newline(C), !.

All other characters are added to the list of characters to be formed into a word.

read_word(Char, [Char|Chars], New) :-


read_word(Next, Chars, New).

Using read_sent, we can write a top-level loop that reads sentences, computes
the appropriate reply with talk, and prints that reply.

main_loop :-

write(’>> ’),


talk(Words, Reply),



The final recursive call of main_loop starts the read-compute-printloop over again.
Finally,aprogramtoprinttherepliesina moresatisfactoryfashionwas assumedin
the definition of main_loop. This predicate, print_reply, is listed in the Appendix
A code. As it presents no interesting problems, its definition is not repeated here.
Interacting with the talk program through this interface, although not ideal, is
considerably more natural as evidenced by the dialogue presented at the beginning of
Section 5.2. The full commentedlisting for the talk programis given in AppendixA.


A hardbound edition of Prolog and Natural-Language Analysis is available
from www.mtome.com and other booksellers.

Chapter 5. Full Prolog

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->