带变量的Prolog DCG

时间:2019-05-16 11:35:25

标签: prolog dcg

我有两个人的DCG句子,分别代表男性和女性。我想用“他”或“她”指代上一句话中提到的人。

假设我们有这些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], []).会返回true

如何确保当我现在查询?- s([he, is, a, male], []).时应返回true仅仅是因为我已经在上一句中提到了Peter(男性)。否则,它将返回false

此问题使用与here相同的示例。

1 个答案:

答案 0 :(得分:2)

您可以增强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来约束关系(使用Gender参数)。例如,您的DCG当前接受'Peter' is mother of 'Guido',但我不确定这是故意的。