"""Add spelling annotations to quoted text.""" import re from .add_spelling import add_spelling QUOTED = re.compile(r'"([^"\n]*)"|“([^”\n]*)”') ANNOTATION = re.compile(r'\s*\(spelled: [^()\n]+\)') def augment_caption(caption: str) -> str: """Annotate quoted text once, including individual Chinese characters. Use the English spelling helper first, then annotate any remaining quoted text with a space between characters and a slash between words. """ if not QUOTED.search(caption): return caption augmented = add_spelling(caption) def annotate_remaining(match: re.Match) -> str: target = match.group(1) if match.group(1) is not None else match.group(2) if not target.strip() or ANNOTATION.match(augmented, match.end()): return match.group(0) spelled = ' / '.join(' '.join(word) for word in target.split()) return f'{match.group(0)} (spelled: {spelled})' return QUOTED.sub(annotate_remaining, augmented)