Spaces:
Sleeping
Sleeping
| # provider_summary_formatter.py | |
| """ | |
| Provider Summary Formatter for Enhanced UI Display. | |
| This module provides formatting for provider summaries as coherent paragraphs | |
| according to the requirements for improved readability and consistency. | |
| Requirements: 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8 | |
| """ | |
| from typing import List, Optional, Dict, Any | |
| from dataclasses import dataclass | |
| from src.core.provider_summary_generator import ProviderSummary | |
| class PatientData: | |
| """Patient data for summary formatting.""" | |
| name: str | |
| age: int | |
| gender: str | |
| phone: str | |
| medical_history: List[str] | |
| expressed_concerns: List[str] | |
| patient_input: str | |
| class ClassificationData: | |
| """Classification data for summary formatting.""" | |
| classification: str # RED/YELLOW/GREEN | |
| spiritual_concern_type: str | |
| consent_given: bool | |
| class ProviderSummaryFormatter: | |
| """ | |
| Provider Summary Formatter for coherent paragraph generation. | |
| Formats provider summaries as single coherent paragraphs following | |
| the Medical Brain style as specified in requirements 2.1-2.8. | |
| """ | |
| def __init__(self): | |
| """Initialize the provider summary formatter.""" | |
| pass | |
| def format_coherent_paragraph( | |
| self, | |
| patient_data: PatientData, | |
| classification_data: ClassificationData | |
| ) -> str: | |
| """ | |
| Format provider summary as a single coherent paragraph. | |
| Args: | |
| patient_data: Patient information and context | |
| classification_data: Classification and consent information | |
| Returns: | |
| Single coherent paragraph formatted for provider summary | |
| Requirements: 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8 | |
| """ | |
| # Build demographic section (Requirement 2.2) | |
| demographic_section = self.build_demographic_section(patient_data) | |
| # Build medical history section (Requirement 2.3) | |
| medical_section = self.build_medical_history_section(patient_data.medical_history) | |
| # Build spiritual concerns section (Requirement 2.4, 2.5) | |
| concerns_section = self.build_spiritual_concerns_section( | |
| patient_data.expressed_concerns, | |
| classification_data.spiritual_concern_type, | |
| classification_data.classification | |
| ) | |
| # Build consent and contact section (Requirement 2.6, 2.7) | |
| contact_section = self.build_contact_section( | |
| patient_data.phone, | |
| classification_data.consent_given | |
| ) | |
| # Combine all sections into coherent paragraph (Requirement 2.1) | |
| paragraph_parts = [ | |
| demographic_section, | |
| medical_section, | |
| concerns_section, | |
| contact_section | |
| ] | |
| # Filter out empty parts and join with appropriate connectors | |
| non_empty_parts = [part for part in paragraph_parts if part.strip()] | |
| if not non_empty_parts: | |
| return "No summary information available." | |
| # Join parts with appropriate connectors for flow | |
| coherent_paragraph = ". ".join(non_empty_parts) | |
| # Ensure proper sentence ending | |
| if not coherent_paragraph.endswith('.'): | |
| coherent_paragraph += '.' | |
| return coherent_paragraph | |
| def build_demographic_section(self, patient: PatientData) -> str: | |
| """ | |
| Build demographic information section. | |
| Args: | |
| patient: Patient data containing demographic information | |
| Returns: | |
| Formatted demographic section string | |
| Requirements: 2.2 | |
| """ | |
| # Format: "[patient name] is a X-year-old [gender]" | |
| gender_text = patient.gender.lower() if patient.gender else "individual" | |
| return f"{patient.name} is a {patient.age}-year-old {gender_text}" | |
| def build_medical_history_section(self, medical_history: List[str]) -> str: | |
| """ | |
| Build medical history section. | |
| Args: | |
| medical_history: List of medical conditions/history items | |
| Returns: | |
| Formatted medical history section string | |
| Requirements: 2.3 | |
| """ | |
| if not medical_history: | |
| return "with no significant medical history documented" | |
| # Format: "with clinical history of X, Y, and Z" | |
| if len(medical_history) == 1: | |
| history_text = medical_history[0] | |
| elif len(medical_history) == 2: | |
| history_text = f"{medical_history[0]} and {medical_history[1]}" | |
| else: | |
| # Multiple items: "X, Y, and Z" | |
| history_text = ", ".join(medical_history[:-1]) + f", and {medical_history[-1]}" | |
| return f"with clinical history of {history_text}" | |
| def build_spiritual_concerns_section( | |
| self, | |
| concerns: List[str], | |
| concern_type: str, | |
| classification: str | |
| ) -> str: | |
| """ | |
| Build spiritual concerns section. | |
| Args: | |
| concerns: List of expressed concerns | |
| concern_type: Type of spiritual concern identified | |
| classification: Classification level (RED/YELLOW/GREEN) | |
| Returns: | |
| Formatted spiritual concerns section string | |
| Requirements: 2.4, 2.5 | |
| """ | |
| if not concerns: | |
| concerns_text = "general distress" | |
| elif len(concerns) == 1: | |
| concerns_text = concerns[0] | |
| elif len(concerns) == 2: | |
| concerns_text = f"{concerns[0]} and {concerns[1]}" | |
| else: | |
| # Multiple concerns: "X, Y, and Z" | |
| concerns_text = ", ".join(concerns[:-1]) + f", and {concerns[-1]}" | |
| # Format: "The patient expressed X, Y, and Z, which may indicate [specific spiritual/emotional concern]" | |
| concern_description = concern_type if concern_type else "spiritual or emotional distress" | |
| concerns_section = f"The patient expressed {concerns_text}, which may indicate {concern_description}" | |
| # Add classification information (Requirement 2.5) | |
| flag_text = f"{classification} FLAG" if classification in ["RED", "YELLOW"] else "GREEN status" | |
| classification_section = f", resulting in generation of a {flag_text}" | |
| return concerns_section + classification_section | |
| def build_contact_section(self, phone: str, consent_given: bool) -> str: | |
| """ | |
| Build contact and consent section. | |
| Args: | |
| phone: Patient's phone number | |
| consent_given: Whether patient gave consent for spiritual care contact | |
| Returns: | |
| Formatted contact section string | |
| Requirements: 2.6, 2.7 | |
| """ | |
| sections = [] | |
| # Add consent information (Requirement 2.6) | |
| if consent_given: | |
| sections.append("The patient has given consent to be contacted by the spiritual care team") | |
| else: | |
| sections.append("The patient has not yet provided consent for spiritual care contact") | |
| # Add contact information (Requirement 2.7) | |
| if phone and phone.strip(): | |
| sections.append(f"The preferred contact number is {phone}") | |
| else: | |
| sections.append("No contact number is currently available") | |
| return ". ".join(sections) | |
| def add_patient_quote_section(self, quote: str) -> str: | |
| """ | |
| Add patient quote section as separate line. | |
| Args: | |
| quote: Direct quote from patient input | |
| Returns: | |
| Formatted patient quote section | |
| Requirements: 2.8 | |
| """ | |
| if not quote or not quote.strip(): | |
| return "" | |
| # Format: "Patient reported: [patient input here]" | |
| return f"Patient reported: \"{quote.strip()}\"" | |
| def format_enhanced_summary( | |
| self, | |
| patient_data: PatientData, | |
| classification_data: ClassificationData | |
| ) -> str: | |
| """ | |
| Format complete enhanced summary with paragraph and quote. | |
| Args: | |
| patient_data: Patient information and context | |
| classification_data: Classification and consent information | |
| Returns: | |
| Complete formatted summary with paragraph and quote sections | |
| Requirements: 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8 | |
| """ | |
| # Generate main coherent paragraph | |
| main_paragraph = self.format_coherent_paragraph(patient_data, classification_data) | |
| # Generate patient quote section (Requirement 2.8) | |
| quote_section = self.add_patient_quote_section(patient_data.patient_input) | |
| # Combine paragraph and quote | |
| if quote_section: | |
| return f"{main_paragraph}\n\n{quote_section}" | |
| else: | |
| return main_paragraph | |
| def format_from_provider_summary(self, summary: ProviderSummary) -> str: | |
| """ | |
| Format coherent paragraph from existing ProviderSummary object. | |
| This method bridges the existing ProviderSummary structure with the new | |
| coherent paragraph formatting requirements by using the enhanced method | |
| in ProviderSummaryGenerator. | |
| Args: | |
| summary: Existing ProviderSummary object | |
| Returns: | |
| Formatted coherent paragraph | |
| Requirements: 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8 | |
| """ | |
| # Import here to avoid circular imports | |
| from src.core.provider_summary_generator import ProviderSummaryGenerator | |
| # Create a temporary generator instance to use the enhanced formatting | |
| temp_generator = ProviderSummaryGenerator() | |
| # Use the enhanced format_coherent_paragraph method | |
| return temp_generator.format_coherent_paragraph(summary) |