DCG prolog 测试几个句子 [英] DCG prolog testing several sentences
问题描述
如果我有下面的一段代码,我如何让它产生Answer= 5 and Answer2= 10?
.我运行目标 ?- test(Data),lpsolve(Data, [Answer1,Answer2]).
If I have the below piece of code, how would I make it produce Answer= 5 and Answer2= 10?
. I run the goal ?- test(Data),lpsolve(Data, [Answer1,Answer2]).
:-use_module(library(clpfd)).
test([the, variable, X, is, five,fullstop,
the,variable, Y, is, ten, fullstop]).
lpsolve(Data, [Answer,Answer2]):- sentence(Answer, Data,[]).
sentence(X) --> nounphrase, verbphrase(X).
nounphrase --> [the], [variable].
verbphrase(X) --> [X], [is], [five],[fullstop], {X = 5}.
sentence(Y) --> nounphrase, verbphrase(Y).
nounphrase --> [the], [variable].
verbphrase(Y) --> [Y], [is], [ten],[fullstop], {Y = 10}.
实际运行且密切相关的程序示例如下:
Example of a program that actually runs and is closely related is the following:
:-use_module(library(clpfd)).
test([the, variable, X, is, five,fullstop]).
lpsolve(Data, Answer):- sentence(Answer, Data,[]).
sentence(X) --> nounphrase, verbphrase(X).
nounphrase --> [the], [variable].
verbphrase(X) --> [X], [is], [five],[fullstop], {X = 5}.
我只有一个句子要测试,目标成功,如下所示.
I have just one sentence to test and the goal succeeds as shown below.
?- test(Data),lpsolve(Data, Answer).
Data = [the, variable, 5, is, five],
Answer = 5.
编辑
我按照第一条评论尝试以下操作:
I try the following as per the first comment:
:-use_module(library(clpfd)).
test([the, variable, x, is, five,fullstop,
the,variable, y, is, ten, fullstop]).
lpsolve(Data, Answer):- sentence(Answer, Data,[]).
sentence(X) --> nounphrase, verbphrase(X).
nounphrase --> [the], [variable].
verbphrase(X) --> [x], [is], [five],[fullstop], {X = 5}.
verbphrase(Y) --> [y], [is], [ten],[fullstop], {Y = 10}.
我得到以下信息:
-? test(Data),lpsolve(Data, Answer).
false.
推荐答案
我不太确定你在这里想做什么,但我觉得你的 DCG 以完全奇怪的方式分解,你可能会从中受益另一种安排方式.
I'm not really sure what you're trying to do here, but I feel your DCG is broken down in completely strange ways and you may benefit from seeing another way to arrange it.
您有一个变量绑定列表,因此您应该已经考虑获取结果列表而不是单个结果:
You have a list of variable bindings, so you should already be thinking in terms of obtaining a list of results rather than a single result:
sentences([S|Ss]) --> sentence(S), sentences(Ss).
sentences([]) --> [].
什么是句子?在你们的简单系统中,它是一个名词短语和一个动词短语.但是您错误地将英语中的名词和动词短语分开,其中像The variable X is 5"这样的句子应该分解为名词短语主语The variable X"和动词短语is 5".理想情况下,动词短语应该进一步分解为动词另一个名词短语,但我们现在忽略了这个细节.我正在寻找动词is"以将其与 Prolog 谓词 =/2
相关联,并在此处处理 fullstop
:
What is a sentence? It is a noun phrase and a verb phrase, in your simple system. But you have broken the noun and verb phrases apart incorrectly for English, where a sentence like "The variable X is 5" should be broken into a noun phrase subject "The variable X" and a verb phrase "is 5". Ideally, the verb phrase should be decomposed further into a verb another noun phrase, but we're ignoring that detail for now. I am looking for the verb "is" to relate it to the Prolog predicate =/2
, and handling fullstop
here:
sentence(N1=N2) --> nounphrase(N1), verbphrase(is, N2), [fullstop].
好的,现在我们需要你的名词短语:
OK, so now we need your noun phrase:
nounphrase(N) --> [the, variable, N].
还有你的动词短语:
verbphrase(is, V) --> [is], value(V).
我只处理两个解码,并且在 DCG 定义中隐式地进行:
I'm only handling the two decodings and I'm doing it implicitly in the DCG definition:
value(5) --> [five].
value(10) --> [ten].
您会发现这适用于您在上面定义的用例:
You'll find this works for the use case you've defined above:
?- phrase(sentences(S), [the,variable,X,is,five,fullstop,the,variable,Y,is,ten,fullstop]).
S = [X=5, Y=10] ;
false.
这篇关于DCG prolog 测试几个句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!