Search code examples
prologdcg

DCG output single variable instead of whole clause in a list


I have a problem, while creating a question answer Prolog file. I have a database with locations and I can already get the question and write the answer out. But there are different types of objects, that require different prefixes. So I defined DCG for prefixes.

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

but what i get is something like this:

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

This works for verifying the input properly, but it seems to be useless for the output. How can I take just the variable and not the clause?


Solution

  • 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...