-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.pl
453 lines (325 loc) · 14.6 KB
/
Main.pl
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
CSINTSY - MCO3
AMOGUIS, Adriel Isaiah V.
SUN, Benedict C.
*/
% Import the Diseases Module
:- consult("Diseases.pl").
% Special Dynamic Facts
:- dynamic emergency/1.
:- dynamic diagnosis/3.
/* MAIN SESSION HANDLER */
diagnose(PatientName) :-
% Greet Patient
format("Good day, ~w!~n~n", [PatientName]),
% Display Program Details
write_ln("Medical Diagnostc System: Developed by AMOGUIS & SUN."),
write_ln("College of Computer Studies, De La Salle University\n"),
% Display Confidentaility Clause
write_ln("The data collected from you during this session is for diagnostic purposes only and shall only be stored throughout this session."),
write_ln("We will be asking you a series of questions regarding your symptoms to establish an idea of possible illnesses.\n"),
% Get User Consent
write("Would you like to proceed? [y / n]: "), read(Consent),
(Consent = 'Y' ; Consent = 'y'),
nl, nl, nl,
% Prompt User of the Questionnaire
write_ln("\nWe will now begin with the questionnaire.\n"),
% Get User Consent
write("Would you like to proceed? [y / n]: "), read(Consent),
(Consent = 'Y' ; Consent = 'y'),
nl, nl, nl,
% Get the HPI
getHPI(PatientName),
% Get the Chief Complaint and Diagnose
(chiefComplaint(PatientName) ; true), !,
% Parse Results
diagnosedSummary(PatientName).
/* Get the Patient's HPI */
getHPI(P) :-
% Ask first if emergency
write("ARE YOU IN AN EMERGENCY? [y / n] : "), read(EMERG),
((EMERG = 'y' ; EMERG = 'Y') -> assert(emergency(P)) ; true),
% Temperature
write("Please enter your temperature (in degrees celsius) : "), read(Temp),
assert(temperature(P, Temp)),
% Diastolic Blood Pressure
write("Please enter your diastolic blood pressure (in mmHg) : "), read(DiasBP),
% Systolic Blood Pressure
write("Please enter your systolic blood pressure (in mmHg) : "), read(SysBP),
assert(bloodPressure(P, DiasBP, SysBP)),
% Height
write("Please enter your height (in centimeters) : "), read(Height),
assert(height(P, Height)),
% Weight
write("Please enter your weight (in kilograms) : "), read(Weight),
assert(weight(P, Weight)).
/* Chief Complaint Questionnaire */
chiefComplaint(P) :-
% Gather the Chief Complaint, ask for some unique symptoms
% VIRAL (and Malaria)
% - Fever, Malaise, Colds/RunnyNose
% RESPIRATORY
% - Chest Pain, Coughing
% GASTRO
% - LooseStools, Dehydration, Questionable Food Intake
% CARDIO
% - Elevated BP
% LIFESTYLE DISEASE
% - Weight Loss
% VIRAL
(viralQuestionnaire(P) ; true), !,
% Check if the patient is already diagnosed
(not(diagnosis(P, _, _)) ; emergency(P)),
% RESPIRATORY
(respiratoryQuestionnaire(P) ; true), !,
% Check if the patient is already diagnosed
(not(diagnosis(P, _, _)) ; emergency(P)),
% GASTRO
(gastroQuestionnaire(P) ; true), !,
% Check if the patient is already diagnosed
(not(diagnosis(P, _, _)) ; emergency(P)),
% LIFESTYLE
lifestyleQuestionnaire(P), !.
/* Questions for Viral Diseases */
viralQuestionnaire(P) :-
% Patient Must Have a Fever
fever(P),
% Body Malaise
write("Do you feel an overall weakness in your whole body? [y / n] : "), read(BM),
((BM = 'y' ; BM = 'Y') -> assert(malaise(P)) ; true),
% Runny Nose / Colds
write("Do you have a runny nose? [y / n] : "), read(RN),
((RN = 'y' ; RN = 'Y') -> assert(runnyNose(P)) ; true),
write("Do you have colds? [y / n] : "), read(Lamig),
((Lamig = 'y' ; Lamig = 'Y') -> assert(cold(P)) ; true),
% Test for Malaria (if there is body malaise)
(malaise(P) -> (
write("Do you feel chills (especially at night)? [y / n] : "), read(Chills),
((Chills = 'Y' ; Chills = 'y') -> assert(chills(P)) ; true),
write("Are you having frequent headaches? [y / n] : "), read(HA),
((HA = 'Y' ; HA = 'y') -> assert(headache(P)) ; true),
write("Are you having nausea? [y / n] : "), read(NA),
((NA = 'Y' ; NA = 'y') -> assert(nausea(P)) ; true),
write("Are you vomiting often? [y / n] : "), read(Vomit),
((Vomit = 'Y' ; Vomit = 'y') -> assert(vomiting(P)) ; true),
write("Have you been in a forest, jungle, fields, or bitten by a mosquito? [y / n] : "), read(RiskArea),
((RiskArea = 'Y' ; RiskArea = 'y') -> assert(malariaRiskArea(P)) ; true),
% Get Malaria Certainty
(malaria(P, MalariaCertainty))
) ; true),
% Test for Dengue (if there is body malaise)
(malaise(P) -> (
(not(headache(P)) -> (write("Are you having frequent headaches? [y / n] : "), read(HAA),
((HAA = 'Y' ; HAA = 'y') -> assert(headache(P)) ; true)); true),
write("Are you having rashes? [y / n] : "), read(Rash),
((Rash = 'Y' ; Rash = 'y') -> assert(rash(P)) ; true),
% Get Dengue Certainty
(dengue(P, DengueCertainty))
) ; true),
% If malaise does not exist, declare certainties to 0
(not(malaise(P)) -> DengueCertainty is 0, MalariaCertainty is 0 ; true),
% Test for Flu (if there is colds/runnynose)
(runnyNose(P) -> (
(not(cough(P)) -> (write("Are you having cough? [y / n] : "), read(Cough),
((Cough = 'Y' ; Cough = 'y') -> assert(cough(P))) ; true); true),
% Get Flu Certainty
(flu(P, FluCertainty))
) ; true),
% If there is no runny nose, declare certainty to 0
(not(runnyNose(P)) -> FluCertainty is 0 ; true), !,
% Check if any certainties are over 75, if not emergency
(not(emergency(P)) -> (
((FluCertainty >= 75) -> assert(diagnosis(P, "Flu", FluCertainty)) ; true), !,
((DengueCertainty >= 75) -> assert(diagnosis(P, "Dengue", DengueCertainty)) ; true), !,
((MalariaCertainty >= 75) -> assert(diagnosis(P, "Malaria", MalariaCertainty)) ; true), !
); true), !,
% If emergency, store diagnosis anyway
((emergency(P) -> (
assert(diagnosis(P, "Flu", FluCertainty)),
assert(diagnosis(P, "Dengue", DengueCertainty)),
assert(diagnosis(P, "Malaria", MalariaCertainty))
)); true), !.
/* Questions for Respiratory Diseases */
respiratoryQuestionnaire(P) :-
% Patient must have chest pain and Coughing
% Chest Pain
write("Are you having chest pain? [y / n] : "), read(CP),
((CP = 'y' ; CP = 'Y') -> assert(chestPain(P)) ; true),
% Coughing
(not(cough(P)) -> (
write("Have you been coughing? [y / n] : "), read(Ubo),
((Ubo = 'y' ; Ubo = 'Y') -> assert(cough(P)) ; true)
) ; true),
% Check if the patient has chest pain and is coughing
chestPain(P), cough(P),
% Check for phlegm
write("Are you coughing with phlegm? [y / n] : "), read(Dura),
((Dura = 'y' ; Dura = 'Y') -> assert(phlegm(P)) ; true),
% Check for Tuberculosis if coughing for more than 2 weeks
write("Have you been coughing for 2 weeks or more? [y / n] : "), read(MahabangUbo),
((MahabangUbo = 'y' ; MahabangUbo = 'Y') -> assert(longCough(P)) ; true),
(longCough(P) -> (
write("Do you cough up blood? [y / n] : "), read(UboDugo),
((UboDugo = 'Y' ; UboDugo = 'y') -> assert(bloodCough(P)) ; true),
% Get TB Certainty
tuberculosis(P, TBCertainty)
) ; true),
% Set certainty to 0 if not longCough
(not(longCough(P)) -> TBCertainty is 0 ; true),
% Check for Pneumonia if feverish
(fever(P) -> (
write("Are you having shortness of breath? [y / n] : "), read(ShortBreath),
((ShortBreath = 'Y' ; ShortBreath = 'y') -> assert(shortnessBreath(P)) ; true),
% Get Dengue Certainty
pneumonia(P, PneuCertainty)
) ; true),
% Set certainty to 0 if not feverish
(not(fever(P)) -> PneuCertainty is 0 ; true),
% Check for Bronchitis if Wheezing
write("Are you wheezing? [y / n] : "), read(Wheeze),
((Wheeze = 'Y' ; Wheeze = 'y') -> assert(wheezing(P)) ; true),
% Get Bronchitis Certainty
((wheezing(P) -> (bronchitis(P, BronchitisCertainty)) ; true)),
% Set certainty to 0 if not wheezing
(not(wheezing(P)) -> BronchitisCertainty is 0 ; true),
% Check if any certainties are over 75, if not emergency
(not(emergency(P)) -> (
((TBCertainty >= 75) -> assert(diagnosis(P, "Tuberculosis", TBCertainty)) ; true), !,
((PneuCertainty >= 75) -> assert(diagnosis(P, "Pneumonia", PneuCertainty)) ; true), !,
((BronchitisCertainty >= 75) -> assert(diagnosis(P, "Bronchitis", BronchitisCertainty)) ; true), !
); true), !,
% If emergency, store diagnosis anyway
((emergency(P) -> (
assert(diagnosis(P, "Tuberculosis", TBCertainty)),
assert(diagnosis(P, "Pneumonia", PneuCertainty)),
assert(diagnosis(P, "Bronchitis", BronchitisCertainty))
)); true), !.
/* Questions for Gastrointestinal Diseases */
gastroQuestionnaire(P) :-
% Check if the patient has loose bowel movement
% LBM
write("Are you having loose bowel movement? [y / n] : "), read(LBM),
((LBM = 'y' ; LBM = 'Y') -> assert(looseStools(P)) ; true),
% Must have looseStools
looseStools(P),
% Check for diarrhea
% Check for Abdominal Pain
write("Are you experiencing abdominal pain? [y / n] : "), read(AwitTiyan),
((AwitTiyan = 'y' ; AwitTiyan = 'Y') -> assert(abdominalPain(P)) ; true),
% Check for Dehydration
write("Are you dehydrated? [y / n] : "), read(Dehyd),
((Dehyd = 'y' ; Dehyd = 'Y') -> assert(dehydration(P)) ; true),
% Get Diarrhea Certainty
diarrhea(P, DiarrheaCertainty),
% Check for Cholera only if there is diarrhea
(((diarrhea(P, DCer), DCer >= 75) -> (
% Check for more profuse bowel movement
write("Is your bowel movement really severe (totally liquid)? [y / n] : "), read(AwitAwitTiyan),
((AwitAwitTiyan = 'y' ; AwitAwitTiyan = 'Y') -> assert(fasterStools(P)) ; true),
% Check for more profuse dehydration
write("Is your dehydration severe? [y / n] : "), read(AwitDehydration),
((AwitDehydration = 'y' ; AwitDehydration = 'Y') -> assert(fasterDehydration(P)) ; true),
% Get cholera certainty
cholera(P, CholeraCertainty)
)) ; true),
(not((diarrhea(P, DCCer), DCCer >= 75)) -> CholeraCertainty is 0 ; true),
% Check if any certainties are over 75, if not emergency
(not(emergency(P)) -> (
((DiarrheaCertainty >= 75) -> assert(diagnosis(P, "Diarrhea", DiarrheaCertainty)) ; true), !,
((CholeraCertainty >= 75) -> assert(diagnosis(P, "Cholera", CholeraCertainty)) ; true), !
); true), !,
% If emergency, store diagnosis anyway
((emergency(P) -> (
assert(diagnosis(P, "Diarrhea", DiarrheaCertainty)),
assert(diagnosis(P, "Cholera", CholeraCertainty))
)); true), !.
/* Questions for Lifestyle Diseases */
lifestyleQuestionnaire(P) :-
% HYPERTENSION - Patient must have elevated blood pressure
(highBP(P) -> (
% Check for Headache
write("Are you experiencing headaches? [y / n] : "), read(SakitUlo),
((SakitUlo = 'y' ; SakitUlo = 'Y') -> assert(headache(P)) ; true),
% Vision Changes
write("Are you experiencing vision changes? [y / n] : "), read(IbaPagtingin),
((IbaPagtingin = 'y' ; IbaPagtingin = 'Y') -> assert(blurryVision(P)) ; true),
% Get Hypertension Certainty
hypertension(P, HypertensionCertainty)
) ; true),
(not(highBP(P)) -> HypertensionCertainty is 0 ; true),
% DIABETES - Check for all symptoms
% Check for Increased Urination
write("Are you experiencing increased urination? [y / n] : "), read(DamingIhi),
((DamingIhi = 'y' ; DamingIhi = 'Y') -> assert(increasedUrine(P)) ; true),
% Check for Increased Thirst
write("Are you experiencing increased thirst? [y / n] : "), read(Uhaw),
((Uhaw = 'y' ; Uhaw = 'Y') -> assert(increasedThirst(P)) ; true),
% Check for Weight Loss
write("Have you been losing weight lately? [y / n] : "), read(GettingLighter),
((GettingLighter = 'y' ; GettingLighter = 'Y') -> assert(weightLoss(P)) ; true),
% Check for Family History
write("Do you have family history of Diabetes? [y / n] : "), read(FamHistoryDiabetes),
((FamHistoryDiabetes = 'y' ; FamHistoryDiabetes = 'Y') -> assert(diabetesFamily(P)) ; true),
% Get Diabetes Certainty
(diabetes(P, DiabetesCertainty) ; true),
% Check if any certainties are over 75, if not emergency
(not(emergency(P)) -> (
((HypertensionCertainty >= 75) -> assert(diagnosis(P, "Hypertension", HypertensionCertainty)) ; true), !,
((DiabetesCertainty >= 75) -> assert(diagnosis(P, "Diabetes", DiabetesCertainty)) ; true), !
); true), !,
% If emergency, store diagnosis anyway
((emergency(P) -> (
assert(diagnosis(P, "Hypertension", HypertensionCertainty)),
assert(diagnosis(P, "Diabetes", DiabetesCertainty))
)); true), !.
/* Get the Patient's Diagnosis Summary */
diagnosedSummary(P) :-
% EMERGENCY
(emergency(P) -> (
write("EMERGENCY DIAGNOSIS\n"),
% Display all the listed Diagnosis
getTopDiagnosis(P, [Name | [Certainty | _]]),
(((Certainty > 0) -> (
write_ln("\n\nYou have been diagnosed with the following:"),
format("~s with a certainty level of ~w%~n", [Name, Certainty])
)) ; true), !
) ; true),
% NON-EMERGENCY
(not(emergency(P)) -> (
% Check if there is a valid diagnosis
((not(diagnosis(P, _, _)) -> write_ln("This system cannot provide a diagnosis. Please visit a higher health institution.")); true), !,
% Display only the top diagnosis
((diagnosis(P, _, _) -> (
% Get the top
getTopDiagnosis(P, [Name | [Certainty | _]]),
write_ln("\n\nYou have been diagnosed with the following:"),
format("~s with a certainty level of ~w%~n", [Name, Certainty])
)) ; true), !
) ; true).
/* UTILITY PREDICATES */
% Print Diagnosis
printAllDiseases(P) :-
listDiseases(P, DiseaseList),
printList(DiseaseList).
% Get top diagnosis
getTopDiagnosis(P, Disease) :-
listDiseases(P, DiseaseList),
listMax(DiseaseList, Disease), !.
% Get the maximum in the disease list
listMax([X],X) :- !, true.
listMax([[_|C]|R], [Mn|Mc]) :-
listMax(R, [Mn|Mc]), Mc >= C.
listMax([[N|C]|R], [N|C]) :-
listMax(R, [_|Mc]),
C > Mc.
% Get the disease list
listDiseases(P, []) :- not(diagnosis(P,_,_)), !.
listDiseases(P, [Disease | Tail]) :-
retract(diagnosis(P, D, C)), !,
Disease = [D,C],
listDiseases(P, Tail), !.
% Print a List
printList([]).
printList([[N | [C | _]] | T]) :-
format("~s with a certainty of ~w%~n", [N,C]),
printList(T).