更改 Prolog 中的输出 - 用于检测 Covid 症状

问题描述

我想问你是否可以建议我如何创建条件或重做代码。我需要程序的输出来检测 covid19 的症状。如果我在程序中的 10 个症状中至少标记了 3 次“是”,则输出将是“您有 covid19 症状”。如果“是”少于 3 次,则输出将是“您没有 COVID19 症状”。 谢谢

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),( (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 again\n",[]),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.

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...