2

我在创建问题答案 Prolog 文件时遇到问题。我有一个包含位置的数据库,我已经可以得到问题并写出答案。但是有不同类型的对象,需要不同的前缀。所以我为前缀定义了 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] 

这适用于正确验证输入,但它似乎对输出无用。我怎样才能只取变量而不取子句?

4

1 回答 1

3

OK, I tried to translate the whole program to a more or less usefull and working example.

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]).

Result:

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

I think, what I looked for was a construction like: article(G,Art,[]) to determine the depending DCG variable... actually I do not fathom yet how the two last arguments are working...

于 2012-05-30T21:46:16.267 回答