-
Notifications
You must be signed in to change notification settings - Fork 1
/
ColdOrFlu.py
234 lines (201 loc) · 7.34 KB
/
ColdOrFlu.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
from pyknow import *
#This system has capable of diagnosing respiratory illnesses and
# decide if someone is having a cold or flu.
# The system requests the user to enter the symptoms as input and
# the system diagnosis accurately even if the user entered any
# synonyms of the symptoms. Then it checks if the symptom is flu
# symptom or cold symptom to determine which counter that need to
# increase flu or cold counter, it does the same step with all symptoms.
# Also, if the user enters less than 3 symptoms, it asked to enter more
# symptoms to diagnose their state. Finally, it compares between flu and
# cold counter to print the diagnosis result.
class FluOrCold(KnowledgeEngine):
@DefFacts()
def symptoms(self):
yield Fact(action="flu_or_cold")
#################
#Cold Symptoms
#Fever
@Rule(Fact(action='flu_or_cold'),
OR(Fact(symptom="no fever"),
Fact(symptom="mild fever"),
Fact(symptom="no temperature"),
Fact(symptom="mild temperature"),
Fact(symptom="no hyperthermia"),
Fact(symptom="mild hyperthermia"),
Fact(symptom="no pyrexia"),
Fact(symptom="mild pyrexia")))
def fever_cold(self):
global cold_symptoms
cold_symptoms +=1
#Coughing
@Rule(Fact(action='flu_or_cold'),
OR(Fact(symptom="producing cough"),
Fact(symptom="mucus cough")))
def coughing_cold(self):
global cold_symptoms
cold_symptoms +=1
#Nasal discharge
@Rule(Fact(action='flu_or_cold'),
OR(Fact(symptom="stuffy nose"),
Fact(symptom="runny nose")))
def nasal_discharge_cold(self):
global cold_symptoms
cold_symptoms +=1
#Tiredness
@Rule(Fact(action='flu_or_cold'),
OR(Fact(symptom="mild tiredness"),
Fact(symptom="mild fatigue"),
Fact(symptom="mild exhaustion")))
def tiredness_cold(self):
global cold_symptoms
cold_symptoms +=1
#Headache
@Rule(Fact(action='flu_or_cold'),
OR(Fact(symptom="headache"),
Fact(symptom="mild headache"),
Fact(symptom="mild migraine"),
Fact(symptom="mild head pain")), salience=0)
def headache_cold(self):
global cold_symptoms
cold_symptoms +=1
#Dizziness
@Rule(Fact(action='flu_or_cold'),
OR(Fact(symptom="no lightheadedness"),
Fact(symptom="no syncope"),
Fact(symptom="no fainting"),
Fact(symptom="no dizziness")))
def dizziness_flu(self):
global cold_symptoms
cold_symptoms +=1
#Nausea
@Rule(Fact(action='flu_or_cold'),
OR(Fact(symptom="no nausea"),
Fact(symptom="no vomiting"),
Fact(symptom="no stomach upset"),
Fact(symptom="no sickness"),
Fact(symptom="no low appetite")))
def nausea_cold(self):
global cold_symptoms
cold_symptoms +=1
#################
#Cold Symptoms
#Fever
@Rule(Fact(action='flu_or_cold'),
OR(Fact(symptom="high fever"),
Fact(symptom="moderate fever"),
Fact(symptom="high temperature"),
Fact(symptom="moderate temperature"),
Fact(symptom="high hyperthermia"),
Fact(symptom="moderate hyperthermia"),
Fact(symptom="high pyrexia"),
Fact(symptom="moderate pyrexia")))
def fever_flu(self):
global flu_symptoms
flu_symptoms +=1
#Coughing
@Rule(Fact(action='flu_or_cold'),
Fact(symptom="dry cough"))
def coughing_flu(self):
global flu_symptoms
flu_symptoms +=1
#Nasal discharge
@Rule(Fact(action='flu_or_cold'),
Fact(symptom="runny nose"))
def nasal_discharge_flu(self):
global flu_symptoms
flu_symptoms +=1
#Tiredness
@Rule(Fact(action='flu_or_cold'),
OR(Fact(symptom="moderate tiredness"),
Fact(symptom="severe tiredness"),
Fact(symptom="moderate fatigue"),
Fact(symptom="severe fatigue"),
Fact(symptom="moderate exhaustion"),
Fact(symptom="severe exhaustion")))
def tiredness_flu(self):
global flu_symptoms
flu_symptoms +=1
#Headache
@Rule(Fact(action='flu_or_cold'),
OR(Fact(symptom="headache"),
Fact(symptom="moderate headache"),
Fact(symptom="severe headache"),
Fact(symptom="moderate migraine"),
Fact(symptom="severe migraine"),
Fact(symptom="moderate head pain"),
Fact(symptom="severe head pain")), salience=1)
def headache_flu(self):
global flu_symptoms
flu_symptoms +=1
#Dizziness
@Rule(Fact(action='flu_or_cold'),
OR(Fact(symptom="lightheadedness"),
Fact(symptom="syncope"),
Fact(symptom="fainting"),
Fact(symptom="dizziness")))
def dizziness_flu(self):
global flu_symptoms
flu_symptoms +=1
#Nausea
@Rule(Fact(action='flu_or_cold'),
OR(Fact(symptom="nausea"),
Fact(symptom="vomiting"),
Fact(symptom="stomach upset"),
Fact(symptom="sickness"),
Fact(symptom="low appetite")))
def nausea_cold(self):
global flu_symptoms
flu_symptoms +=1
#################
#Common Symptoms
#Sneezing
@Rule(Fact(action='flu_or_cold'),
Fact(symptom="sneezing"))
def sneezing_cold_flu(self):
global cold_symptoms
cold_symptoms +=1
global flu_symptoms
flu_symptoms +=1
#Sore Throat
@Rule(Fact(action='flu_or_cold'),
OR(Fact(symptom="pain throat"),
Fact(symptom="throat sore"),
Fact(symptom="sore throat")))
def sore_throat_cold_flu(self):
global cold_symptoms
cold_symptoms +=1
global flu_symptoms
flu_symptoms +=1
##################################################33
#Output
flu_symptoms =0
cold_symptoms =0
print("")
print(" Cold or Flu ".center(40, "*"))
print(" Note ".center(40, "*"))
print (" Enter one symptom in line ".center(40, "*"))
print ("Enter (I do not have) to show your state ".center(40, "*"))
print(" ")
engine = FluOrCold()
while(1):
print("What is your symptoms?")
symptom = input()
#Exit from loop if the input equal " i do not have"
if symptom.strip().lower() == "i do not have":
break
engine.reset() # Prepare the engine for the execution.
engine.declare(Fact(symptom=symptom.strip().lower()))
engine.run() # Run it
#Check if user enter less than 3 symptoms
if ((flu_symptoms < 3) and (cold_symptoms < 3)):
print("You must enter at least 3 symptoms")
#Cann't diagnose user state when the flu and cold symptoms are equal
elif((flu_symptoms== cold_symptoms)):
print("Cann't diagnose your state")
#If the flu counter greater than cold counter, then the user has flu
elif ((flu_symptoms > 3) and (flu_symptoms > cold_symptoms)):
print ("You have flu")
#If the cold counter greater than flu counter, then the user has cold
elif ((cold_symptoms > 3) and (flu_symptoms < cold_symptoms)):
print ("You have cold")