【问题标题】:what mistake in this medical diagnosis prolog?这个医学诊断序言有什么错误?
【发布时间】:2018-10-05 22:10:03
【问题描述】:

我还是 prolog 的新手,我对使用 prolog 的简单医疗诊断程序有疑问,我试图找到有关此医疗诊断系统的代码以使其工作,但我不能,我不知道我需要什么改变,我使用 SWI prolog 软件。

这里是我尝试使用的序言代码 它在域中显示错误,显示“syntax error= operator expected”

domains
disease,indication = symbol
Patient,name = string

predicates
hypothesis(string,disease)
symptom(name,indication)
response(char)
go
clauses

go :-
    write("What is the patient's name? "),
    readln(Patient),
    hypothesis(Patient,Disease),
    write(Patient,"probably has ",Disease,"."),nl.
go :-
    write("Sorry, I don't seem to be able to"),nl,
    write("diagnose the disease."),nl.

symptom(Patient,fever) :-
    write("Does ",Patient," have a fever (y/n) ?"),
    response(Reply),
    Reply='y'.
symptom(Patient,rash) :-
    write("Does ",Patient," have a rash (y/n) ?"),
    response(Reply),
    Reply='y'.
symptom(Patient,headache) :-
    write("Does ",Patient," have a headache (y/n) ?"),
    response(Reply),
    Reply='y'.
symptom(Patient,runny_nose) :-
    write("Does ",Patient," have a runny_nose (y/n) ?"),
    response(Reply),
    Reply='y'.
symptom(Patient,conjunctivitis) :-
    write("Does ",Patient," have a conjunctivitis (y/n) ?"),
    response(Reply),
    Reply='y'.
symptom(Patient,cough) :-
    write("Does ",Patient," have a cough (y/n) ?"),
    response(Reply),
    Reply='y'.
symptom(Patient,body_ache) :-
    write("Does ",Patient," have a body_ache (y/n) ?"),
    response(Reply),
    Reply='y'.
symptom(Patient,chills) :-
    write("Does ",Patient," have a chills (y/n) ?"),
    response(Reply),
    Reply='y'.
symptom(Patient,sore_throat) :-
    write("Does ",Patient," have a sore_throat (y/n) ?"),
    response(Reply),
    Reply='y'.
symptom(Patient,sneezing) :-
    write("Does ",Patient," have a sneezing (y/n) ?"),
    response(Reply),
    Reply='y'.
symptom(Patient,swollen_glands) :-
    write("Does ",Patient," have a swollen_glands (y/n) ?"),
    response(Reply),
    Reply='y'.

hypothesis(Patient,measles) :-
    symptom(Patient,fever),
    symptom(Patient,cough),
    symptom(Patient,conjunctivitis),
    symptom(Patient,runny_nose),
    symptom(Patient,rash).
hypothesis(Patient,german_measles) :-
    symptom(Patient,fever),
    symptom(Patient,headache),
    symptom(Patient,runny_nose),
    symptom(Patient,rash).
hypothesis(Patient,flu) :-
    symptom(Patient,fever),
    symptom(Patient,headache),
    symptom(Patient,body_ache),
    symptom(Patient,conjunctivitis),
    symptom(Patient,chills),
    symptom(Patient,sore_throat),
    symptom(Patient,runny_nose),
    symptom(Patient,cough).
hypothesis(Patient,common_cold) :-
    symptom(Patient,headache),
    symptom(Patient,sneezing),
    symptom(Patient,sore_throat),
    symptom(Patient,runny_nose),
    symptom(Patient,chills).
hypothesis(Patient,mumps) :-
    symptom(Patient,fever),
    symptom(Patient,swollen_glands).
hypothesis(Patient,chicken_pox) :-
    symptom(Patient,fever),
    symptom(Patient,chills),
    symptom(Patient,body_ache),
    symptom(Patient,rash).
hypothesis(Patient,measles) :-
    symptom(Patient,cough),
    symptom(Patient,sneezing),
    symptom(Patient,runny_nose).

response(Reply) :-
    readchar(Reply),
    write(Reply),nl.

我可以做些什么来让它工作?

【问题讨论】:

  • 您没有使用 SWI-Prolog。您正在使用 Turbo-、PDC 或 Visual Prolog
  • SWI Prolog 无法识别 Turbo/PDC/Visual Prolog 语法。删除所有内容,包括“条款”这一行。将字符串上的引号从 " 更改为 '。并且write 调用必须有机会,因为 ISO write 不接受那么多参数。然后从那里调试。
  • 哦,我不知道每个 prolog 软件之间的区别...好的,现在我明白了,谢谢...
  • 一般来说,尽量避免在你的程序核心中产生副作用。见this related answer

