| """Sentence-scoped grammar repair after structural rewrite.""" | |
| from __future__ import annotations | |
| import re | |
| from app.pipeline.grammar_fix import correct_text | |
| def repair_sentence(text: str) -> str: | |
| """Fix punctuation, capitalization, agreement, spacing on one sentence.""" | |
| raw = (text or "").strip() | |
| if not raw: | |
| return raw | |
| s = re.sub(r"\s+", " ", raw).strip() | |
| s = re.sub(r"\s+([,.;:!?])", r"\1", s) | |
| s = re.sub(r",\s*,+", ",", s) | |
| s = re.sub(r"\s+,", ",", s) | |
| if s and s[0].islower(): | |
| s = s[0].upper() + s[1:] | |
| try: | |
| fixed = correct_text(s) | |
| if fixed and fixed.strip(): | |
| out = fixed.strip() | |
| if abs(len(out.split()) - len(s.split())) <= max(3, len(s.split()) // 2): | |
| return out | |
| except Exception: | |
| pass | |
| return s | |