File size: 10,107 Bytes
be1b5d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# 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


@dataclass
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


@dataclass
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)