gpt4 book ai didi

带有变量的 Prolog DCG

转载 作者:行者123 更新时间:2023-12-03 23:52:06 29 4
gpt4 key购买 nike

我有两个人的 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只是因为我在前一句话中已经提到了彼得(男性)。否则它应该返回 false .

本题使用与 here相同的例子.

最佳答案

您可以增加 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'我不确定这是故意的。

关于带有变量的 Prolog DCG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56167657/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com