File size: 858 Bytes
8f6d79d
 
 
 
 
 
 
4726a76
 
8f6d79d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39cfcd1
8f6d79d
 
 
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
"""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