-
Notifications
You must be signed in to change notification settings - Fork 8
/
greetings.py
421 lines (389 loc) · 12.5 KB
/
greetings.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
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
from experta import *
class Greetings(KnowledgeEngine):
def __init__(self, symptom_map, if_not_matched, get_treatments, get_details):
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)
#code giving instructions on how to use the Expert System
@DefFacts()
def _initial_action(self):
print("")
print("This is a knowledge based bot to diagnose diseases")
print("")
print("Do you feel any of the following symptoms?")
print("Reply high or low or no")
print("")
yield Fact(action="find_disease")
#taking various input from user
@Rule(Fact(action="find_disease"), NOT(Fact(headache=W())), salience=4)
def symptom_0(self):
self.declare(Fact(headache=input("headache: ")))
@Rule(Fact(action="find_disease"), NOT(Fact(back_pain=W())), salience=1)
def symptom_1(self):
self.declare(Fact(back_pain=input("back pain: ")))
@Rule(Fact(action="find_disease"), NOT(Fact(chest_pain=W())), salience=1)
def symptom_2(self):
self.declare(Fact(chest_pain=input("chest pain: ")))
@Rule(Fact(action="find_disease"), NOT(Fact(cough=W())), salience=3)
def symptom_3(self):
self.declare(Fact(cough=input("cough: ")))
@Rule(Fact(action="find_disease"), NOT(Fact(fainting=W())), salience=1)
def symptom_4(self):
self.declare(Fact(fainting=input("fainting: ")))
@Rule(Fact(action="find_disease"), NOT(Fact(fatigue=W())), salience=1)
def symptom_5(self):
self.declare(Fact(fatigue=input("fatigue: ")))
@Rule(Fact(action="find_disease"), NOT(Fact(sunken_eyes=W())), salience=1)
def symptom_6(self):
self.declare(Fact(sunken_eyes=input("sunken eyes: ")))
@Rule(Fact(action="find_disease"), NOT(Fact(low_body_temp=W())), salience=1)
def symptom_7(self):
self.declare(Fact(low_body_temp=input("low body temperature: ")))
@Rule(Fact(action="find_disease"), NOT(Fact(restlessness=W())), salience=1)
def symptom_8(self):
self.declare(Fact(restlessness=input("restlessness: ")))
@Rule(Fact(action="find_disease"), NOT(Fact(sore_throat=W())), salience=1)
def symptom_9(self):
self.declare(Fact(sore_throat=input("sore throat: ")))
@Rule(Fact(action="find_disease"), NOT(Fact(fever=W())), salience=1)
def symptom_10(self):
self.declare(Fact(fever=input("fever: ")))
@Rule(Fact(action="find_disease"), NOT(Fact(nausea=W())), salience=1)
def symptom_11(self):
self.declare(Fact(nausea=input("nausea: ")))
@Rule(Fact(action="find_disease"), NOT(Fact(blurred_vision=W())), salience=1)
def symptom_12(self):
self.declare(Fact(blurred_vision=input("blurred_vision: ")))
#different rules checking for each disease match
@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="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
@Rule(Fact(action="find_disease"), Fact(disease=MATCH.disease), salience=-998)
def disease(self, disease):
print("")
id_disease = disease
disease_details = self.get_details(id_disease)
treatments = self.get_treatments(id_disease)
print("")
print("Your symptoms match %s\n" % (id_disease))
print("A short description of the disease is given below :\n")
print(disease_details + "\n")
print(
"The common medications and procedures suggested by other real doctors are: \n"
)
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)
for j in range(0, len(lis)):
if temp_list[j] == lis[j] and (lis[j] == "high" or lis[j] == "low" or lis[j] == "yes"):
count = count + 1
if count > max_count:
max_count = count
max_disease = val
if max_disease != "":
self.if_not_matched(max_disease)