DCG序言测试几个句子
DCG prolog testing several sentences
如果我有下面这段代码,我将如何让它产生 Answer= 5 and Answer2= 10?
。我运行目标?- 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}.
实际上 运行 密切相关的程序示例如下:
:-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}.
我只有一句话要测试,目标成功如下图。
?- test(Data),lpsolve(Data, Answer).
Data = [the, variable, 5, is, five],
Answer = 5.
编辑
我根据第一条评论尝试以下操作:
:-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 以完全奇怪的方式分解,你可能会从另一种方式安排它中获益。
您有一个变量绑定列表,因此您应该已经在考虑获取结果列表而不是单个结果:
sentences([S|Ss]) --> sentence(S), sentences(Ss).
sentences([]) --> [].
什么是句子?在您的简单系统中,它是一个名词短语和一个动词短语。但是对于英语,您错误地将名词和动词短语分开,其中像 "The variable X is 5" 这样的句子应该分成名词短语主语 "The variable X" 和动词短语 "is 5"。理想情况下,动词短语应该进一步分解为一个动词另一个名词短语,但我们暂时忽略这个细节。我正在寻找动词 "is" 将其与 Prolog 谓词 =/2
相关联,并在此处处理 fullstop
:
sentence(N1=N2) --> nounphrase(N1), verbphrase(is, N2), [fullstop].
好的,现在我们需要你的名词短语:
nounphrase(N) --> [the, variable, N].
你的动词短语:
verbphrase(is, V) --> [is], value(V).
我只处理这两个解码,并且我在 DCG 定义中隐式地进行:
value(5) --> [five].
value(10) --> [ten].
您会发现这适用于您在上面定义的用例:
?- phrase(sentences(S), [the,variable,X,is,five,fullstop,the,variable,Y,is,ten,fullstop]).
S = [X=5, Y=10] ;
false.
如果我有下面这段代码,我将如何让它产生 Answer= 5 and Answer2= 10?
。我运行目标?- 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}.
实际上 运行 密切相关的程序示例如下:
:-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}.
我只有一句话要测试,目标成功如下图。
?- test(Data),lpsolve(Data, Answer).
Data = [the, variable, 5, is, five],
Answer = 5.
编辑
我根据第一条评论尝试以下操作:
:-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 以完全奇怪的方式分解,你可能会从另一种方式安排它中获益。
您有一个变量绑定列表,因此您应该已经在考虑获取结果列表而不是单个结果:
sentences([S|Ss]) --> sentence(S), sentences(Ss).
sentences([]) --> [].
什么是句子?在您的简单系统中,它是一个名词短语和一个动词短语。但是对于英语,您错误地将名词和动词短语分开,其中像 "The variable X is 5" 这样的句子应该分成名词短语主语 "The variable X" 和动词短语 "is 5"。理想情况下,动词短语应该进一步分解为一个动词另一个名词短语,但我们暂时忽略这个细节。我正在寻找动词 "is" 将其与 Prolog 谓词 =/2
相关联,并在此处处理 fullstop
:
sentence(N1=N2) --> nounphrase(N1), verbphrase(is, N2), [fullstop].
好的,现在我们需要你的名词短语:
nounphrase(N) --> [the, variable, N].
你的动词短语:
verbphrase(is, V) --> [is], value(V).
我只处理这两个解码,并且我在 DCG 定义中隐式地进行:
value(5) --> [five].
value(10) --> [ten].
您会发现这适用于您在上面定义的用例:
?- phrase(sentences(S), [the,variable,X,is,five,fullstop,the,variable,Y,is,ten,fullstop]).
S = [X=5, Y=10] ;
false.