📄 disease_diagnosis.clp
字号:
;;;======================================================
;;; Medical Diagnosis Expert System
;;; Student no.:MA665272
;;; Name:Derek,Sam Sai Hei
;;;
;;; This expert system diagnoses some disease
;;; such as SARS,Flu-1,Flu-2,Heavy Cold,Normal Cold,
;;; Meningitis and Pneumonia
;;;
;;; To execute, merely load, reset and run.
;;;======================================================
;;****************
;;* DEFFUNCTIONS *
;;****************
(deffunction ask-question (?question $?allowed-values)
(printout t ?question)
(bind ?answer (read))
(if (lexemep ?answer)
then (bind ?answer (lowcase ?answer)))
(while (not (member ?answer ?allowed-values)) do
(printout t ?question)
(bind ?answer (read))
(if (lexemep ?answer)
then (bind ?answer (lowcase ?answer))))
?answer)
(deffunction yes-or-no-p (?question)
(bind ?response (ask-question ?question yes no y n))
(if (or (eq ?response yes) (eq ?response y))
then TRUE
else FALSE))
;;;*******************
;;;* INITIAL QUESTION*
;;;*******************
(defrule startup ""
(initial-fact)
=>
(assert (next "yes"))
(printout t "Input the Symptoms 1.High-Fever,2.Low-Fever,3.Cough Seriously,4.Sputum,"crlf)
(printout t " 5.Snuffle and Drivel,6.Headache,7.Throatache,8.hard to breath,"crlf)
(printout t " 9.Virus in Blood,10.Neck Harden,q.No more Symtoms "crlf)
(bind ?symptoms 11)
(while TRUE
(printout t "Symptom:")
(bind ?symptoms (read))
(if (eq ?symptoms q) then (break))
(switch ?symptoms
(case 1 then (assert (fever-state patient fever-high)))
(case 2 then (assert (fever-state patient fever-low)))
(case 3 then (assert (cough-state patient has-cough)))
(case 4 then (assert (sputum-state patient has-sputum)))
(case 5 then (assert (snuffle-state patient has-snuffle)))
(case 6 then (assert (headache-state patient has-headache)))
(case 7 then (assert (throatache-state patient has-throatache)))
(case 8 then (assert (hard-to-breath-state patient has-hard-to-breath)))
(case 9 then (assert (virus-in-blood-state patient has-virus-in-blood)))
(case 10 then (assert (neck-harden-state patient has-neck-harden)))
(default none ))))
;;;***************
;;;* QUERY RULES *
;;;***************
(defrule determine-fever-state ""
(next ?)
(not (fever-state patient ?))
=>
(bind ?response (ask-question "Do you have fever(low/high/no)?" low high no))
(if (eq ?response no) then (assert (ans "unknown disease"))
else (if (eq ?response low) then (assert (fever-state patient fever-low)))
else (if (eq ?response high) then (assert (fever-state patient fever-high)))))
;;----------For low fever----three diseases:Pneumonia,Normal Cold,Flu-2-----------------------------------
(defrule chain-1-cough-state ""
(fever-state patient fever-low)
(not (cough-state patient ?))
=>
(if (yes-or-no-p "Do you have cough(yes/no)? ") then (assert (cough-state patient has-cough))
else (assert (ans "unknown disease"))))
(defrule chain-1-sputum-state ""
(fever-state patient fever-low)
(cough-state patient has-cough)
(not (sputum-state patient ?))
(not (ans ?))
=>
(if (yes-or-no-p "Do you have sputum(yes/no)? ") then (assert (sputum-state patient has-sputum))
else (assert (ans "unknown disease"))))
(defrule chain-1-snuffle-state ""
(fever-state patient fever-low)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(not (snuffle-state patient ?))
(not (ans ?))
=>
(if (yes-or-no-p "Do you have Snuffle and Drivel(yes/no)? ") then (assert (snuffle-state patient has-snuffle))
else (assert (snuffle-state patient no-snuffle))))
(defrule chain-1-headache-state ""
(fever-state patient fever-low)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient has-snuffle)
(not (headache-state patient ?))
(not (ans ?))
=>
(if (yes-or-no-p "Do you have headache(yes/no)? ") then (assert (ans "unknown disease"))
else (assert (headache-state patient no-headache))))
(defrule chain-1-throatache-state ""
(fever-state patient fever-low)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient has-snuffle)
(headache-state patient no-headache)
(not (throatache-state patient ?))
(not (ans ?))
=>
(if (yes-or-no-p "Do you have throatache(yes/no)? ") then (assert (throatache-state patient has-throatache))
else (assert (ans "unknown disease"))))
(defrule chain-1-hard-to-breath-state ""
(fever-state patient fever-low)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient has-snuffle)
(headache-state patient no-headache)
(throatache-state patient has-throatache)
(not (hard-to-breath-state patient ?))
(not (ans ?))
=>
(if (yes-or-no-p "Do you breath hardly(yes/no)? ") then (assert (ans "unknown disease"))
else (assert (hard-to-breath-state patient no-hard-to-breath))))
(defrule chain-1-virus-in-blood-state ""
(fever-state patient fever-low)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient has-snuffle)
(headache-state patient no-headache)
(throatache-state patient has-throatache)
(hard-to-breath-state patient no-hard-to-breath)
(not (virus-in-blood-state patient ?))
(not (ans ?))
=>
(if (yes-or-no-p "Do you have virus in your blood(yes/no)? ") then (assert (virus-in-blood-state patient has-virus-in-blood))
else (assert (virus-in-blood-state patient no-virus-in-blood))))
(defrule determine-flu2 ""
(fever-state patient fever-low)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient has-snuffle)
(headache-state patient no-headache)
(throatache-state patient has-throatache)
(hard-to-breath-state patient no-hard-to-breath)
(virus-in-blood-state patient has-virus-in-blood)
(not (neck-harden-state patient ?))
(not (ans ?))
=>
(if (yes-or-no-p "Do you feel your neck harden(yes/no)? ") then (assert (ans "unknown disease"))
else (assert (ans "Flu-2"))))
(defrule determine-normal-cold ""
(fever-state patient fever-low)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient has-snuffle)
(headache-state patient no-headache)
(throatache-state patient has-throatache)
(hard-to-breath-state patient no-hard-to-breath)
(virus-in-blood-state patient no-virus-in-blood)
(not (neck-harden-state patient ?))
(not (ans ?))
=>
(if (yes-or-no-p "Do you feel your neck harden(yes/no)? ") then (assert (ans "unknown disease"))
else (assert (ans "Normal Cold"))))
;;---finish to determine flu-2 and normal cold-------------------------------------------------------------------------
(defrule chain-2-headache-state ""
(fever-state patient fever-low)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient no-snuffle)
(not (headache-state patient ?))
(not (ans ?))
=>
(if (yes-or-no-p "Do you have headache(yes/no)? ") then (assert (headache-state patient has-headache))
else (assert (ans "unknown disease"))))
(defrule pneu-throatache-state ""
(fever-state patient fever-low)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient no-snuffle)
(headache-state patient has-headache)
(not (throatache-state patient ?))
(not (ans ?))
=>
(if (yes-or-no-p "Do you have throatache(yes/no)? ") then (assert (ans "unknown disease"))
else (assert (throatache-state patient no-throatache))))
(defrule chain-2-hard-to-breath-state ""
(fever-state patient fever-low)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient no-snuffle)
(headache-state patient has-headache)
(not (hard-to-breath-state patient ?))
(not (ans ?))
=>
(if (yes-or-no-p "Do you breath hardly(yes/no)? ") then (assert (hard-to-breath-state patient has-hard-to-breath))
else (assert (ans "unknown disease"))))
(defrule chain-2-virus-in-blood-state ""
(fever-state patient fever-low)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient no-snuffle)
(headache-state patient has-headache)
(hard-to-breath-state patient has-hard-to-breath)
(not (virus-in-blood-state patient ?))
(not (ans ?))
=>
(if (yes-or-no-p "Do you have virus in your blood(yes/no)? ") then (assert (ans "unknown disease"))
else (assert (virus-in-blood-state patient no-virus-in-blood))))
(defrule determine-pneumonia ""
(fever-state patient fever-low)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient no-snuffle)
(headache-state patient has-headache)
(hard-to-breath-state patient has-hard-to-breath)
(virus-in-blood-state patient no-virus-in-blood)
(not (neck-harden-state patient ?))
(not (ans ?))
=>
(if (yes-or-no-p "Do you feel your neck harden(yes/no)? ") then (assert (ans "unknown disease"))
else (assert (ans "Pneumonia"))))
;;-------------------finish to determine Pneumonia---------------------------------------------------------------------
;;------------------start to determine:Heavy Cold,SARS,Flu-1,Heavy Cold,Meningistis------------------------------------
(defrule fever-high-cough-state ""
(fever-state patient fever-high)
(not (cough-state patient ?))
=>
(if (yes-or-no-p "Do you have cough(yes/no)? ") then (assert (cough-state patient has-cough))
else (assert (cough-state patient no-cough))))
(defrule chain-3-1-sputum-state ""
(fever-state patient fever-high)
(cough-state patient has-cough)
(not (sputum-state patient ?))
=>
(if (yes-or-no-p "Do you have sputum(yes/no)? ") then (assert (sputum-state patient has-sputum))
else (assert (ans "unknown disease"))))
(defrule chain-3-1-snuffle-state ""
(fever-state patient fever-high)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(not (snuffle-state patient ?))
=>
(if (yes-or-no-p "Do you have Snuffle and Drivel(yes/no)? ") then (assert (snuffle-state patient has-snuffle))
else (assert (snuffle-state patient no-snuffle))))
;;------------------start to determine:Heavy Cold----------------------------------------------------------------------
(defrule heavy-cold-headache-state ""
(fever-state patient fever-high)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient has-snuffle)
(not (headache-state patient ?))
=>
(if (yes-or-no-p "Do you have headache(yes/no)? ") then (assert (headache-state patient has-headache))
else (assert (ans "unknown disease"))))
(defrule heavy-cold-throatache-state ""
(fever-state patient fever-high)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient has-snuffle)
(headache-state patient has-headache)
(not (throatache-state patient ?))
=>
(if (yes-or-no-p "Do you have throactache(yes/no)? ") then (assert (throatache-state patient has-throatache))
else (assert (ans "unknown disease"))))
(defrule heavy-cold-hard-to-breath-state ""
(fever-state patient fever-high)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient has-snuffle)
(headache-state patient has-headache)
(throatache-state patient has-throatache)
(not (hard-to-breath-state patient ?))
=>
(if (yes-or-no-p "Do you feel breath hardly(yes/no)? ") then (assert (ans "unknown disease"))
else (assert (hard-to-breath-state patient no-hard-to-breath))))
(defrule heavy-cold-virus-in-blood-state ""
(fever-state patient fever-high)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient has-snuffle)
(headache-state patient has-headache)
(throatache-state patient has-throatache)
(hard-to-breath-state patient no-hard-to-breath)
(not (virus-in-blood-state patient ?))
=>
(if (yes-or-no-p "Do you have virus in blood(yes/no)? ") then (assert (ans "unknown disease"))
else (assert (virus-in-blood-state patient no-virus-in-blood))))
(defrule determine-heavy-cold ""
(fever-state patient fever-high)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient has-snuffle)
(headache-state patient has-headache)
(throatache-state patient has-throatache)
(hard-to-breath-state patient no-hard-to-breath)
(virus-in-blood-state patient no-virus-in-blood)
(not (neck-harden-state patient ?))
=>
(if (yes-or-no-p "Do you feel your neck harden(yes/no)? ") then (assert (ans "unknown disease"))
else (assert (ans "Heavy Cold"))))
;;------------------Finish to determine:Heavy Cold----------------------------------------------------------------------
;;------------------Start to determine:SARS-----------------------------------------------------------------------------
(defrule sars-headache-state ""
(fever-state patient fever-high)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient no-snuffle)
(not (headache-state patient ?))
=>
(if (yes-or-no-p "Do you have headache(yes/no)? ") then (assert (headache-state patient has-headache))
else (assert (ans "unknown disease"))))
(defrule sars-throatache-state ""
(fever-state patient fever-high)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient no-snuffle)
(headache-state patient has-headache)
(not (throatache-state patient ?))
=>
(if (yes-or-no-p "Do you have throactache(yes/no)? ") then (assert (throatache-state patient has-throatache))
else (assert (ans "unknown disease"))))
(defrule sars-hard-to-breath-state ""
(fever-state patient fever-high)
(cough-state patient has-cough)
(sputum-state patient has-sputum)
(snuffle-state patient no-snuffle)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -