Assignment No 6
Assignment No 6
1. Information management
Code:
Main. Py
Diseases_list = []
Diseases_symptoms = []
Symptom_map = {}
D_desc_map = {}
D_treatment_map = {}
#loads the knowledge from .txt files into variables to allow the code to use it
Def preprocess():
Diseases = open(“diseases.txt”)
Diseases_t = diseases.read()
Diseases_list = diseases_t.split(“\n”)
Diseases.close()
Disease_s_data = disease_s_file.read()
S_list = disease_s_data.split(“\n”)
Diseases_symptoms.append(s_list)
Symptom_map[str(s_list)] = disease
Disease_s_file.close()
Disease_s_data = disease_s_file.read()
D_desc_map[disease] = disease_s_data
Disease_s_file.close()
Disease_s_data = disease_s_file.read()
D_treatment_map[disease] = disease_s_data
Disease_s_file.close()
Def identify_disease(*arguments):
Symptom_list = []
Symptom_list.append(symptom)
Return symptom_map[str(symptom_list)]
Def get_details(disease):
Return d_desc_map[disease]
Def get_treatments(disease):
Return d_treatment_map[disease]
Def if_not_matched(disease):
Print(“”)
Id_disease = disease
Disease_details = get_details(id_disease)
Treatments = get_treatments(id_disease)
Print(“”)
Print(disease_details + “\n”)
Print(
“The common medications and procedures suggested by other real doctors are: \n”
Print(treatments + “\n”)
#driver function
If __name__ == “__main__”:
Preprocess()
#loop to keep running the code until user says no when asked for another diagnosis
While 1:
Engine.reset()
Engine.run()
Print(“Would you like to diagnose some other symptoms?\n Reply yes or no”)
If input() == “no”:
Exit()
Greetings. Py
1.
2.
master
/greetings.py
class Greetings(KnowledgeEngine):
self.symptom_map = symptom_map
self.if_not_matched = if_not_matched
self.get_details = get_details
self.get_treatments = get_treatments
KnowledgeEngine.__init__(self)
@DefFacts()
def _initial_action(self):
print("")
print("")
print("")
yield Fact(action="find_disease")
def symptom_0(self):
self.declare(Fact(headache=input("headache: ")))
def symptom_1(self):
def symptom_2(self):
def symptom_3(self):
self.declare(Fact(cough=input("cough: ")))
def symptom_4(self):
self.declare(Fact(fainting=input("fainting: ")))
def symptom_5(self):
self.declare(Fact(fatigue=input("fatigue: ")))
def symptom_7(self):
def symptom_8(self):
self.declare(Fact(restlessness=input("restlessness: ")))
def symptom_9(self):
def symptom_10(self):
self.declare(Fact(fever=input("fever: ")))
def symptom_11(self):
self.declare(Fact(nausea=input("nausea: ")))
def symptom_12(self):
self.declare(Fact(blurred_vision=input("blurred_vision: ")))
Fact(action="find_disease"),
Fact(headache="no"),
Fact(back_pain="no"),
Fact(chest_pain="no"),
Fact(cough="no"),
Fact(fainting="no"),
Fact(sore_throat="no"),
Fact(fatigue="high"),
Fact(restlessness="no"),
Fact(low_body_temp="no"),
Fact(fever="low"),
Fact(sunken_eyes="no"),
Fact(nausea="high"),
Fact(blurred_vision="no"),
def disease_0(self):
self.declare(Fact(disease="Jaundice"))
@Rule(
Fact(action="find_disease"),
Fact(headache="no"),
Fact(back_pain="no"),
Fact(chest_pain="no"),
Fact(cough="no"),
Fact(fainting="no"),
Fact(sore_throat="no"),
Fact(fatigue="no"),
Fact(restlessness="high"),
Fact(low_body_temp="no"),
Fact(fever="no"),
Fact(sunken_eyes="no"),
Fact(nausea="no"),
Fact(blurred_vision="no"),
def disease_1(self):
self.declare(Fact(disease="Alzheimers"))
@Rule(
Fact(action="find_disease"),
Fact(headache="no"),
Fact(back_pain="high"),
Fact(chest_pain="no"),
Fact(cough="no"),
Fact(fainting="no"),
Fact(sore_throat="no"),
Fact(fatigue="low"),
Fact(restlessness="no"),
Fact(low_body_temp="no"),
Fact(fever="no"),
Fact(sunken_eyes="no"),
Fact(nausea="no"),
Fact(blurred_vision="no"),
def disease_2(self):
self.declare(Fact(disease="Arthritis"))
@Rule(
Fact(action="find_disease"),
Fact(headache="no"),
Fact(back_pain="no"),
Fact(chest_pain="high"),
Fact(cough="low"),
Fact(fainting="no"),
Fact(sore_throat="no"),
Fact(fatigue="no"),
Fact(restlessness="no"),
Fact(low_body_temp="no"),
Fact(fever="high"),
Fact(sunken_eyes="no"),
Fact(nausea="no"),
Fact(blurred_vision="no"),
def disease_3(self):
self.declare(Fact(disease="Tuberculosis"))
@Rule(
Fact(action="find_disease"),
Fact(headache="no"),
Fact(back_pain="no"),
Fact(chest_pain="high"),
Fact(cough="high"),
Fact(fainting="no"),
Fact(sore_throat="no"),
Fact(fatigue="no"),
Fact(restlessness="low"),
Fact(low_body_temp="no"),
Fact(fever="no"),
Fact(sunken_eyes="no"),
Fact(nausea="no"),
Fact(blurred_vision="no"),
def disease_4(self):
self.declare(Fact(disease="Asthma"))
@Rule(
Fact(action="find_disease"),
Fact(headache="low"),
Fact(back_pain="no"),
Fact(chest_pain="no"),
Fact(cough="high"),
Fact(fainting="no"),
Fact(sore_throat="high"),
Fact(fatigue="no"),
Fact(restlessness="no"),
Fact(low_body_temp="no"),
Fact(fever="low"),
Fact(sunken_eyes="no"),
Fact(nausea="no"),
Fact(blurred_vision="no"),
def disease_5(self):
self.declare(Fact(disease="Sinusitis"))
@Rule(
Fact(action="find_disease"),
Fact(headache="no"),
Fact(back_pain="no"),
Fact(chest_pain="no"),
Fact(cough="no"),
Fact(fainting="no"),
Fact(sore_throat="no"),
Fact(fatigue="low"),
Fact(restlessness="no"),
Fact(low_body_temp="no"),
Fact(fever="no"),
Fact(sunken_eyes="no"),
Fact(nausea="no"),
Fact(blurred_vision="no"),
def disease_6(self):
self.declare(Fact(disease="Epilepsy"))
@Rule(
Fact(action="find_disease"),
Fact(headache="no"),
Fact(back_pain="no"),
Fact(chest_pain="high"),
Fact(cough="no"),
Fact(fainting="no"),
Fact(sore_throat="no"),
Fact(fatigue="no"),
Fact(restlessness="no"),
Fact(low_body_temp="no"),
Fact(fever="no"),
Fact(sunken_eyes="no"),
Fact(nausea="high"),
Fact(blurred_vision="no"),
def disease_7(self):
self.declare(Fact(disease="Heart Disease"))
@Rule(
Fact(action="find_disease"),
Fact(headache="no"),
Fact(back_pain="no"),
Fact(chest_pain="no"),
Fact(cough="no"),
Fact(fainting="no"),
Fact(sore_throat="no"),
Fact(fatigue="high"),
Fact(restlessness="no"),
Fact(low_body_temp="no"),
Fact(fever="no"),
Fact(sunken_eyes="no"),
Fact(nausea="low"),
Fact(blurred_vision="low"),
)
def disease_8(self):
self.declare(Fact(disease="Diabetes"))
@Rule(
Fact(action="find_disease"),
Fact(headache="low"),
Fact(back_pain="no"),
Fact(chest_pain="no"),
Fact(cough="no"),
Fact(fainting="no"),
Fact(sore_throat="no"),
Fact(fatigue="no"),
Fact(restlessness="no"),
Fact(low_body_temp="no"),
Fact(fever="no"),
Fact(sunken_eyes="no"),
Fact(nausea="high"),
Fact(blurred_vision="low"),
def disease_9(self):
self.declare(Fact(disease="Glaucoma"))
@Rule(
Fact(action="find_disease"),
Fact(headache="no"),
Fact(back_pain="no"),
Fact(chest_pain="no"),
Fact(cough="no"),
Fact(fainting="no"),
Fact(sore_throat="no"),
Fact(fatigue="high"),
Fact(restlessness="no"),
Fact(low_body_temp="no"),
Fact(fever="no"),
Fact(sunken_eyes="no"),
Fact(nausea="low"),
Fact(blurred_vision="no"),
def disease_10(self):
self.declare(Fact(disease="Hyperthyroidism"))
@Rule(
Fact(action="find_disease"),
Fact(headache="high"),
Fact(back_pain="no"),
Fact(chest_pain="no"),
Fact(cough="no"),
Fact(fainting="no"),
Fact(sore_throat="no"),
Fact(fatigue="no"),
Fact(restlessness="no"),
Fact(low_body_temp="no"),
Fact(fever="high"),
Fact(sunken_eyes="no"),
Fact(nausea="high"),
Fact(blurred_vision="no"),
)
def disease_11(self):
self.declare(Fact(disease="Heat Stroke"))
@Rule(
Fact(action="find_disease"),
Fact(headache="no"),
Fact(back_pain="no"),
Fact(chest_pain="no"),
Fact(cough="no"),
Fact(fainting="yes"),
Fact(sore_throat="no"),
Fact(fatigue="no"),
Fact(restlessness="no"),
Fact(low_body_temp="high"),
Fact(fever="no"),
Fact(sunken_eyes="no"),
Fact(nausea="no"),
Fact(blurred_vision="no"),
def disease_12(self):
self.declare(Fact(disease="Hypothermia"))
@Rule(
Fact(action="find_disease"),
Fact(headache="high"),
Fact(back_pain="no"),
Fact(chest_pain="high"),
Fact(cough="high"),
Fact(fainting="no"),
Fact(sore_throat="high"),
Fact(fatigue="high"),
Fact(restlessness="no"),
Fact(low_body_temp="no"),
Fact(fever="high"),
Fact(sunken_eyes="no"),
Fact(nausea="no"),
Fact(blurred_vision="no"),
def disease_13(self):
self.declare(Fact(disease="Coronavirus"))
#when the user's input doesn't match any disease in the knowledge base
print("")
id_disease = disease
disease_details = self.get_details(id_disease)
treatments = self.get_treatments(id_disease)
print("")
print(disease_details + "\n")
print(
print(treatments + "\n")
@Rule(
Fact(action="find_disease"),
Fact(headache=MATCH.headache),
Fact(back_pain=MATCH.back_pain),
Fact(chest_pain=MATCH.chest_pain),
Fact(cough=MATCH.cough),
Fact(fainting=MATCH.fainting),
Fact(sore_throat=MATCH.sore_throat),
Fact(fatigue=MATCH.fatigue),
Fact(low_body_temp=MATCH.low_body_temp),
Fact(restlessness=MATCH.restlessness),
Fact(fever=MATCH.fever),
Fact(sunken_eyes=MATCH.sunken_eyes),
Fact(nausea=MATCH.nausea),
Fact(blurred_vision=MATCH.blurred_vision),
NOT(Fact(disease=MATCH.disease)),
salience=-999
def not_matched(
self,
headache,
back_pain,
chest_pain,
cough,
fainting,
sore_throat,
fatigue,
restlessness,
low_body_temp,
fever,
sunken_eyes,
nausea,
blurred_vision,
):
print("\nThe bot did not find any diseases that match your exact
symptoms.")
lis = [
headache,
back_pain,
chest_pain,
cough,
fainting,
sore_throat,
fatigue,
restlessness,
low_body_temp,
fever,
sunken_eyes,
nausea,
blurred_vision,
max_count = 0
max_disease = ""
for key, val in self.symptom_map.items():
count = 0
temp_list = eval(key)
count = count + 1
max_count = count
max_disease = val
if max_disease != "":
self.if_not_matched(max_disease)
Output: