创建Prolog查询和应答系统
我正在学习prolog,我一直被困在一个问题上。我正在制作问答系统。创建Prolog查询和应答系统,prolog,Prolog,我正在学习prolog,我一直被困在一个问题上。我正在制作问答系统。 例如,当我输入“汽车的颜色是蓝色”时,程序会说“OK”并添加新规则,因此当被问到“汽车的颜色是什么?”时,它会以蓝色回应。 如果我说“车的颜色是绿色的”,它会回答“不是。” 但每当我输入“汽车的颜色是蓝色”时,问题版本就会返回true,false。有人能指点下往哪里走吗?我不知道如何让程序说“它的蓝色”或其他什么 input :- read_line_to_codes(user_input, Input),
例如,当我输入“汽车的颜色是蓝色”时,程序会说“OK”并添加新规则,因此当被问到“汽车的颜色是什么?”时,它会以蓝色回应。
如果我说“车的颜色是绿色的”,它会回答“不是。” 但每当我输入“汽车的颜色是蓝色”时,问题版本就会返回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.
你没有追踪你的查询,看看有没有错?而不是
?-查询。
只需编写?-跟踪、查询。
并逐步完成即可。但是请不要忽略警告,我知道它们只是警告,但是如果你忽略警告,甚至忽略错误,然后你来到Stackoverflow,那么如果有人警告你犯了错误,有什么区别?你有几个单变量警告和一个语法错误,因此,在取得进一步进展之前,需要解决这些问题。我已经删除了错误,除了描述,因为我不确定应该将其更改为什么。我不需要那个变量。有人能从我最初的问题中给我指点方向吗?