Spaces:
Sleeping
Sleeping
Create diagnosis_module.py
Browse files- diagnosis_module.py +25 -0
diagnosis_module.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from difflib import SequenceMatcher
|
| 3 |
+
|
| 4 |
+
def load_diseases():
|
| 5 |
+
with open("diseases.json", "r", encoding="utf-8") as f:
|
| 6 |
+
return json.load(f)
|
| 7 |
+
|
| 8 |
+
def diagnose_symptoms(user_input):
|
| 9 |
+
diseases = load_diseases()
|
| 10 |
+
results = []
|
| 11 |
+
user_symptoms = [s.strip() for s in user_input.split(",")]
|
| 12 |
+
for disease, info in diseases.items():
|
| 13 |
+
match_score = sum(
|
| 14 |
+
SequenceMatcher(None, sym.lower(), us.lower()).ratio()
|
| 15 |
+
for sym in info["symptoms"]
|
| 16 |
+
for us in user_symptoms
|
| 17 |
+
) / (len(info["symptoms"]) * len(user_symptoms))
|
| 18 |
+
if match_score > 0.2:
|
| 19 |
+
results.append({
|
| 20 |
+
"disease": disease,
|
| 21 |
+
"score": match_score,
|
| 22 |
+
"source": info["source"]
|
| 23 |
+
})
|
| 24 |
+
results.sort(key=lambda x: x["score"], reverse=True)
|
| 25 |
+
return results[:5]
|