Spaces:
Sleeping
Sleeping
analysis improvement
Browse files- database.py +221 -71
database.py
CHANGED
|
@@ -1,91 +1,241 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
-
Based on Kent's Repertory and Materia Medica
|
| 4 |
"""
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
REMEDY_DATABASE = {
|
| 7 |
"Arnica Montana": {
|
|
|
|
|
|
|
|
|
|
| 8 |
"description": "First remedy for trauma, injuries, and shock",
|
| 9 |
-
"indications": [
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
],
|
| 13 |
-
"modalities": {
|
| 14 |
-
"worse": ["Touch", "Motion", "Cold damp", "Rest"],
|
| 15 |
-
"better": ["Lying down", "Cold applications", "Warmth"]
|
| 16 |
-
},
|
| 17 |
-
"mental": "Apathetic, denies illness, wants solitude, says 'I'm fine'",
|
| 18 |
-
"physical": "Sore bruised feeling, bed feels hard, black and blue spots",
|
| 19 |
-
"constitutional": "Robust, sanguine temperament, active individuals",
|
| 20 |
-
"potencies": {
|
| 21 |
-
"acute": ["6C", "30C", "200C"],
|
| 22 |
-
"chronic": ["200C", "1M", "10M"],
|
| 23 |
-
"first_aid": "30C",
|
| 24 |
-
"recommended": "30C for acute, 200C for chronic"
|
| 25 |
-
},
|
| 26 |
-
"dosage": "3-5 pellets every 2-4 hours for acute, once daily for chronic",
|
| 27 |
-
"duration": "48 hours for acute, 4-6 weeks for chronic",
|
| 28 |
-
"aggravation": "Possible initial aggravation",
|
| 29 |
-
"antidotes": ["Camphor", "Coffee"],
|
| 30 |
-
"complementary": ["Aconite", "Hypericum", "Ruta"],
|
| 31 |
-
"miasm": "Traumatic"
|
| 32 |
},
|
| 33 |
"Belladonna": {
|
|
|
|
|
|
|
|
|
|
| 34 |
"description": "Sudden violent onset with intense symptoms",
|
| 35 |
-
"indications": [
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
],
|
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-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
def
|
| 90 |
-
"""Get
|
| 91 |
-
return REMEDY_DATABASE.
|
|
|
|
| 1 |
"""
|
| 2 |
+
Comprehensive Homeopathic Medicine Database with Common Misspellings
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
+
import json
|
| 6 |
+
from typing import Dict, List, Tuple
|
| 7 |
+
|
| 8 |
+
# Main remedy database with common misspellings
|
| 9 |
REMEDY_DATABASE = {
|
| 10 |
"Arnica Montana": {
|
| 11 |
+
"correct_name": "Arnica Montana",
|
| 12 |
+
"common_names": ["Arnica", "Mountain Tobacco", "Leopard's Bane"],
|
| 13 |
+
"common_misspellings": ["Arnika", "Arnicha", "Arnoca", "Arnicum"],
|
| 14 |
"description": "First remedy for trauma, injuries, and shock",
|
| 15 |
+
"indications": ["Physical trauma", "Bruising", "Muscle soreness", "Post-surgical", "Shock"],
|
| 16 |
+
"potencies": ["6C", "30C", "200C", "1M"],
|
| 17 |
+
"dosage": "3-5 pellets every 2-4 hours for acute"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
},
|
| 19 |
"Belladonna": {
|
| 20 |
+
"correct_name": "Belladonna",
|
| 21 |
+
"common_names": ["Deadly Nightshade"],
|
| 22 |
+
"common_misspellings": ["Belladona", "Beladona", "Belladone"],
|
| 23 |
"description": "Sudden violent onset with intense symptoms",
|
| 24 |
+
"indications": ["Sudden fever", "Throbbing headache", "Acute inflammation"],
|
| 25 |
+
"potencies": ["30C", "200C", "1M"],
|
| 26 |
+
"dosage": "Every 1-2 hours in acute fever"
|
| 27 |
+
},
|
| 28 |
+
"Argentum Nitricum": {
|
| 29 |
+
"correct_name": "Argentum Nitricum",
|
| 30 |
+
"common_names": ["Silver Nitrate"],
|
| 31 |
+
"common_misspellings": [
|
| 32 |
+
"Argentic Nitricum", "Argentum Nitric", "Argent Nitricum",
|
| 33 |
+
"Argentum Nitrate", "Argentic Nitrate", "Argent Nitric"
|
| 34 |
],
|
| 35 |
+
"description": "Anxiety, anticipation, digestive issues",
|
| 36 |
+
"indications": ["Anxiety", "Stage fright", "Diarrhea from anxiety", "Vertigo"],
|
| 37 |
+
"potencies": ["30C", "200C", "1M"],
|
| 38 |
+
"dosage": "3 times daily for anxiety"
|
| 39 |
+
},
|
| 40 |
+
"Nux Vomica": {
|
| 41 |
+
"correct_name": "Nux Vomica",
|
| 42 |
+
"common_names": ["Poison Nut", "Quaker Buttons"],
|
| 43 |
+
"common_misspellings": ["Nuxvomica", "Nux Vomic", "Nuxvomic", "Nux Vom"],
|
| 44 |
+
"description": "Irritable, impatient, digestive complaints",
|
| 45 |
+
"indications": ["Digestive disorders", "Overindulgence", "Insomnia", "Irritability"],
|
| 46 |
+
"potencies": ["6C", "30C", "200C"],
|
| 47 |
+
"dosage": "3 times daily for digestive issues"
|
| 48 |
+
},
|
| 49 |
+
"Pulsatilla": {
|
| 50 |
+
"correct_name": "Pulsatilla",
|
| 51 |
+
"common_names": ["Wind Flower", "Pasque Flower"],
|
| 52 |
+
"common_misspellings": ["Pulsatila", "Pulsitilla", "Pulsitila", "Pulsatella"],
|
| 53 |
+
"description": "Mild, yielding, changeable symptoms",
|
| 54 |
+
"indications": ["Changeable symptoms", "Digestive issues", "Colds", "Weepy"],
|
| 55 |
+
"potencies": ["6C", "30C", "200C"],
|
| 56 |
+
"dosage": "Every 4 hours for acute"
|
| 57 |
+
},
|
| 58 |
+
# Additional 15 common remedies
|
| 59 |
+
"Bryonia Alba": {
|
| 60 |
+
"correct_name": "Bryonia Alba",
|
| 61 |
+
"common_names": ["White Bryony", "Wild Hops"],
|
| 62 |
+
"common_misspellings": ["Briyonia", "Bryonia", "Bironia", "Bryonia Alba"],
|
| 63 |
+
"description": "Irritable, thirsty, worse from motion",
|
| 64 |
+
"indications": ["Dry mucous", "Joint pains", "Constipation", "Pleurisy"],
|
| 65 |
+
"potencies": ["6C", "30C", "200C"],
|
| 66 |
+
"dosage": "Every 4 hours"
|
| 67 |
+
},
|
| 68 |
+
"Aconitum Napellus": {
|
| 69 |
+
"correct_name": "Aconitum Napellus",
|
| 70 |
+
"common_names": ["Monkshood", "Aconite"],
|
| 71 |
+
"common_misspellings": ["Aconite", "Aconitum", "Aconite Napellus", "Aconiteum"],
|
| 72 |
+
"description": "Sudden onset after fright, intense anxiety",
|
| 73 |
+
"indications": ["Sudden fever", "Panic attacks", "Croup", "Exposure"],
|
| 74 |
+
"potencies": ["30C", "200C", "1M"],
|
| 75 |
+
"dosage": "Every 30 minutes in acute panic"
|
| 76 |
+
},
|
| 77 |
+
"Chamomilla": {
|
| 78 |
+
"correct_name": "Chamomilla",
|
| 79 |
+
"common_names": ["German Chamomile"],
|
| 80 |
+
"common_misspellings": ["Camomilla", "Chamomile", "Chamomila", "Camomile"],
|
| 81 |
+
"description": "Extreme irritability, oversensitivity to pain",
|
| 82 |
+
"indications": ["Teething", "Colic", "Irritability", "Childbirth pain"],
|
| 83 |
+
"potencies": ["6C", "30C", "200C"],
|
| 84 |
+
"dosage": "Every 15-30 minutes for acute pain"
|
| 85 |
+
},
|
| 86 |
+
"Gelsemium": {
|
| 87 |
+
"correct_name": "Gelsemium",
|
| 88 |
+
"common_names": ["Yellow Jasmine"],
|
| 89 |
+
"common_misspellings": ["Gelsem", "Gelsimium", "Gelseminum", "Gelsemia"],
|
| 90 |
+
"description": "Weakness, trembling, anticipatory anxiety",
|
| 91 |
+
"indications": ["Flu", "Stage fright", "Headache", "Weakness"],
|
| 92 |
+
"potencies": ["30C", "200C", "1M"],
|
| 93 |
+
"dosage": "Every 2-4 hours"
|
| 94 |
+
},
|
| 95 |
+
"Ignatia": {
|
| 96 |
+
"correct_name": "Ignatia",
|
| 97 |
+
"common_names": ["St. Ignatius Bean"],
|
| 98 |
+
"common_misspellings": ["Ignatia Amara", "Ignat", "Ignatia", "Ignatiam"],
|
| 99 |
+
"description": "Acute grief, hysteria, contradictory symptoms",
|
| 100 |
+
"indications": ["Grief", "Hysteria", "Headache from grief", "Sighing"],
|
| 101 |
+
"potencies": ["30C", "200C", "1M"],
|
| 102 |
+
"dosage": "As needed for emotional distress"
|
| 103 |
+
},
|
| 104 |
+
"Phosphorus": {
|
| 105 |
+
"correct_name": "Phosphorus",
|
| 106 |
+
"common_names": [],
|
| 107 |
+
"common_misspellings": ["Phosporus", "Phos", "Phosphorous", "Phosph"],
|
| 108 |
+
"description": "Bleeding, fear, thirst for cold drinks",
|
| 109 |
+
"indications": ["Bleeding", "Anxiety", "Pneumonia", "Burning pains"],
|
| 110 |
+
"potencies": ["30C", "200C", "1M"],
|
| 111 |
+
"dosage": "3 times daily"
|
| 112 |
+
},
|
| 113 |
+
"Sepia": {
|
| 114 |
+
"correct_name": "Sepia",
|
| 115 |
+
"common_names": ["Cuttlefish Ink"],
|
| 116 |
+
"common_misspellings": ["Sep", "Sepiah", "Sapia", "Seppia"],
|
| 117 |
+
"description": "Indifference, bearing-down sensation",
|
| 118 |
+
"indications": ["Menopausal symptoms", "Indifference", "Prolapse", "Headache"],
|
| 119 |
+
"potencies": ["30C", "200C", "1M"],
|
| 120 |
+
"dosage": "Once daily or weekly"
|
| 121 |
},
|
| 122 |
+
"Silicea": {
|
| 123 |
+
"correct_name": "Silicea",
|
| 124 |
+
"common_names": ["Silica", "Flint"],
|
| 125 |
+
"common_misspellings": ["Silica", "Silice", "Silicaea", "Sil"],
|
| 126 |
+
"description": "Lack of confidence, suppurations, sensitive to cold",
|
| 127 |
+
"indications": ["Abscesses", "Lack of confidence", "Sweaty feet", "Headache"],
|
| 128 |
+
"potencies": ["30C", "200C", "1M"],
|
| 129 |
+
"dosage": "Once weekly"
|
| 130 |
+
},
|
| 131 |
+
"Sulphur": {
|
| 132 |
+
"correct_name": "Sulphur",
|
| 133 |
+
"common_names": ["Brimstone"],
|
| 134 |
+
"common_misspellings": ["Sulfur", "Sulpur", "Sulfer", "Sulph"],
|
| 135 |
+
"description": "Philosophical, untidy, skin complaints",
|
| 136 |
+
"indications": ["Skin disorders", "Itching", "Philosophical", "Hungry at 11am"],
|
| 137 |
+
"potencies": ["30C", "200C", "1M"],
|
| 138 |
+
"dosage": "Once weekly"
|
| 139 |
+
},
|
| 140 |
+
"Thuja": {
|
| 141 |
+
"correct_name": "Thuja",
|
| 142 |
+
"common_names": ["Arbor Vitae"],
|
| 143 |
+
"common_misspellings": ["Thuga", "Thuja Occidentalis", "Thuje", "Thuya"],
|
| 144 |
+
"description": "Fixed ideas, warts, vaccination effects",
|
| 145 |
+
"indications": ["Warts", "Vaccination effects", "Fixed ideas", "Skin growths"],
|
| 146 |
+
"potencies": ["30C", "200C", "1M"],
|
| 147 |
+
"dosage": "Once weekly"
|
| 148 |
+
}
|
| 149 |
}
|
| 150 |
|
| 151 |
+
def search_remedy_by_name(query: str, use_spell_check: bool = True) -> List[Tuple[str, Dict, float]]:
|
| 152 |
+
"""
|
| 153 |
+
Search for remedies by name with spell correction
|
| 154 |
+
Returns: [(remedy_name, remedy_data, confidence_score)]
|
| 155 |
+
"""
|
| 156 |
+
query = query.strip().lower()
|
| 157 |
+
if not query or len(query) < 2:
|
| 158 |
return []
|
| 159 |
|
|
|
|
| 160 |
results = []
|
| 161 |
|
| 162 |
+
for remedy_name, remedy_data in REMEDY_DATABASE.items():
|
| 163 |
+
confidence = 0
|
| 164 |
+
|
| 165 |
+
# 1. Exact match (highest confidence)
|
| 166 |
+
if query == remedy_name.lower():
|
| 167 |
+
confidence = 100
|
| 168 |
+
|
| 169 |
+
# 2. Partial match in correct name
|
| 170 |
+
elif query in remedy_name.lower():
|
| 171 |
+
confidence = 80
|
| 172 |
+
|
| 173 |
+
# 3. Match in common names
|
| 174 |
+
elif any(query in name.lower() for name in remedy_data.get("common_names", [])):
|
| 175 |
+
confidence = 70
|
| 176 |
+
|
| 177 |
+
# 4. Spell correction matching
|
| 178 |
+
elif use_spell_check and _spell_correction_match(query, remedy_name, remedy_data):
|
| 179 |
+
confidence = _calculate_spell_confidence(query, remedy_name, remedy_data)
|
| 180 |
+
|
| 181 |
+
if confidence > 0:
|
| 182 |
+
results.append((remedy_name, remedy_data, confidence))
|
| 183 |
+
|
| 184 |
+
# Sort by confidence
|
| 185 |
+
results.sort(key=lambda x: x[2], reverse=True)
|
| 186 |
+
return results[:10]
|
| 187 |
+
|
| 188 |
+
def _spell_correction_match(query: str, remedy_name: str, remedy_data: Dict) -> bool:
|
| 189 |
+
"""Check if query matches through spell correction"""
|
| 190 |
+
# Check common misspellings
|
| 191 |
+
if query in [m.lower() for m in remedy_data.get("common_misspellings", [])]:
|
| 192 |
+
return True
|
| 193 |
+
|
| 194 |
+
# Simple Levenshtein-like check for short remedies
|
| 195 |
+
if len(query) > 3 and len(remedy_name) > 3:
|
| 196 |
+
# Check if query is close to remedy name
|
| 197 |
+
if _string_similarity(query, remedy_name.lower()) > 0.7:
|
| 198 |
+
return True
|
| 199 |
|
| 200 |
+
return False
|
| 201 |
+
|
| 202 |
+
def _calculate_spell_confidence(query: str, remedy_name: str, remedy_data: Dict) -> float:
|
| 203 |
+
"""Calculate confidence score for spell-corrected matches"""
|
| 204 |
+
# Exact misspelling match
|
| 205 |
+
if query in [m.lower() for m in remedy_data.get("common_misspellings", [])]:
|
| 206 |
+
return 90
|
| 207 |
|
| 208 |
+
# Calculate string similarity
|
| 209 |
+
similarity = _string_similarity(query, remedy_name.lower())
|
| 210 |
+
|
| 211 |
+
if similarity > 0.9:
|
| 212 |
+
return 85
|
| 213 |
+
elif similarity > 0.8:
|
| 214 |
+
return 75
|
| 215 |
+
elif similarity > 0.7:
|
| 216 |
+
return 65
|
| 217 |
+
else:
|
| 218 |
+
return 50
|
| 219 |
+
|
| 220 |
+
def _string_similarity(s1: str, s2: str) -> float:
|
| 221 |
+
"""Calculate simple string similarity"""
|
| 222 |
+
if not s1 or not s2:
|
| 223 |
+
return 0
|
| 224 |
+
|
| 225 |
+
# Common prefix length
|
| 226 |
+
prefix_len = 0
|
| 227 |
+
for i in range(min(len(s1), len(s2))):
|
| 228 |
+
if s1[i] == s2[i]:
|
| 229 |
+
prefix_len += 1
|
| 230 |
+
else:
|
| 231 |
+
break
|
| 232 |
+
|
| 233 |
+
return prefix_len / max(len(s1), len(s2))
|
| 234 |
+
|
| 235 |
+
def get_remedy_details(name: str) -> Dict:
|
| 236 |
+
"""Get complete remedy details"""
|
| 237 |
+
return REMEDY_DATABASE.get(name)
|
| 238 |
|
| 239 |
+
def get_all_remedies() -> List[str]:
|
| 240 |
+
"""Get list of all remedy names"""
|
| 241 |
+
return list(REMEDY_DATABASE.keys())
|