// Abstract ------------------------------------------------------------------------------------------------------------------------------

This sample is a simple lung diagnosis expert system, based on Vincent Mosoti's lung_diagnosis.pl

// Examples ------------------------------------------------------------------------------------------------------------------------------

?- #diagnose([fever],:d)
-> ( tuberculosis ) := 0.14 (0.005) 1
-> ( pneumonia ) := 0.25 (0.005) 2
-> ( sarcoidosis ) := 0.12 (0.007) 3
-> ( bronchiolitis ) := 0.25 (0.007) 4
-> ( influenza ) := 0.17 (0.008) 5
-> ( lung_cancer ) := 0.10 (0.008) 6
?- #diagnose([fever,persistent_cough],:d)
-> ( tuberculosis ) := 0.29 (0.006) 1
-> ( pneumonia ) := 0.12 (0.008) 2
-> ( sarcoidosis ) := 0.06 (0.011) 3
-> ( bronchiolitis ) := 0.12 (0.013) 4
-> ( influenza ) := 0.08 (0.013) 5
-> ( lung_cancer ) := 0.05 (0.013) 6
?- #diagnose([wheezing, cough, fever, chest_tightness, shortness_of_breath],:d)
-> ( tuberculosis ) := 0.03 (0.016) 1
-> ( pneumonia ) := 0.45 (0.017) 2
-> ( byssinosis ) := 0.60 (0.017) 3
-> ( pneumoconiosis ) := 0.10 (0.019) 4
-> ( sarcoidosis ) := 0.10 (0.019) 5
-> ( asbestosis ) := 0.20 (0.020) 6
-> ( asthma ) := 0.80 (0.020) 7
-> ( bronchiolitis ) := 0.20 (0.020) 8
-> ( influenza ) := 0.03 (0.020) 9
-> ( lung_cancer ) := 0.32 (0.020) 10
 
// Code ----------------------------------------------------------------------------------------------------------------------------------

disease { // hypothesis for each disease

(tuberculosis,      [persistent_cough, constant_fatigue, weight_loss, lack_of_appetite, fever, coughing_blood, night_sweats]);
(pneumonia,         [cough, fever, shaking_chills, shortness_of_breath]);
(byssinosis,        [chest_tightness, cough, wheezing]);
(pertusis,          [runny_nose, mild_fever]);
(pneumoconiosis,    [chronic_cough, shortness_of_breath]);
(sarcoidosis,       [dry_cough, shortness_of_breath, mild_chest_pain, scaly_rash, fever, red_bumps_on_legs, sore_eyes, swollen_ankles]);
(asbestosis,        [chest_tightness, shortness_of_breath, chest_pain, lack_of_appetite]);
(asthma,            [wheezing, cough, chest_tightness, shortness_of_breath]);
(bronchiolitis,     [wheezing, fever, blue_skin, rapid_breath]);
(influenza,         [headache, fever, shaking_chills, nasal_congestion, runny_nose, sore_throat]);
(lung_cancer,       [cough, fever, hoarseness, chest_pain, wheezing, weight_loss, lack_of_appetite, coughing_blood, headache, shortness_of_breath]);

}

diagnose.score { // compute a score from the lists matching score

(:l,:s,:o) :- mul(:l,:s,:o); // simply multiply bothe value

}

diagnose { // diagnose the list of symptoms

(:symptoms, :disease)   :-  lst.length(:symptoms,:symptoms.l),          // get the length of the patient's symptoms
                            #disease(:disease,:list),                   // query for the disease
                            lst.length(:list,:list.l),                  // get the length of the symptoms for the disease's list
                            #lst.matches(:list,:symptoms,:matches),     // get the number of symptoms that matches
                            div(:matches,:symptoms.l,:symptoms.s),      // get % of matches in the patient's symptoms
                            div(:matches,:list.l,:list.s),              // get * of matches in the disease's symptoms
                            #diagnose.score(:list.s,:symptoms.s,:score),// score the disease
                            fuzz(:score);                               // use the score as the value of the inference

}


lst.matches { // count the number of matching items between two lists

(_,[],0)^                           :- true;
(:l,[:e?[lst.member(:l)]],1)^       :- true;
(_,[:e],0)^                         :- true;
(:l,[:h?[lst.member(:l)]|:r],:c)^   :- #lst.matches(:l,:r,:c1), add(:c1,1,:c);
(:l,[:h?[lst.except(:l)]|:r],:c)^   :- #lst.matches(:l,:r,:c);

}

// ---------------------------------------------------------------------------------------------------------------------------------------

[Home] [Email] [Twitter] [LinkedIn]