V3
Browse files- src/text_processors.py +0 -158
src/text_processors.py
DELETED
@@ -1,158 +0,0 @@
|
|
1 |
-
import re
|
2 |
-
|
3 |
-
def has_meaningful_content(text):
|
4 |
-
"""Check if explanation has meaningful content."""
|
5 |
-
if not text:
|
6 |
-
return False
|
7 |
-
|
8 |
-
# Check if the text is just equal signs or other separators
|
9 |
-
stripped_text = text.strip()
|
10 |
-
if re.match(r'^[=\-_*]+$', stripped_text.replace('\n', '')):
|
11 |
-
return False
|
12 |
-
|
13 |
-
# Check if the text only contains "## REASONING" with no actual content
|
14 |
-
if "## REASONING" in stripped_text and len(stripped_text) < 20:
|
15 |
-
return False
|
16 |
-
|
17 |
-
return True
|
18 |
-
|
19 |
-
|
20 |
-
def remove_reasoning_and_sources(text):
|
21 |
-
"""Remove reasoning and sources sections from the main response text."""
|
22 |
-
# First, remove any reasoning sections
|
23 |
-
pattern_reasoning = r'(?i)(\n+\s*reasoning:|\n+\s*\*{0,2}reasoning\*{0,2}:?|\n+\s*#{1,3}\s*reasoning).*?(?=\n+\s*(?:#{1,3}|sources:|references:|\Z))'
|
24 |
-
cleaned_text = re.sub(pattern_reasoning, '', text, flags=re.DOTALL)
|
25 |
-
|
26 |
-
# Then, remove any sources/references sections
|
27 |
-
pattern_sources = r'(?i)(\n+\s*sources:|\n+\s*references:|\n+\s*\*{0,2}sources\*{0,2}:?|\n+\s*\*{0,2}references\*{0,2}:?|\n+\s*#{1,3}\s*sources|\n+\s*#{1,3}\s*references).*?(?=\n+\s*(?:#{1,3}|\Z))'
|
28 |
-
cleaned_text = re.sub(pattern_sources, '', cleaned_text, flags=re.DOTALL)
|
29 |
-
|
30 |
-
# Also remove any source citations in the text (e.g., [1], [source_id])
|
31 |
-
cleaned_text = re.sub(r'\[([\w\d:_\-\.+]+)\]', '', cleaned_text)
|
32 |
-
|
33 |
-
# Process line by line to handle sections more comprehensively
|
34 |
-
lines = cleaned_text.split('\n')
|
35 |
-
filtered_lines = []
|
36 |
-
skip_section = False
|
37 |
-
|
38 |
-
for line in lines:
|
39 |
-
# Check if we should skip this line (part of reasoning or sources)
|
40 |
-
if re.search(r'(?i)^(\s*reasoning:|\s*sources:|\s*references:|\s*\*{0,2}reasoning\*{0,2}:?|\s*\*{0,2}sources\*{0,2}:?|\s*\*{0,2}references\*{0,2}:?|\s*#{1,3}\s*reasoning|\s*#{1,3}\s*sources|\s*#{1,3}\s*references)', line):
|
41 |
-
skip_section = True
|
42 |
-
continue
|
43 |
-
# Check if we're entering a new section
|
44 |
-
elif skip_section and re.search(r'(?i)^(\s*#{1,3}|\s*[a-zA-Z]+:)', line):
|
45 |
-
skip_section = False
|
46 |
-
|
47 |
-
# Only keep lines that aren't in sections we want to skip
|
48 |
-
if not skip_section:
|
49 |
-
filtered_lines.append(line)
|
50 |
-
|
51 |
-
# Remove any trailing URL citations that might be left
|
52 |
-
result = '\n'.join(filtered_lines).strip()
|
53 |
-
result = re.sub(r'\[([^\]]+)\]\(https?://[^)]+\)', r'\1', result)
|
54 |
-
|
55 |
-
return result
|
56 |
-
|
57 |
-
|
58 |
-
def clean_explanation(text):
|
59 |
-
"""Remove duplicate sources sections and data availability notes from explanation."""
|
60 |
-
if not text:
|
61 |
-
return text
|
62 |
-
|
63 |
-
# Remove DATA AVAILABILITY NOTE section
|
64 |
-
pattern_data_note = r'\n+\s*#{1,3}\s*DATA AVAILABILITY NOTE.*?(?=\n+\s*#{1,3}|\Z)'
|
65 |
-
cleaned_text = re.sub(pattern_data_note, '', text, flags=re.DOTALL)
|
66 |
-
|
67 |
-
# Fix formatting issues with reasoning points - ensure consistent formatting
|
68 |
-
pattern_reasoning_headers = r'(#{1,3}\s*REASONING[^#]*?)#{1,3}\s*(\d+\.\s+)'
|
69 |
-
cleaned_text = re.sub(pattern_reasoning_headers, r'\1\2', cleaned_text, flags=re.DOTALL)
|
70 |
-
|
71 |
-
# Remove any "REASONING1." pattern which creates the heading effect
|
72 |
-
cleaned_text = re.sub(r'(#{1,3}\s*REASONING)(\d+\.)', r'\1', cleaned_text)
|
73 |
-
|
74 |
-
# Normalize all reasoning points to use the same format
|
75 |
-
cleaned_text = re.sub(r'(\n+)(\d+\.)', r'\1 \2', cleaned_text)
|
76 |
-
|
77 |
-
# SIMPLER APPROACH: Remove all sources sections except the last one
|
78 |
-
# First, split the text by source section headers
|
79 |
-
pattern_sources = r'(\n+\s*#{1,3}\s+(?:SOURCES|Sources)(?:\s+USED)?[^\n]*)'
|
80 |
-
sections = re.split(pattern_sources, cleaned_text)
|
81 |
-
|
82 |
-
# Find all source sections
|
83 |
-
source_sections = []
|
84 |
-
current_section = ""
|
85 |
-
in_source = False
|
86 |
-
source_content = ""
|
87 |
-
|
88 |
-
for i, section in enumerate(sections):
|
89 |
-
# If this is a source section header
|
90 |
-
if re.match(r'\s*#{1,3}\s+(?:SOURCES|Sources)(?:\s+USED)?', section.strip()):
|
91 |
-
in_source = True
|
92 |
-
current_section = section
|
93 |
-
# If this is content after a source header
|
94 |
-
elif in_source and i > 0:
|
95 |
-
source_content = section
|
96 |
-
current_section += section
|
97 |
-
source_sections.append(current_section)
|
98 |
-
in_source = False
|
99 |
-
current_section = ""
|
100 |
-
|
101 |
-
# Remove all sources sections from the text
|
102 |
-
for section in source_sections:
|
103 |
-
cleaned_text = cleaned_text.replace(section, '')
|
104 |
-
|
105 |
-
# Clean up any double newlines
|
106 |
-
cleaned_text = re.sub(r'\n{3,}', '\n\n', cleaned_text)
|
107 |
-
|
108 |
-
# Add the sources section back with a consistent heading
|
109 |
-
if source_content.strip():
|
110 |
-
# Extract just the content without the header
|
111 |
-
source_content = source_content.strip()
|
112 |
-
|
113 |
-
# If the source content starts with bullet points, make sure they're properly formatted
|
114 |
-
source_content = re.sub(r'^(\s*)(\d+\.)', r'\1•', source_content, flags=re.MULTILINE)
|
115 |
-
|
116 |
-
# Add a clean, consistent "Sources" heading
|
117 |
-
cleaned_text = cleaned_text.strip()
|
118 |
-
if cleaned_text:
|
119 |
-
cleaned_text += "\n\n"
|
120 |
-
cleaned_text += "## Sources\n" + source_content
|
121 |
-
|
122 |
-
return cleaned_text.strip()
|
123 |
-
|
124 |
-
|
125 |
-
def format_conversation_history(history, patient_info=None):
|
126 |
-
"""
|
127 |
-
Format the conversation history into a string suitable for LLM processing.
|
128 |
-
Optionally adds patient info at the beginning.
|
129 |
-
"""
|
130 |
-
formatted_text = "# Medical Consultation\n\n"
|
131 |
-
|
132 |
-
# Add patient info if provided
|
133 |
-
if patient_info:
|
134 |
-
formatted_text += "## Patient Information\n"
|
135 |
-
formatted_text += f"* Name: {patient_info.get('name', '')}\n"
|
136 |
-
formatted_text += f"* Age: {patient_info.get('age', '')}\n"
|
137 |
-
formatted_text += f"* Gender: {patient_info.get('gender', '')}\n\n"
|
138 |
-
|
139 |
-
formatted_text += "## Conversation Transcript\n\n"
|
140 |
-
|
141 |
-
for message in history:
|
142 |
-
role = message.get("role", "").strip()
|
143 |
-
content = message.get("content", "").strip()
|
144 |
-
|
145 |
-
if not content:
|
146 |
-
continue # Skip empty messages
|
147 |
-
|
148 |
-
if role.lower() == "user":
|
149 |
-
formatted_text += f"PATIENT: {content}\n\n"
|
150 |
-
elif role.lower() == "assistant":
|
151 |
-
formatted_text += f"ASSISTANT: {content}\n\n"
|
152 |
-
# Include explanations which often contain diagnostic reasoning
|
153 |
-
if "explanation" in message and message["explanation"]:
|
154 |
-
explanation = message.get("explanation", "").strip()
|
155 |
-
if explanation:
|
156 |
-
formatted_text += f"REASONING: {explanation}\n\n"
|
157 |
-
|
158 |
-
return formatted_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|