from typing import Dict, Any, List import json class StructuringEngine: """ Takes raw NLP entities and OCR text, mapping them into structured JSON that aligns with our PostgreSQL database schema (Medications, LabResults). """ @staticmethod def construct_report_payload(raw_text: str, nlp_entities: Dict[str, Any], evaluation_flags: List[Dict]) -> str: """ Constructs the final structured JSON object to be sent to the frontend and stored in the database. """ # In a real scenario, this engine would use regex/heuristics to pair # extracted chemical entities with their specific dosages (e.g. "Lisinopril 10mg") # from the surrounding text in the OCR output. medications = [] for chemical in nlp_entities.get("chemical_entity", []): medications.append({ "drug_name": chemical, "dosage": "Unknown (Requires Manual Verification)", # Extracted from context heuristic "frequency": "Unknown", "instructions": "As prescribed by physician" }) lab_results = nlp_entities.get("extracted_vitals", []) report = { "metadata": { "confidence_score": "High", # Based on OCR read certainty "source_text_length": len(raw_text) }, "extracted_data": { "medications": medications, "conditions": nlp_entities.get("disease_entity", []), "lab_results": lab_results }, "risk_analysis": { "flags": evaluation_flags, "summary": "High priority alerts detected." if any(f.get("severity") in ["EMERGENCY", "HIGH"] for f in evaluation_flags) else "No immediate risks detected." } } return json.dumps(report, indent=2)