标签: prolog medical


【解决方案1】:

正如 cmets 中所解释的,该代码可能是为与 Prolog 共享功能但不被视为 Prolog 系统的逻辑编程语言编写的(相对于官方和事实上的 Prolog 标准)。

快速重写代码以使其在 SWI-Prolog 上运行是:

go :-
    write('What is the patient''s name? '),
    read(Patient),
    hypothesis(Patient,Disease),
    write_list([Patient,'probably has ',Disease,'.']),nl.

go :-
    write('Sorry, I don''t seem to be able to'),nl,
    write('diagnose the disease.'),nl.

symptom(Patient,fever) :-
    write_list(['Does ',Patient,' have a fever (y/n) ?']),
    response(Reply),
    Reply='y'.

symptom(Patient,rash) :-
    write_list(['Does ',Patient,' have a rash (y/n) ?']),
    response(Reply),
    Reply='y'.

symptom(Patient,headache) :-
    write_list(['Does ',Patient,' have a headache (y/n) ?']),
    response(Reply),
    Reply='y'.

symptom(Patient,runny_nose) :-
    write_list(['Does ',Patient,' have a runny_nose (y/n) ?']),
    response(Reply),
    Reply='y'.

symptom(Patient,conjunctivitis) :-
    write_list(['Does ',Patient,' have a conjunctivitis (y/n) ?']),
    response(Reply),
    Reply='y'.

symptom(Patient,cough) :-
    write_list(['Does ',Patient,' have a cough (y/n) ?']),
    response(Reply),
    Reply='y'.

symptom(Patient,body_ache) :-
    write_list(['Does ',Patient,' have a body_ache (y/n) ?']),
    response(Reply),
    Reply='y'.

symptom(Patient,chills) :-
    write_list(['Does ',Patient,' have a chills (y/n) ?']),
    response(Reply),
    Reply='y'.

symptom(Patient,sore_throat) :-
    write_list(['Does ',Patient,' have a sore_throat (y/n) ?']),
    response(Reply),
    Reply='y'.

symptom(Patient,sneezing) :-
    write_list(['Does ',Patient,' have a sneezing (y/n) ?']),
    response(Reply),
    Reply='y'.

symptom(Patient,swollen_glands) :-
    write_list(['Does ',Patient,' have a swollen_glands (y/n) ?']),
    response(Reply),
    Reply='y'.

hypothesis(Patient,measles) :-
    symptom(Patient,fever),
    symptom(Patient,cough),
    symptom(Patient,conjunctivitis),
    symptom(Patient,runny_nose),
    symptom(Patient,rash).

hypothesis(Patient,german_measles) :-
    symptom(Patient,fever),
    symptom(Patient,headache),
    symptom(Patient,runny_nose),
    symptom(Patient,rash).

hypothesis(Patient,flu) :-
    symptom(Patient,fever),
    symptom(Patient,headache),
    symptom(Patient,body_ache),
    symptom(Patient,conjunctivitis),
    symptom(Patient,chills),
    symptom(Patient,sore_throat),
    symptom(Patient,runny_nose),
    symptom(Patient,cough).

hypothesis(Patient,common_cold) :-
    symptom(Patient,headache),
    symptom(Patient,sneezing),
    symptom(Patient,sore_throat),
    symptom(Patient,runny_nose),
    symptom(Patient,chills).

hypothesis(Patient,mumps) :-
    symptom(Patient,fever),
    symptom(Patient,swollen_glands).

hypothesis(Patient,chicken_pox) :-
    symptom(Patient,fever),
    symptom(Patient,chills),
    symptom(Patient,body_ache),
    symptom(Patient,rash).

hypothesis(Patient,measles) :-
    symptom(Patient,cough),
    symptom(Patient,sneezing),
    symptom(Patient,runny_nose).

write_list([]).
write_list([Term| Terms]) :-
    write(Term),
    write_list(Terms).

response(Reply) :-
    get_single_char(Code),
    put_code(Code), nl,
    char_code(Reply, Code).

为方便起见,此重写使用了 SWI-Prolog 特定的内置谓词 get_single_char/1。如果您想修改代码以在其他 Prolog 系统上运行,则需要替换它的调用。

【讨论】:

  • 为什么是get_single_char/1
  • 正如其文档中所解释的,“这个谓词不等待返回”,这使得它在这个用例中很方便,避免了输入句点并为每个回复点击返回。跨度>
  • 好的,谢谢。这就是为什么我放了一个没有说明这一点的文档的链接......
猜你喜欢
  • 2016-03-04
  • 2018-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多