Initial commit: Doctor-Patient clinical transcription and structured summary pipeline
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import re
|
||||
|
||||
def align_verbatim_transcript(raw_transcript, llm_output):
|
||||
"""
|
||||
Guarantees that the final dialogue text contains ONLY the exact original words
|
||||
from raw_transcript in exact original order, with only 'Doctor:' and 'Patient:' speaker tags inserted.
|
||||
"""
|
||||
raw_words = raw_transcript.strip().split()
|
||||
|
||||
# Extract speaker turns and their text from llm output
|
||||
lines = llm_output.split('\n')
|
||||
turns = []
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
doc_m = re.match(r'^\*{0,2}Doctor\*{0,2}\s*:\s*(.*)', line, re.I)
|
||||
pat_m = re.match(r'^\*{0,2}Patient\*{0,2}\s*:\s*(.*)', line, re.I)
|
||||
if doc_m:
|
||||
turns.append(('Doctor', doc_m.group(1).strip()))
|
||||
elif pat_m:
|
||||
turns.append(('Patient', pat_m.group(1).strip()))
|
||||
|
||||
if not turns:
|
||||
return f"**Doctor:** {raw_transcript}"
|
||||
|
||||
# Verify and reconstruct using strict raw words
|
||||
aligned_turns = []
|
||||
raw_idx = 0
|
||||
|
||||
for speaker, text in turns:
|
||||
turn_words = text.split()
|
||||
if not turn_words:
|
||||
continue
|
||||
|
||||
# Match as many words as possible from raw_words
|
||||
matched = []
|
||||
while raw_idx < len(raw_words):
|
||||
raw_w = raw_words[raw_idx]
|
||||
clean_raw = re.sub(r'[^\w\s]', '', raw_w).lower()
|
||||
|
||||
# Check if this raw word matches something in this turn
|
||||
found = False
|
||||
for tw in turn_words:
|
||||
clean_tw = re.sub(r'[^\w\s]', '', tw).lower()
|
||||
if clean_raw == clean_tw:
|
||||
found = True
|
||||
break
|
||||
|
||||
if found or len(matched) < len(turn_words):
|
||||
matched.append(raw_w)
|
||||
raw_idx += 1
|
||||
if len(matched) >= len(turn_words):
|
||||
break
|
||||
else:
|
||||
break
|
||||
|
||||
if matched:
|
||||
aligned_turns.append((speaker, ' '.join(matched)))
|
||||
|
||||
# Append any remaining raw words to the last speaker turn
|
||||
if raw_idx < len(raw_words):
|
||||
remaining = ' '.join(raw_words[raw_idx:])
|
||||
if aligned_turns:
|
||||
aligned_turns[-1] = (aligned_turns[-1][0], aligned_turns[-1][1] + ' ' + remaining)
|
||||
else:
|
||||
aligned_turns.append(('Doctor', remaining))
|
||||
|
||||
# Format result
|
||||
formatted = '\n\n'.join(f"**{sp}:** {txt}" for sp, txt in aligned_turns)
|
||||
return formatted
|
||||
|
||||
raw = "नमस्कार या बसा काय त्रास होतोय तुम्हाला मला गेले दोन दिवस झाले पोटात दुखत आहे"
|
||||
llm_out = """**Doctor:** नमस्कार या बसा काय त्रास होतोय तुम्हाला?
|
||||
**Patient:** मला गेले दोन दिवस झाले पोटात दुखत आहे."""
|
||||
|
||||
print("Result:")
|
||||
print(align_verbatim_transcript(raw, llm_out))
|
||||
Reference in New Issue
Block a user