% File : nlproj.pl % Author : Alan Bundy ? % Written: ? % Updated: 24 January 1985 % Purpose: a simple RTN grammar sentence generator for AI1 project % Taken from file rtn.pl:- /* generation of random sentences from an rtn */ /* use with random and drinks */ /*top level*/ go :- generate(sentence,start), /* generate a sentence from start */ nl,nl. /* and print 2 new lines */ generate(RTN,finish). /* if at finish then do nothing */ generate(RTN,Node):- /* to generate RTN from node */ arc(RTN,Node,Arcs), /* find all arcs leaving node */ random_pick(Arcs,[NewNode,Label,Type]), /* pick one at random*/ traverse(Label,Type), /* traverse it */ generate(RTN,NewNode). /* and carry on from the other end */ traverse(Word,word):- /* to traverse a word arc */ write(Word), tab(1). /* write word and a space */ traverse(SubRTN,rtn) :- /*to traverse an rtn arc */ generate(SubRTN,start). /* generate subrtn from start */ % Taken from file drinks.pl:- /* Example rtn */ /* sentence rtn */ arc(sentence,start,[[a,nounphrase,rtn]]). arc(sentence,a,[[b,verb,rtn]]). arc(sentence,b,[[c,nounphrase,rtn]]). arc(sentence,c,[[finish,stop_mark,rtn]]). /* nounphrase rtn */ arc(nounphrase,start,[[finish,proper_noun,rtn],[a,determiner,rtn]]). arc(nounphrase,a,[[finish,noun,rtn],[a,adjective,rtn]]). /* verb rtn */ arc(verb,start,[[finish,is,word],[finish,drinks,word],[finish,buys,word], [finish,refuses,word],[finish,has,word],[finish,consumes,word]]). /* determiner rtn */ arc(determiner,start,[[finish,a,word],[finish,an,word],[finish,the,word]]). /* noun rtn */ arc(noun,start,[[finish,vicar,word],[finish,policeman,word],[finish,drinks,word] ,[finish,tea,word],[finish,beer,word],[finish,drunk,word],[finish,orange,word], [finish,sherry,word]]). /* adjective rtn */ arc(adjective,start,[[finish,happy,word],[finish,iced,word],[finish,pink,word], [finish,fizzy,word],[finish,orange,word],[finish,drunk,word]]). /* proper noun rtn */ arc(proper_noun,start,[[finish,jane,word],[finish,fred,word],[finish,mr_plod, word],[finish,heineken,word]]). /* stop mark rtn */ arc(stop_mark,start,[[finish,(.),word],[finish,(?),word],[finish,(!),word]]). % Taken from file random.pl:- /* Random number generator */ random_pick(List,El):- /* to pick random element from a list*/ length(List,L), random(L,N), nth(N,List,El). random(Range,Num):- /* to choose random number in range */ seed(Seed), /* get seed from database */ Num is (Seed mod Range) + 1, /* fit seed into range */ retract(seed(Seed)), /* delete old value of seed */ NewSeed is (125*Seed+1) mod 4093, /* calculate new value*/ assert(seed(NewSeed)). /* and assert it into database */ seed(13). /* random number seed database entry */ /* to get the nth element of list */ nth(1,[Hd|T],Hd):- !. /* if n is i then return first element*/ nth(N,[Hd|T],El):- /* otherwise*/ N1 is N-1, /* decrement n by 1 */ nth(N1,T,El). /* and recurse down list */ /* to find the length of a list */ length(List,L):-lengthl(0,List,L). lengthl(Ans,[],Ans). lengthl(L1,[H|T],L2):- L is L1+1, lengthl(L,T,L2).