我正在为我的研究做Prolog编程。
hypothesis(Patient,cold,Score) :-
symptom(Patient,fever) -> Score is 10; Score is 0.
hypothesis(Patient,severe_cold,Score) :-
symptom(Patient,fever) -> Score1 is 10; Score1 is 0,
symptom(Patient, runny_nose) -> Score2 is 20; Score2 is 0, Score is Score1 + Score2
.
我想得到所有假设的分数。也就是说,如果症状(患者,发烧)被检查为好,那么我想获得sever_cold(得分10)和冷(得分10)两者的得分。再举一个例子,如果症状(Patient,runny_nose)被检查为ok,那么我想得到severe_cold的得分(得分20)。但是Prolog不起作用......
这是我第一次使用Prolog。处理起来太麻烦了。
的 的 ** 被修改的 * ** * ** 实际上,我的所有代码如下所示。
%% Lines are without period(.)
diagnosis :-
readln(Line1),
readln(Line2),
readln(Line3),
readln(Line4),
readln(Line5),
readln(Line6),
readln(Line7),
readln(Line8),
readln(Line9),
readln(Line10),
write(Line1),nl,
write(Line2),nl,
write(Line3),nl,
write(Line4),nl,
write(Line5),nl,
write(Line6),nl,
write(Line7),nl,
write(Line8),nl,
write(Line9),nl,
write(Line10),nl.
%% (get_symptom(Line1,[man]) -> write('man!!!!!!!!!')),
%% (get_symptom(Line2,[woman]) -> write('woman!!!!!!!!!')).
%% if A then B else C, (A->B; C)
%% grammar
s --> np, vp.
np --> det, n.
vp --> v, np.
det --> [a].
n --> [man].
v --> [has].
n --> [woman].
n --> [fever].
n --> [runny_nose].
get_symptom(Line,[N]) :- s(Line,[]), n([N],[]).
%% FindSymptom(Line, [Symptom]) : - s(Line,[]), np(_, _, object,[a,
%% Symptom]), n(singular, [Symptom], []).
start :-
write('What is the patient''s name? '),
readln(Patient), %% Here, this can be used for input of all symtoms
diagnosis,
hypothesis1(Patient,cold,S1),
append([cold/S1/red],[],N1), write(S1),
write('until...'),
hypothesis2(Patient,severe_cold,S2), write(S2),
append([severe_cold/S2/red],N1,BarList),
write('until...'),
%% write(Patient,"probably has ",Disease,"."),nl.
hypothesis(Disease),
write(Patient),
write(' probably has '),
write(Disease),
write('.'),
test_barchart(BarList).
start :-
write('Sorry, I don''t seem to be able to'),nl,
write('diagnose the disease.'),nl.
symptom(Patient,fever) :-
get_symptom(Line1, [fever]);
get_symptom(Line2, [fever]);
get_symptom(Line3, [fever]);
get_symptom(Line4, [fever]);
get_symptom(Line5, [fever]);
get_symptom(Line6, [fever]);
get_symptom(Line7, [fever]);
get_symptom(Line8, [fever]);
get_symptom(Line9, [fever]);
get_symptom(Line10, [fever]).
symptom(Patient,runny_nose) :-
get_symptom(Line1, [runny_nose]);
get_symptom(Line2, [runny_nose]);
get_symptom(Line3, [runny_nose]);
get_symptom(Line4, [runny_nose]);
get_symptom(Line5, [runny_nose]);
get_symptom(Line6, [runny_nose]);
get_symptom(Line7, [runny_nose]);
get_symptom(Line8, [runny_nose]);
get_symptom(Line9, [runny_nose]);
get_symptom(Line10, [runny_nose]).
hypothesis1(Patient,cold,Score_Cold) :-
symptom(Patient,fever) -> Score_Cold is 100; Score_Cold is 0.
hypothesis2(Patient,severe_cold,Score_Severe) :-
(symptom(Patient,fever) -> Score1 is 50; Score1 is 0),
(symptom(Patient, runny_nose) -> Score2 is 50; Score2 is 0),
Score_Severe is Score1 + Score2.
hypothesis(Disease) :-
(hypothesis1(Patient,cold,Score1),
Score1 =:= 100 -> Disease = cold);
(hypothesis2(Patient,severe_cold,Score2),
Score2 =:= 100 -> Disease = severe_cold).
%% make graph for the result
:- use_module(library(pce)).
:- use_module(library(plot/barchart)).
:- use_module(library(autowin)).
test_barchart(BarList):-
new(W, picture),
send(W, display, new(BC, bar_chart(vertical,0,100))),
forall(member(Name/Height/Color,
BarList),
( new(B, bar(Name, Height)),
send(B, colour(Color)),
send(BC, append, B)
)),
send(W, open).
%% [X/100/red, y/150/green, z/80/blue, v/50/yellow]
%% append List
append([], L, L).
append([H|T], L2, [H|L3]):-
append(T, L2, L3).
在这里,当我测试我的程序时......
1 ?- start.
What is the patient's name? GJ
|: a man has a fever
|: wow
|: great
|: good
|: nice
|: nothing
|: wow
|: wow
|: wow
|: wow
100until...100until...[GJ] probably has cold.
true
如图所示,症状只有“发烧”..但所有得分都是100 ......发生了什么?
答案 0 :(得分:2)
只需添加所需的括号(注意运营商订单!)并声明您想要的内容。总和,我想?
hypothesis(Patient,severe_cold,Score) :-
(symptom(Patient,fever) -> Score1 is 10; Score1 is 0),
(symptom(Patient, runny_nose) -> Score2 is 20; Score2 is 0),
Score is Score1 + Score2.
在阅读修正后的问题后,编辑,似乎你已经超过了工程学。
只是分别说明每个假设,而Prolog将在回溯时提供所有解决方案。然后findall / 3将很容易获得所有对Symptom-Score的列表,如果需要列表进行进一步处理。