File size: 2,407 Bytes
c29fb5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1447cb
 
c29fb5e
 
 
 
 
 
 
 
c1447cb
 
c29fb5e
c1447cb
 
c29fb5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1447cb
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Light post-rewrite cleanup and length control."""

from __future__ import annotations

import re


def tidy(text: str) -> str:
    """Normalize whitespace without flattening document structure."""
    value = re.sub(r"\n{3,}", "\n\n", text or "")
    value = re.sub(r"[ \t]{2,}", " ", value)
    value = re.sub(r'([,;:])(["“])', r"\1 \2", value)
    paragraphs = [part.strip() for part in re.split(r"\n\s*\n", value) if part.strip()]
    return "\n\n".join(paragraphs).strip()


def enforce_length_budget(

    original: str,

    rewritten: str,

    *,

    preserve_length: bool = True,

) -> str:
    """Trim only when the rewrite clearly ballooned past the original.



    Preserves paragraph breaks (\\n\\n). Never trims so hard that most of the

    source content disappears.

    """
    source = original or ""
    output = rewritten or ""
    source_words = len(source.split())
    output_words = len(output.split())
    if source_words == 0:
        return output

    # Slightly looser ceiling so normal phrase/lexical growth is kept.
    max_ratio = 1.20 if preserve_length else 1.6
    max_words = max(1, int(source_words * max_ratio))
    # Floor: do not destroy content just to hit the ceiling.
    min_words = max(1, int(source_words * 0.90))
    if output_words <= max_words:
        return output

    paragraphs = [
        part.strip() for part in re.split(r"\n\s*\n", output.strip()) if part.strip()
    ]
    if not paragraphs:
        return output

    kept_paragraphs: list[str] = []
    count = 0
    for paragraph in paragraphs:
        parts = re.split(r"(?<=[.!?])\s+", paragraph.strip())
        kept_sentences: list[str] = []
        for part in parts:
            width = len(part.split())
            if kept_sentences and count + width > max_words:
                break
            if not kept_sentences and kept_paragraphs and count + width > max_words:
                break
            kept_sentences.append(part)
            count += width
        if kept_sentences:
            kept_paragraphs.append(" ".join(kept_sentences))
        if count >= max_words:
            break
    trimmed = "\n\n".join(kept_paragraphs).strip()
    if not trimmed or len(trimmed.split()) < min_words:
        # Trimming would drop too much meaning — keep the full rewrite.
        return output
    return trimmed