2013-04-28 47 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