2012-09-01 168 views
2

我试图计算所有患者的症状来计算疾病的确定性因素,但我只是得到每种疾病的一种症状。
此外,结果显示了一些重复。
的可信度/患者的症状的人数占总数的疾病的症状:
Prolog - 计算所有患者的症状

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('%'). 

谁能帮助我,好吗?

回答

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]. 
+0

谢谢回答,CHAC! 我用聚合,但我仍然没有得到我需要的结果。我需要获取患者与我的数据库中每种疾病症状相同的所有症状的数量。我不知道我做错了什么。 –

+1

@Ítala:编辑你的问题与你的新尝试,如果你想好的建议! :d – m09

+0

@chac谢谢!这正是我需要:) –