gpt4 book ai didi

prolog - 创建一个序言查询和回答系统

转载 作者:行者123 更新时间:2023-12-02 03:03:34 24 4
gpt4 key购买 nike

我正在学习序言,但一直被一个问题困住了。我正在制作一个问答系统。
例如,当我键入“汽车的颜色是蓝色”时。程序会说“确定”并添加新规则,所以当被问及“汽车的颜色是什么?”时它会以蓝色响应。
如果我说“汽车的颜色是绿色的”,它会回答“不是”。但是每当我输入“汽车的颜色是蓝色”时,它会为问题版本返回 true,false。有人可以指导在哪里走得更远吗?我不知道如何让程序说“它是蓝色的”或其他什么

 input :-
read_line_to_codes(user_input, Input),
string_to_atom(Input,Atoms),
atomic_list_concat(Alist, ' ', Atoms),
phrase(sentence(S), Alist),
process(S).

statement(Statement) --> np(Description), np(N), ap(A),
{ Statement =.. [Description, N, A]}.

query(Fact) --> qStart, np(A), np(N),
{ Fact =.. [A, N, X]}.


np(Noun) --> det, [Noun], prep.
np(Noun) --> det, [Noun].

ap(Adj) --> verb, [Adj].
qStart --> adjective, verb.

vp --> det, verb.

adjective --> [what].
det --> [the].

prep --> [of].

verb -->[is].


%% Combine grammar rules into one sentence
sentence(statement(S)) --> statement(S).
sentence(query(Q)) --> query(Q).
process(statement(S)) :- asserta(S).
process(query(Q)) :- Q.

最佳答案

你真的很接近。看看这个:

?- phrase(sentence(Q), [what,is,the,color,of,the,car]).
Q = query(color(car, _6930)) ;
false.

您已成功将句子解析为查询。现在让我们处理它:

?- phrase(sentence(Q), [what,is,the,color,of,the,car]), process(Q).
Q = query(color(car, 'blue.')) ;
false.

如你所见,你妥善统一了。完成后,您只是没有对它做任何事情。我认为您需要做的就是将 process/1 的结果传递给显示结果的东西:

display(statement(S)) :- format('~w added to database~n', [S]).
display(query(Q)) :- Q =.. [Rel, N, X], format('the ~w has ~w ~w~n', [N, Rel, X]).

并修改input/0传递给display/1谓词:

input :-
read_line_to_codes(user_input, Input),
string_to_atom(Input,Atoms),
atomic_list_concat(Alist, ' ', Atoms),
phrase(sentence(S), Alist),
process(S),
display(S).

现在您在使用它时会得到一些结果:

?- phrase(sentence(Q), [what,is,the,color,of,the,car]), process(Q), display(Q).
the car has color blue.
Q = query(color(car, 'blue.')) ;
false.

?- phrase(sentence(Q), [the,siding,of,the,car,is,steel]), process(Q), display(Q).
siding(car,steel) added to database
Q = statement(siding(car, steel)) ;
false.

?- phrase(sentence(Q), [what,is,the,siding,of,the,car]), process(Q), display(Q).
the car has siding steel
Q = query(siding(car, steel)) ;
false.

关于prolog - 创建一个序言查询和回答系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44236359/

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