Article View: comp.lang.prolog
Article #319Re: A phrase generator for Prolog DCG's
From: Thom Fruhwirth
Date: Wed, 13 Sep 1989 21:02
Date: Wed, 13 Sep 1989 21:02
53 lines
2194 bytes
2194 bytes
This is a comment to the posting "ken@aiai.UUCP Tue Sep 12 12:54:10 1989".
Ken Johnson introduced "a phrase generator that generates phrases at random
from a DCG". I like the idea, but I could not resist simplifying the program.
I assume you have the original program, because I only present the part
that has changed. I did not take time to comment the code itself.
Ken Johnson used a 'repeat'-construction and a recursion to make the
deterministic top-level predicate indeterministic. Either the 'repeat'
or the recursion would have been sufficient, I prefer the 'repeat'-
construct, because it is more efficient. Further I renamed gen_phrase/2
into random_phrase/2.
I merged gen_1_phrase/3 into expand/3 making gen_1_phrase/3 obsolete.
The original expand/3 used two difference lists, which is a kind of
overkill, one difference list (now in the second and third argument
of expand/3) suffices.
% Phrase generator: Generate a phrase at random from a simple DCG.
% Ken Johnson 12-9-89, modified by Thom Fruehwirth 13-9-89
% This generator does not understand about arguments to non-terminals,
% the use of { }, or anything else. Don't say I didn't tell you.
% The top level phrase generator: Given a class name it will
% produce a sentence that matches the pattern defined in the DCG.
random_phrase(Class,Sentence):-
functor(Goal,Class,2),
expand(Goal,Sentence,[]).
% Expand a sequence of non-terminals
expand((P,Q),A,C) :-
!,
expand(P,A,B),
expand(Q,B,C).
expand('C'(_,Word,_),[Word|B],B) :-
!.
expand(P,A,B) :-
setof(Body_,clause(P,Body_),Bodies),
pick_one(Bodies,Body),
expand(Body,A,B).
% Pick at random from a list
% I like the code as it is in the original posting, see there
% the end
Technical Details
Message-ID:
<3499@sbcs.sunysb.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!boulder!sunybcs!sbcs!thomReferences:
<873@skye.ed.ac.uk>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!boulder!sunybcs!sbcs!thom From: thom@sbcs.sunysb.edu (Thom Fruhwirth) Newsgroups: comp.lang.prolog Subject: Re: A phrase generator for Prolog DCG's Summary: program code can be simplified Keywords: dcg prolog sentences Message-ID: <3499@sbcs.sunysb.edu> Date: 13 Sep 89 21:02:25 GMT References: <873@skye.ed.ac.uk> Sender: news@sbcs.sunysb.edu Lines: 53