更改Prolog中的输出-用于检测新冠肺炎症状



我想问你是否可以建议我如何创建条件或重做代码。我需要程序的输出来检测新冠肺炎的症状。如果我在程序中的十种症状中至少有三次标记为"是",输出将是"你有新冠肺炎症状"。如果"是"少于3次,输出将为"您没有新冠肺炎症状"。谢谢

start :- x(Disease), write('the verdict is : '), write(Disease),nl, undo.

x(symptoms)   :- symptoms,!.
x(withoutsymptoms).  /* without symptoms */
symptoms :- verify(conjunctivitis),
verify(sore_throat),            
verify(fatigure),
verify(shake),
verify(diarrhea),        
verify(runny_nose),      
verify(cold),
verify(muscle_pain),
verify(chest_pressure),
verify(loss_of_taste),          
verify(fever).
ask(Question) :-
write('How are you? '),
write(Question),
write('? '),
read(Response),
nl,
( (Response == yes ; Response == yes)
->
assert(yes(Question)) ;
assert(no(Question)), fail).
:- dynamic yes/1,no/1.
verify(S) :-
(yes(S)
->
true ;
(no(S)
->
fail ;
ask(S))).
undo :- retract(yes(_)),fail.
undo :- retract(no(_)),fail.
undo.

这种尝试看起来令人困惑。下面的内容如何?它另外不使用assertz/1来戳Prolog数据库,而是将响应收集在列表中以供稍后分析。

此外,代替使用";术语解串器";read/1,我们使用read_line_to_string/2

% ---
% Symptoms are listed naturally in a list.
% There are 11 symptoms
symptoms( [conjunctivitis,
sore_throat,
fatigue,
shake,
diarrhea,        
runny_nose,      
cold,
muscle_pain,
chest_pressure,
loss_of_taste,          
fever] ).
% ---
% Go through the symptoms list, collecting answers
ask_about_symptoms([],[]).
ask_about_symptoms([Symptom|MoreSymptoms],[Answer-Symptom|MorePairs]) :-
repeat,
format("Do you have: ~s?n",[Symptom]),
read_line_to_string(user_input,S1),
string_lower(S1,S2),
(
member(S2,["yes","y","ja","oui"])
->
Answer = yes
;
member(S2,["no","n","nein","non"])
->
Answer = no
;
format("Please try againn",[]),fail % Back to "repeat"
),   
ask_about_symptoms(MoreSymptoms,MorePairs).

is_yes_pair(yes-_).
ask_candidate :-
symptoms(Symptoms),
ask_about_symptoms(Symptoms,AnswerPairs),
!, % don't go back to asking
include(is_yes_pair,AnswerPairs,Included),
format("So you say you have the following symptoms: ~q~n",[Included]),
length(Included,Hits),
((Hits>=3) -> format("Looks bad.~n",[]) ; format("You probably don't have it.~n",[])).

示例运行:

?- [rona].
true.
?- ask_candidate.
Do you have: conjunctivitis?
|: y
Do you have: sore_throat?
|: y
Do you have: fatigue?
|: n
Do you have: shake?
|: n
Do you have: diarrhea?
|: n
Do you have: runny_nose?
|: y
Do you have: cold?
|: foo
Please try again
Do you have: cold?
|: bar
Please try again
Do you have: cold?
|: n
Do you have: muscle_pain?
|: n
Do you have: chest_pressure?
|: y
Do you have: loss_of_taste?
|: y
Do you have: fever?
|: y
So you say you have the following symptoms: [yes-conjunctivitis,yes-sore_throat,yes-runny_nose,yes-chest_pressure,yes-loss_of_taste,yes-fever]
Looks bad.
true.

最新更新