2012-05-30 36 views
2

我有一個問題,同時創建一個問題答案Prolog文件。我有一個包含位置的數據庫,我已經可以找到問題並寫出答案。但是有不同類型的對象,需要不同的前綴。所以我定義了DCG作爲前綴。DCG輸出單個變量而不是整個子句在列表中

answer(P,A) :- location(P, Str, Nr),A = [there, is, article(G,K,N,P), noun(G,K,N,P), pre(P),Str,Nr]. 
question(A) --> questionsentence(P),{answer(P,A)}. 

pre(P) --> [in, P], {member(P, [road66])}. 
pre(P) --> [at, P], {member(P, [trafalgarsquare])}. 

但我得到的是這樣的:

?-question(A, [where,is,a,kentuckys],[]). 
A = [there, is, article(_G2791, _G2792, _G2793, kentuckys), noun(_G2791, _G2792, _G2793, kentuckys), prep(kentuckys), road66, 123] 

這適用於正確驗證輸入,但它似乎是無用的輸出。我怎樣才能把變量而不是條款?

+0

你的例子似乎是不完整的:'questionsentence'和'pre'之間沒有關係,'location'丟失,並且' A'只是一個術語,而不是呼叫謂詞... –

+0

對不起,我只是粘貼一小段代碼,因爲整個程序都是用德語寫的,我懶得翻譯所有的東西......但是,我重構了一下,解決了我的問題。 – abimelex

回答

3

好吧,我試着將整個程序翻譯成一個或多或少有用的工作示例。

location(kentuckys, road66, 121). 
location(soliver, trafalgarsquare, 15). 
location(marcopolo, trafalgarsquare, 15). 
location(internist, jumpstreet, 21). 

questionsentence(P,G) --> [where],[is], article(G), noun(G,P). 
answer(P,A,G) :- location(P, Str, Nr), prep(W,Str), article(G,Art,[]), flatten([there, is, Art, P, W, Str,Nr], A). 
question(A) --> questionsentence(P,G),{answer(P,A,G)}. 

article(m) --> [a]. 
article(f) --> [an]. 
noun(m, P) --> [P], {member(P, [kentuckys, soliver, marcopolo])}. 
noun(f, P) --> [P], {member(P, [internist])}. 
prep([at],Str) :- member(Str, [trafalgarsquare, road66]). 
prep([in],Str) :- member(Str, [jumpstreet]). 

結果:

?- question(A, [where,is,a,kentuckys],[]). 
A = [there, is, a, kentuckys, at, road66, 121] . 

我想,我看了很喜歡​​建築:文章(G,藝術,[])來確定取決於DCG變量...其實我不最後兩個參數是如何工作的...

相關問題