-
Notifications
You must be signed in to change notification settings - Fork 0
/
expert_system.py
60 lines (50 loc) · 2.06 KB
/
expert_system.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import pandas as pd
class MedicalExpertSystem:
def __init__(self):
"""Initialize the MedicalExpertSystem."""
self.__symptoms = []
self.__diagnosis = None
self.__patient_data = pd.DataFrame(columns=["Symptoms", "Diagnosis"])
def __ask_question(self, question):
"""Ask a yes/no question and return the answer."""
answer = input(question + " (yes/no): ").strip().lower()
if answer == "yes":
return True
elif answer == "no":
return False
else:
print("Invalid input. Please answer with 'yes' or 'no'.")
return self.__ask_question(question)
def __diagnose(self):
"""Diagnose the patient based on symptoms."""
if self.__ask_question("Do you have a fever?"):
self.__symptoms.append("fever")
if self.__ask_question("Do you have a headache?"):
self.__symptoms.append("headache")
if self.__ask_question("Do you have a cough?"):
self.__symptoms.append("cough")
new_data = pd.DataFrame(
{"Symptoms": [", ".join(self.__symptoms)], "Diagnosis": [""]}
)
self.__patient_data = pd.concat(
[self.__patient_data, new_data], ignore_index=True
)
if "fever" in self.__symptoms and "headache" in self.__symptoms:
self.__diagnosis = "You might have the flu."
elif "fever" in self.__symptoms and "cough" in self.__symptoms:
self.__diagnosis = "You might have a cold."
else:
self.__diagnosis = "Your condition is unclear. Please consult a doctor."
self.__patient_data.loc[
self.__patient_data.index[-1], "Diagnosis"
] = self.__diagnosis
def run(self):
"""Run the Medical Expert System."""
print("Welcome to the Medical Expert System.")
self.__diagnose()
print("Diagnosis:", self.__diagnosis)
print("Patient Data:")
print(self.__patient_data)
if __name__ == "__main__":
expert_system = MedicalExpertSystem()
expert_system.run()