List 序言-统计所有患者';s症状
我试图计算所有患者的症状,以计算疾病的确定因素,但我只得到每个疾病的一个症状List 序言-统计所有患者';s症状,list,count,prolog,member,List,Count,Prolog,Member,我试图计算所有患者的症状,以计算疾病的确定因素,但我只得到每个疾病的一个症状 结果还显示了一些重复项。 确定因素是患者症状的数量/疾病症状的总数: start:- write('Enter the name of the patient: '), read(Patient), write('Enter the symptoms: '), read(Symptoms), write('\n'), countSint(Diseases, Symptoms , Patient). co
结果还显示了一些重复项。
确定因素是患者症状的数量/疾病症状的总数:
start:-
write('Enter the name of the patient: '), read(Patient),
write('Enter the symptoms: '), read(Symptoms), write('\n'),
countSint(Diseases, Symptoms , Patient).
countSint(Diseases, Symptoms , Patient) :-
findall(Sint , member(Sint, Symptoms), L1), length(L1 , Size),
( Size < 2
-> writeln('Enter with at least 3 symptoms...\n'), start
; Size > 1
-> write('\n Enter semicolon...:\n\n'), write('Patient: '), write(Patient),
diagnose(Symptoms,Diseases, L)
).
diagnose(Symptoms,Diseases,L) :- search(Symptoms, Diseases, L).
% disease(Disease, Symptoms, Num).
disease(meningitis,[fever, stiff_neck],2).
disease(dengue,[fever, vomiting, red_spots], 3).
% search(Symptoms, Diseases, L).
search([H|T] , Diseases, L) :-
disease(Disease, Symptoms, Num),
Disease0 = [Disease,Diseases],
member(H, Symptoms),
search(T , Diseases0, L),
write('has '), write(Disease), writeln(': '),
setof(H, (disease(Disease, Symptoms, Num),
member(H, Symptoms)), L),
length(L, Size),
calc_cf(Num, Size, R).
calc_cf(Num, Size, R):- % Calculate the certainty factor
R is Size / Num * 100,
write('The certainty factor is '),
write(R),
writeln('%').
开始:-
写(‘输入患者姓名:’),读(患者),
写入('输入症状:')、读取(症状)、写入('\n'),
计数(疾病、症状、患者)。
计数(疾病、症状、患者):-
findall(Sint,构件(Sint,症状),L1),长度(L1,尺寸),
(尺寸<2
->writeln('输入时至少有3个症状…\n'),开始
;尺寸>1
->write('\n输入分号…:\n\n')、write('Patient:')、write(Patient),
诊断(症状、疾病、L)
).
诊断(症状,疾病,L):-搜索(症状,疾病,L)。
%疾病(疾病、症状、数量)。
疾病(脑膜炎,[发烧,颈部僵硬],2)。
疾病(登革热[发烧、呕吐、红斑],3)。
%搜索(症状、疾病、L)。
搜索([H | T],疾病,L):-
疾病(疾病、症状、数量),
疾病0=[疾病,疾病],
成员(H,症状),
搜索(T,疾病0,L),
write('has')、write(Disease)、writeln(':'),
设置(H)(疾病(疾病,症状,数量),
成员(H,症状)),L),
长度(L,尺寸),
计算(数值、大小、R)。
计算(数值、大小、R):-%计算确定系数
R是大小/数量*100,
写入(“确定性因素为”),
写入(R),
writeln(“%”)。
谁能帮帮我吗?这似乎没用:
findall(Sint , member(Sint, Symptoms), L1)
只需将症状改写为L1。为什么?
在这个片段中
( Size < 2
-> writeln('Enter with at least 3 symptoms...\n'), start
; Size > 1
-> write('\n Enter semicolon...:\n\n'), write('Patient: '), write(Patient),
diagnose(Symptoms,Diseases, L)
)
编辑
最好将逻辑和表示分开,并在这里获得一些好的反馈,所以我认为您应该从代码中删除写/读,并显示一些您关心的示例。现在,我展示了您需要的基本公式,正如我从您的评论中可以猜到的:
disease(meningitis, [fever, stiff_neck]).
disease(dengue, [fever, vomiting, red_spots]).
% find diseases from symptoms, sort by certainty factor
diagnose(Symptoms, Diseases) :-
setof(CF-Disease, common_symptoms(Symptoms, Disease, CF), Diseases).
common_symptoms(Symptoms_Patient, Disease, CF) :-
disease(Disease, Symptoms_Disease),
intersection(Symptoms_Patient, Symptoms_Disease, Common_Symptoms),
length(Common_Symptoms, NCS),
length(Symptoms_Disease, NSD),
CF is NCS / NSD * 100.
测试:
谢谢你的回答,查克!我使用了聚合,但仍然没有得到所需的结果。我需要获得患者的所有症状的编号,这些症状与我数据库中每个疾病的症状相同。我不知道我做错了什么。@Í塔拉:如果你想要好的建议,请用你的新尝试编辑你的问题!:d@chac谢谢这正是我需要的:)
disease(meningitis, [fever, stiff_neck]).
disease(dengue, [fever, vomiting, red_spots]).
% find diseases from symptoms, sort by certainty factor
diagnose(Symptoms, Diseases) :-
setof(CF-Disease, common_symptoms(Symptoms, Disease, CF), Diseases).
common_symptoms(Symptoms_Patient, Disease, CF) :-
disease(Disease, Symptoms_Disease),
intersection(Symptoms_Patient, Symptoms_Disease, Common_Symptoms),
length(Common_Symptoms, NCS),
length(Symptoms_Disease, NSD),
CF is NCS / NSD * 100.
?- diagnose([fever, stiff_neck],L).
L = [33.33333333333333-dengue, 100-meningitis].