我正在尝试计算所有患者的症状,以计算疾病的确定性因素,但我只是得到每种疾病的一种症状。
结果也显示了一些重复
确定性因素是患者症状的数量/疾病的症状总数:
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('%').
有人可以帮助我吗?
答案 0 :(得分:1)
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(Disease, Symptoms, Num).
应该没用,但它会引入未绑定的变量,这使得任何进一步处理变得更加困难。
您可以考虑查看库(aggregate),在那里您可以找到用于计算解决方案的精心设计的谓词,例如
countSint(Diseases, Symptoms, Patient) :-
aggregate(count, diagnose(Symptoms, Diseases, _), Count),
format('diagnosed:~d for:~w~n', [Count, Patient]).
修改强>
最好将逻辑与表示分开,并在此处获得一些好的反馈我认为你应该从代码中删除写/读,而是显示你关心的一些例子。现在我展示你需要的基本公式,我可以从你的评论中猜出:
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].