""" Medical, surgical, family history, and previous episode extraction engine. """ import re from typing import Dict, Any, List, Optional, Tuple, Union from app.ai.extraction.negation_engine import extract_family_subject HISTORY_KEYWORDS = [ ("diabetes", "Diabetes"), ("sugar", "Diabetes"), ("diabetic", "Diabetes"), ("hypertension", "Hypertension (High BP)"), ("high bp", "Hypertension (High BP)"), ("bp", "Hypertension (High BP)"), ("asthma", "Asthma"), ("thyroid", "Thyroid disorder"), ("சர்க்கரை நோய்", "Diabetes"), ("சுகர்", "Diabetes"), ("பிபி", "Hypertension"), ("डायबिटीज", "Diabetes"), ("शुगर", "Diabetes"), ("बीपी", "Hypertension"), ] def extract_history( message: str, normalized_msg: str, duration: Optional[str] = None ) -> Dict[str, Any]: """ Extracts medical_history, family_history, surgical_history, previous_episodes, and additional_information. """ msg_lower = normalized_msg.lower() updates: Dict[str, Any] = {} # Check for explicit negation of medical history for self neg_history_phrases = [ "no past history", "no medical history", "no health conditions", "no bp no diabetes", "no chronic illness", "don't have diabetes", "no diabetes", "no bp", "எந்த நோயும் இல்லை", "வேற ஒன்னும் இல்லை", "பிபி சுகர் எதுவும் இல்ல", "कोई बीमारी नहीं", "बीमारी नहीं है", "ఏ సమస్య లేదు", "ಯಾವ ಕಾಯಿಲೆ ಇಲ್ಲ" ] # Check if patient mentions family member (e.g., "my father has diabetes") is_fam, relation, _ = extract_family_subject(message) if is_fam: fam_list: List[Dict[str, Any]] = [] for kw, cond in HISTORY_KEYWORDS: if re.search(rf"\b{re.escape(kw)}\b", msg_lower) or kw in message: fam_list.append({"relation": relation, "condition": cond}) if fam_list: updates["family_history"] = fam_list if any(neg in msg_lower for neg in neg_history_phrases): updates["medical_history"] = {"status": "confirmed_none", "items": []} elif not is_fam: med_history: List[Dict[str, Any]] = [] for kw, cond in HISTORY_KEYWORDS: if re.search(rf"\b{re.escape(kw)}\b", msg_lower) or kw in message: h_obj: Dict[str, Any] = {"condition": cond} h_dur_m = re.search(r"(?:for|since)\s+(\d+\s+(?:years|months|yrs))", msg_lower) if h_dur_m: h_obj["duration"] = h_dur_m.group(1).strip() if not any(x.get("condition") == cond for x in med_history): med_history.append(h_obj) if med_history: updates["medical_history"] = med_history # Surgical History & Previous Episodes if "surgery" in msg_lower or "operation" in msg_lower or "ஆபரேஷன்" in message: updates["surgical_history"] = [{"procedure": "Prior surgery reported", "approximate_date": duration or "Past"}] if "same problem" in msg_lower or "had this before" in msg_lower or "மூன்று மாதங்களுக்கு முன்" in message: updates["previous_episodes"] = [{"description": "Similar symptoms previously", "time": duration or "Past"}] # Additional Information / Functional Impact add_info: List[Dict[str, Any]] = [] if "couldn't eat" in msg_lower or "not eaten" in msg_lower or "சாப்பிட முடியல" in message: add_info.append({"category": "functional_impact", "value": "Loss of appetite / difficulty eating"}) if "couldn't sleep" in msg_lower or "haven't slept" in msg_lower or "தூங்க முடியல" in message: add_info.append({"category": "functional_impact", "value": "Sleep disruption"}) if "weak" in msg_lower or "tired" in msg_lower or "ரொம்ப டயர்டா இருக்கு" in message: add_info.append({"category": "functional_impact", "value": "Severe weakness / fatigue"}) if add_info: updates["additional_information"] = add_info return updates