Sentence2atoms

Description

A procedure to transform a sentence into a list of atoms.

Source: PROLOG programming for artificial intelligence, 3rd Edition, Harlow, 2001, ISBN 0-201-40375-7.

Download

Program source code: sentence2atoms.pl

Listing

% Figure 6.2  A procedure to transform a sentence into a list of atoms.
 
/*
  Procedure getsentence reads in a sentence and combines the words 
  into a list of atoms. For example
 
    getsentence( Wordlist)
 
  produces
 
    Wordlist = [ 'Mary', was, pleased, to, see, the, robot, fail]
 
  if the input sentence is:
 
    Mary was pleased to see the robot fail.
*/
 
getsentence( Wordlist)  :-
  get0( Char),
  getrest( Char, Wordlist).
 
getrest( 46, [] )  :-  !.         % End of sentence: 46 = ASCII for '.'
 
getrest( 32, Wordlist)  :-  !,    % 32 = ASCII for blank   
  getsentence( Wordlist).         % Skip the blank 
 
getrest( Letter, [Word | Wordlist] )  :-
  getletters( Letter, Letters, Nextchar), % Read letters of current word
  name( Word, Letters),
  getrest( Nextchar, Wordlist).
 
getletters( 46, [], 46)  :-  !.      % End of word: 46 = full stop 
 
getletters( 32, [], 32)  :-  !.      % End of word: 32 = blank 
 
getletters( Let, [Let | Letters], Nextchar)  :-
   get0( Char),
   getletters( Char, Letters, Nextchar).

Comments

pl/prolog/pllib/sentence2atoms.txt · ostatnio zmienione: 2017/07/17 08:08 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0