In: Computer Science
Write a python program.
The function protocol implements the functionality of the
diagnostic protocol. The main idea of this function is to call the
functions that you
have already implemented in the previous questions. See below for
an explanation of exactly what is
expected.
def protocol(my_symptoms: Tuple[Set],
all_patients_symptoms: Dict[str, Tuple[Set]],
all_patients_diagnostics: Dict[int, str]) -> str:
'''Return the diagnostic for my_symptoms based on the dictionary
of patients symptoms
(all_patients_symptoms) and the dictionary of patients
diagnostics
(all_patients_diagnostics).
Hint: This function selects the patients with the highest
similarity between the their
symptoms and my_symptoms (by calling the function
similarity_to_patients). Then, it
reports the the most frequent diagnostic from the patients with the
highest symptoms
similarity (by calling the function getting_diagnostics).
>>>protocol(yang, all_patients_symptoms,
all_patients_diagnostics)
'cold'
'''
Use the function my_test to evaluate the code
ANSWER:
I have provided the properly commented and indented
code so you can easily copy the code as well as check for correct
indentation.
I have provided the output image of the code so you can easily
cross-check for the correct output of the code.
Have a nice and healthy day!!
CODE
# patients symptoms all_patients_symptoms = {56374: ({"headache","fever"},{"coughing","runny_nose","sneezing"}), 45437: ({"coughing","runny_nose"},{"headache","fever"}), 16372: ({"coughing","sore_throat"},{"fever"}), 54324: ({"vomiting","coughing","stomach_pain"},{"fever"}), 73454: ({"coughing","runny_nose"},{"headache","fever"}), 35249: ({"coughing","sore_throat","fever"},{"stomach_pain","runny_nose"}), 44274: ({"fever","headache"},{"stomach_pain","runny_nose","sore_throat","coughing"}), 74821: ({"vomiting","fever"},{"headache"}), 94231 : ({"stomach_pain","fever","sore_throat","coughing","headache"},{"vomiting"})} # patient's diagonstics all_patients_diagnostics = {45437: "cold", 56374: "meningitis", 54324:"food_poisoning",16372:"cold", 73454: "cold", 35429:"pharyngitis", 44274:"meningitis",74821:"food_poisoning", 94231:"unknown"} # three test patients yang = ({"coughing","runny_nose","sneezing"}, {"headache","fever"}) maria = ({"coughing","fever","sore_thorat","sneezing"},{"muscle_pain"}) jaspal = ({"headache"},{"sneezing"}) # defining sample function similarity_to_patients def similarity_to_patients(my_symptoms, all_patients_symptoms) -> str: # defining empty dict to record similarity of patients similarity_dict = {} # looping through all_patients_symptoms dict for key in all_patients_symptoms: # calling function symptom_similarity to fetch similarity similarity_dict[key] = symptom_similarity(my_symptoms,all_patients_symptoms[key]) # returning similarity_dict return similarity_dict # sample function getting_diagnostics def getting_diagnostics(similarity_dict, all_patients_diagnostics): # fetching most similar symptoms from similarity_dict # using function max and fetching key Id having highest similarity value # using key argument of max function to do so max_similar_id = max(similarity_dict, key = lambda x:similarity_dict[x]) # returning diagnostic of fetched max_similar_id return all_patients_diagnostics[max_similar_id] # Q4. function protocol # using all sample defined functions def protocol(my_symptoms, all_patients_symptoms, all_patients_diagnostics) -> str: # calling function similarity_to_patients and fecthing similarity_dict of patient similarity_dict = similarity_to_patients(my_symptoms, all_patients_symptoms) # calling function getting_diagnostics and fetching most similar dignostic similar_diag = getting_diagnostics(similarity_dict, all_patients_diagnostics) # returning value return similar_diag # testing function protocol diag_str = protocol(yang, all_patients_symptoms, all_patients_diagnostics) print("response: ",diag_str)
OUTPUT IMAGE
i hope it helps..
If you have any doubts please comment and please don't dislike.
PLEASE GIVE ME A LIKE. ITS VERY IMPORTANT FOR ME