2013-04-28 45 views
1

我是Prolog的新手,我正在编写一个小程序,它会给出一个DCG的随机句子。Prolog - DCG - 随机语句

我以前的想法是用findall/3来列出所有可能的句子,然后使用random_member/2。

它的工作了一小会儿,直到语法越来越大,我开始变得因为递归的堆栈错误...

我当时以为的另一种方式:在给定的做出所有可能的方面一会儿,申请random_member获得下一个学期,递归调用这个相同的功能,直到我得到空的列表...

但我怎样才能得到一个不完整的谓词的所有可能的答案集?我怎样才能得到它在

的信息,我的DCG看起来是这样的:

s --> pronoun(X), verb(X), location. 
pronoun(1) --> [i]. 
pronoun(2) --> [you]. 
verb(1) --> [am]. 
verb(2) --> [are]. 
location --> [here]. 
location --> [there]. 

我的解决方案(其中列表是已经连接在一起的术语列表)的想法:

createRandomSentence(List) :- 
    setof(H, s([List|[H|_]], []), Set), 
    random_member(Pick, Set), 
    append(List, [Pick], List2) 
    <recursive call (haven't figured out this one either yet)> 

...

提前致谢! :)

回答

1

似乎一个虽然任务给我。我会用另一种策略解决它,即在DCG中插入选择器,您需要区分备选方案。像

s --> pronoun(X), verb(X), location. 
pronoun(1) --> {this}, [i]. 
pronoun(2) --> [you]. 
verb(1) --> [am]. 
verb(2) --> [are]. 
location --> {this},[here]. 
location --> [there]. 

% here choice just between 2 
this :- random(0,2,1). 

一些东西,得到

?- phrase(s,L). 
L = [i, am, there] ; 
L = [you, are, there]. 

?- phrase(s,L). 
L = [you, are, there]. 
+0

我不知道这是可能的!非常感谢 ! – Clung 2013-04-28 22:07:49