带变量的 Prolog DCG
Prolog DCG with variables
我有两个人的DCG句子,代表一男一女。我想用 "he" 或 "she" 来指代上一句中提到的人。
假设我们有这些 DCG:
father --> [Peter].
mother --> [Isabel].
child --> [Guido].
child --> [Claudia].
verb --> [is].
relation --> [father, of].
relation --> [mother, of].
pronoun --> [he].
pronoun --> [she].
adjective --> [a, male].
adjective --> [a, female].
s --> father, verb, relation, child.
s --> mother, verb, relation, child.
s --> pronoun, verb, adjective.
正在查询 ?- s([Peter, is, father, of, Guido], []).
returns true
.
我如何才能确保现在查询 ?- s([he, is, a, male], []).
时应该 return true
只是因为我在上一句中已经提到了 Peter(男性)。否则应该 return false
.
此题使用与相同的例子。
您可以增强您的 DCG 以保持某种状态(最后一句话的性别):
father --> ['Peter'].
mother --> ['Isabel'].
child --> ['Guido'].
child --> ['Claudia'].
verb --> [is].
relation --> [father, of].
relation --> [mother, of].
pronoun(he) --> [he].
pronoun(she) --> [she].
adjective --> [a, male].
adjective --> [a, female].
s(G) --> s(none,G).
s(_,he) --> father, verb, relation, child.
s(_,she) --> mother, verb, relation, child.
s(G,G) --> pronoun(G), verb, adjective.
现在您可以使用此状态链接查询:
?- phrase(s(G1),['Peter', is, father, of, 'Guido']), phrase(s(G1,G2),[he, is, a, male]).
G1 = G2, G2 = he
您可能需要稍微修改 DCG 以约束关系(使用性别参数)。例如,您的 DCG 目前接受 'Peter' is mother of 'Guido'
,我不确定这是有意的。
我有两个人的DCG句子,代表一男一女。我想用 "he" 或 "she" 来指代上一句中提到的人。
假设我们有这些 DCG:
father --> [Peter].
mother --> [Isabel].
child --> [Guido].
child --> [Claudia].
verb --> [is].
relation --> [father, of].
relation --> [mother, of].
pronoun --> [he].
pronoun --> [she].
adjective --> [a, male].
adjective --> [a, female].
s --> father, verb, relation, child.
s --> mother, verb, relation, child.
s --> pronoun, verb, adjective.
正在查询 ?- s([Peter, is, father, of, Guido], []).
returns true
.
我如何才能确保现在查询 ?- s([he, is, a, male], []).
时应该 return true
只是因为我在上一句中已经提到了 Peter(男性)。否则应该 return false
.
此题使用与
您可以增强您的 DCG 以保持某种状态(最后一句话的性别):
father --> ['Peter'].
mother --> ['Isabel'].
child --> ['Guido'].
child --> ['Claudia'].
verb --> [is].
relation --> [father, of].
relation --> [mother, of].
pronoun(he) --> [he].
pronoun(she) --> [she].
adjective --> [a, male].
adjective --> [a, female].
s(G) --> s(none,G).
s(_,he) --> father, verb, relation, child.
s(_,she) --> mother, verb, relation, child.
s(G,G) --> pronoun(G), verb, adjective.
现在您可以使用此状态链接查询:
?- phrase(s(G1),['Peter', is, father, of, 'Guido']), phrase(s(G1,G2),[he, is, a, male]).
G1 = G2, G2 = he
您可能需要稍微修改 DCG 以约束关系(使用性别参数)。例如,您的 DCG 目前接受 'Peter' is mother of 'Guido'
,我不确定这是有意的。