File size: 39,416 Bytes
8f6d79d 312c142 67f284e 74087c2 8f6d79d 0435b8d b543594 aeab924 c1447cb c29fb5e 31bce5e 8f6d79d c29fb5e ffb5352 8f6d79d 67f284e 8f6d79d 5132ade c29fb5e aeab924 8f6d79d f0b5a65 b543594 8f6d79d ffb5352 0435b8d 8f6d79d 67f284e 74087c2 8f6d79d 312c142 8f6d79d 74087c2 d06306b 74087c2 d06306b 74087c2 67f284e 74087c2 312c142 74087c2 d06306b 312c142 74087c2 d06306b 74087c2 d06306b 74087c2 bf1d8e6 74087c2 5132ade 312c142 32508e6 5132ade 312c142 5132ade aeab924 312c142 aeab924 32508e6 312c142 aeab924 0435b8d f0b5a65 0435b8d f0b5a65 0435b8d f0b5a65 0435b8d f0b5a65 0435b8d f0b5a65 0435b8d f0b5a65 0435b8d f0b5a65 b543594 ffb5352 f0b5a65 b543594 4e06845 f0b5a65 b543594 f0b5a65 4e06845 b543594 4e06845 b543594 f0b5a65 b543594 f0b5a65 24a79a8 ffb5352 b543594 f0b5a65 0435b8d 67f284e 0435b8d 67f284e 0435b8d 67f284e 0435b8d 31bce5e 67f284e d06306b 67f284e ffb5352 67f284e b543594 0435b8d ffb5352 0435b8d bf1d8e6 aeab924 b543594 5132ade 67f284e 5132ade 67f284e 31bce5e d06306b 31bce5e d06306b 31bce5e d06306b 312c142 31bce5e 67f284e 8f6d79d 74087c2 67f284e 0435b8d 31bce5e 74087c2 d06306b 8f6d79d ffb5352 8f6d79d 74087c2 8f6d79d 67f284e 74087c2 67f284e 0435b8d 31bce5e 67f284e d06306b 74087c2 ffb5352 74087c2 8f6d79d ffb5352 8f6d79d ffb5352 8f6d79d ffb5352 8f6d79d ffb5352 8f6d79d 7ea9869 8f6d79d 7b67b50 ffb5352 7b67b50 ffb5352 7b67b50 ffb5352 8f6d79d 312c142 8f6d79d 7ea9869 7b67b50 ffb5352 7b67b50 ffb5352 7b67b50 7ea9869 ffb5352 8f6d79d ffb5352 8f6d79d 74087c2 67f284e 74087c2 67f284e 0435b8d 31bce5e 67f284e d06306b 74087c2 ffb5352 8f6d79d 74087c2 8f6d79d 67f284e 74087c2 67f284e 0435b8d 31bce5e 67f284e d06306b 74087c2 ffb5352 74087c2 8f6d79d 74087c2 67f284e 0435b8d 31bce5e 74087c2 d06306b 8f6d79d ffb5352 8f6d79d 74087c2 67f284e 0435b8d 31bce5e 74087c2 d06306b 8f6d79d ffb5352 8f6d79d 74087c2 67f284e 0435b8d 31bce5e 74087c2 d06306b c29fb5e ffb5352 8f6d79d b543594 8f6d79d b543594 d06306b ffb5352 8f6d79d 74087c2 67f284e 0435b8d 31bce5e 74087c2 d06306b c29fb5e ffb5352 31bce5e 0435b8d 67f284e 8f6d79d 74087c2 67f284e 0435b8d 31bce5e 74087c2 d06306b 8f6d79d ffb5352 8f6d79d c29fb5e 39cfcd1 74087c2 67f284e bf1d8e6 67f284e 0435b8d f0b5a65 0435b8d 8f6d79d 74087c2 0435b8d 8f6d79d c29fb5e 8f6d79d 0435b8d 8f6d79d 0435b8d 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 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 | """Batch/stream orchestrator for the structural rewrite engine."""
from __future__ import annotations
import logging
import time
from difflib import SequenceMatcher
from app.config import (
ENGINE_BATCH_PARAS,
ENGINE_CLASSICAL_AGGRESSIVE,
ENGINE_FORCE_REWRITE,
ENGINE_LEXICAL_MIN_WSD,
ENGINE_LEXICAL_REFINEMENT,
ENGINE_MIN_CONFIDENCE,
ENGINE_PARAPHRASE,
ENGINE_PARAPHRASE_MIN_SIM,
ENGINE_PARAPHRASE_PRIMARY,
ENGINE_PHRASE_MAX_CHANGES,
ENGINE_PHRASE_MIN_SIM,
ENGINE_PHRASE_REWRITE,
ENGINE_PHRASE_USE_T5,
ENGINE_PRESERVE_LENGTH,
ENGINE_REQUIRE_WORDING_CHANGE,
ENGINE_SAFETY_MIN,
ENGINE_SPLIT_LONG,
ENGINE_SPLIT_MIN_WORDS,
ENGINE_STRUCTURAL_VARIATION,
ENGINE_USE_MINILM_SAFETY,
GRAMMAR_FIX_OUTPUT,
)
from app.engine.classify import classify_sentence
from app.engine.consistency import apply_consistency
from app.engine.force import force_cleft_rewrite
from app.engine.grammar import repair_sentence
from app.engine.ingest import ingest_text
from app.engine.lexical import (
dynamic_lexical_budget,
ensure_wording_change,
refine_sentence,
)
from app.engine.mechanics import enforce_length_budget, tidy
from app.engine.phrase import rewrite_phrases
from app.engine.models import (
DocumentBlock,
EngineResult,
EngineStats,
SentenceRecord,
)
from app.engine.normalize import (
detect_blocks,
mask_protected_spans,
normalize_text,
unmask_protected_spans,
)
from app.engine.fallback import structural_fallback_candidates
from app.engine.paraphrase import (
paraphrase_sentence,
sufficiently_changed,
surface_similarity,
)
from app.engine.plan import build_plan
from app.engine.rewrite import generate_from_plan
from app.engine.safety import check_safety
from app.engine.segment import iter_paragraph_batches, split_sentences, word_count
from app.engine.stitch import join_sentences, stitch_blocks
from app.engine.variation import new_seed, rotate
from app.engine.voice import active_to_passive
logger = logging.getLogger("plainrewrite.engine")
_REWRITEABLE_TYPES = frozenset(
{"simple_declarative", "compound", "complex", "because_clause"}
)
def _classical_modes(use_minilm: bool) -> tuple[bool, bool]:
"""Return (classical_strict, classical_aggressive) for wording paths."""
if use_minilm:
return False, False
return True, bool(ENGINE_CLASSICAL_AGGRESSIVE)
def _similarity(a: str, b: str) -> float:
return SequenceMatcher(None, (a or "").lower(), (b or "").lower()).ratio()
def _apply_lexical_refinement(
record: SentenceRecord,
*,
enabled: bool,
min_wsd: float,
max_changes: int | None,
safety_min: float,
min_confidence: float,
use_minilm: bool,
polish: bool = False,
) -> SentenceRecord:
if not enabled or record.sentence_type not in _REWRITEABLE_TYPES:
return record
before = record.rewritten
classical_strict, classical_aggressive = _classical_modes(use_minilm)
refined = refine_sentence(
before,
min_wsd=min_wsd,
max_changes=max_changes,
polish=polish,
classical_strict=classical_strict,
classical_aggressive=classical_aggressive,
)
if not refined.changes or refined.text == before:
return record
safety = check_safety(
before,
refined.text,
min_meaning=safety_min,
min_confidence=min_confidence,
use_minilm=use_minilm,
protected_entities=None,
protected_auxiliaries=None,
structural_validation=False,
)
if not safety.ok:
return record
prior_status = record.status
prior_reasons = list(record.reasons)
prior_lex = list(record.lexical_changes)
record.rewritten = refined.text
record.lexical_changes = prior_lex + refined.changes
record.status = "rewritten"
if not record.template_id:
record.template_id = "lexical_refine"
elif "+lexical" not in record.template_id:
record.template_id = f"{record.template_id}+lexical"
if prior_status in {"skipped", "reverted"}:
record.reasons = [
f"structural_{prior_status}:{reason}" for reason in prior_reasons
]
record.confidence = min(
safety.confidence,
record.confidence if record.confidence > 0 else max(min_confidence, 0.55),
)
return record
def _apply_extra_polish(
record: SentenceRecord,
*,
enabled: bool,
min_wsd: float,
max_changes: int | None,
safety_min: float,
min_confidence: float,
use_minilm: bool,
) -> SentenceRecord:
"""Polish-only extra pass so the UI toggle has a real end-to-end effect."""
if not enabled or record.sentence_type not in _REWRITEABLE_TYPES:
return record
classical_strict, classical_aggressive = _classical_modes(use_minilm)
# Tight classical (no MiniLM, not aggressive): skip extra polish cycling.
if classical_strict and not classical_aggressive:
return record
before = record.rewritten
target_budget = (
max(1, min(int(max_changes), 15))
if max_changes is not None
else dynamic_lexical_budget(before, polish=True)
)
remaining = max(0, target_budget - len(record.lexical_changes))
if remaining <= 0:
return record
refined = refine_sentence(
before,
min_wsd=min(min_wsd, 0.10),
max_changes=remaining,
aggressive=True,
classical_strict=classical_strict,
classical_aggressive=classical_aggressive,
)
if not refined.changes or refined.text == before:
return record
safety = check_safety(
before,
refined.text,
min_meaning=safety_min,
min_confidence=min_confidence,
use_minilm=use_minilm,
protected_entities=None,
protected_auxiliaries=None,
structural_validation=False,
)
if not safety.ok:
return record
prior_lex = list(record.lexical_changes)
record.rewritten = refined.text
record.lexical_changes = prior_lex + refined.changes
record.status = "rewritten"
if not record.template_id:
record.template_id = "lexical_refine"
elif "+lexical" not in record.template_id:
record.template_id = f"{record.template_id}+lexical"
record.confidence = min(
safety.confidence,
record.confidence if record.confidence > 0 else max(min_confidence, 0.55),
)
return record
def _apply_phrase_rewrite(
record: SentenceRecord,
*,
enabled: bool,
polish: bool,
safety_min: float,
min_confidence: float,
use_minilm: bool,
) -> SentenceRecord:
"""Rewrite verb–object phrases before single-word lexical polish."""
if not enabled or record.sentence_type not in _REWRITEABLE_TYPES:
return record
before = record.rewritten
max_changes = (
ENGINE_PHRASE_MAX_CHANGES
if ENGINE_PHRASE_MAX_CHANGES > 0
else (2 if polish else 1)
)
classical_strict, classical_aggressive = _classical_modes(use_minilm)
refined = rewrite_phrases(
before,
max_changes=max_changes,
polish=polish,
min_sim=min(ENGINE_PHRASE_MIN_SIM, safety_min),
use_t5=ENGINE_PHRASE_USE_T5 and use_minilm,
classical_strict=classical_strict,
classical_aggressive=classical_aggressive,
)
if not refined.changes or refined.text == before:
return record
safety = check_safety(
before,
refined.text,
min_meaning=min(safety_min, ENGINE_PHRASE_MIN_SIM),
min_confidence=min_confidence,
use_minilm=use_minilm,
protected_entities=None,
protected_auxiliaries=None,
structural_validation=False,
)
if not safety.ok:
return record
prior_lex = list(record.lexical_changes)
record.rewritten = refined.text
record.lexical_changes = prior_lex + refined.changes
record.status = "rewritten"
if not record.template_id:
record.template_id = "phrase_rewrite"
elif "+phrase" not in record.template_id:
record.template_id = f"{record.template_id}+phrase"
record.confidence = min(
safety.confidence,
record.confidence if record.confidence > 0 else max(min_confidence, 0.55),
)
return record
def _unchanged(record: SentenceRecord) -> bool:
return (
record.rewritten.strip().lower().rstrip(".!?")
== record.original.strip().lower().rstrip(".!?")
)
def _try_accept_rewrite(
record: SentenceRecord,
candidate: str,
*,
template_id: str,
confidence: float,
safety_min: float,
min_confidence: float,
use_minilm: bool,
reason_prefix: str,
) -> SentenceRecord | None:
"""Accept a candidate when safety passes and wording truly changed."""
if not sufficiently_changed(record.original, candidate):
return None
safety = check_safety(
record.original,
candidate,
min_meaning=min(safety_min, ENGINE_PARAPHRASE_MIN_SIM),
min_confidence=min_confidence,
use_minilm=use_minilm,
protected_entities=None,
protected_auxiliaries=None,
structural_validation=False,
)
if not safety.ok:
hard = check_safety(
record.original,
candidate,
hard_invariants_only=True,
)
if not hard.ok:
return None
safety = hard
repaired = repair_sentence(candidate) if GRAMMAR_FIX_OUTPUT else candidate
if repaired != candidate:
repaired_safety = check_safety(
record.original,
repaired,
hard_invariants_only=True,
)
if repaired_safety.ok and sufficiently_changed(
record.original, repaired
):
candidate = repaired
safety = repaired_safety
prior_status = record.status
prior_reasons = list(record.reasons)
record.rewritten = candidate
record.status = "rewritten"
record.template_id = template_id
record.confidence = min(0.75, max(confidence, safety.confidence))
record.reasons = [
f"{reason_prefix}_from_{prior_status}:{reason}"
for reason in prior_reasons
] or [reason_prefix]
return record
def _apply_paraphrase_fallback(
record: SentenceRecord,
*,
enabled: bool,
safety_min: float,
min_confidence: float,
use_minilm: bool,
primary: bool | None = None,
variation_seed: int | None = None,
) -> SentenceRecord:
"""Meaning-safe paraphrase rewrite, then structural fallbacks if needed.
When primary mode is on, paraphrase can replace an existing light structural
rewrite if the paraphrase keeps meaning and looks substantially newer.
"""
if record.sentence_type not in _REWRITEABLE_TYPES:
return record
prefer_primary = ENGINE_PARAPHRASE_PRIMARY if primary is None else primary
options: list[tuple[str, str, float, str]] = []
if enabled:
result = paraphrase_sentence(
record.original,
prefer_divergent=prefer_primary,
)
if (
result.text
and result.text != record.original
and sufficiently_changed(record.original, result.text)
):
para_sim = surface_similarity(record.original, result.text)
current_sim = surface_similarity(record.original, record.rewritten)
# Always keep paraphrase when the sentence is still unchanged.
# In primary mode, also prefer it when it is clearly more divergent
# than the current structural rewrite.
take_paraphrase = _unchanged(record) or (
prefer_primary and para_sim + 0.03 < current_sim
)
if take_paraphrase:
options.append(
(
"paraphrase",
result.text,
max(0.55, result.confidence),
"paraphrase_primary"
if prefer_primary
else "paraphrase_fallback",
)
)
# Structural fallbacks only fill gaps. If the plan path already rewrote the
# sentence, overwriting it here would discard per-request rotation and pin
# every sentence to the single highest-ranked fallback.
if _unchanged(record):
structural = rotate(
structural_fallback_candidates(record.original),
seed=variation_seed,
position=record.index,
confidence_of=lambda item: item[2],
)
for template_id, candidate, confidence in structural:
options.append(
(template_id, candidate, confidence, "structure_fallback")
)
for template_id, candidate, confidence, reason_prefix in options:
accepted = _try_accept_rewrite(
record,
candidate,
template_id=template_id,
confidence=confidence,
safety_min=safety_min,
min_confidence=min_confidence,
use_minilm=use_minilm,
reason_prefix=reason_prefix,
)
if accepted is not None:
return accepted
return record
def _apply_forced_rewrite(record: SentenceRecord, *, enabled: bool) -> SentenceRecord:
"""Optional legacy cleft rewrite; disabled by default."""
if not enabled or record.sentence_type not in _REWRITEABLE_TYPES:
return record
if not _unchanged(record):
return record
candidate = force_cleft_rewrite(record.original)
if not candidate:
return record
safety = check_safety(
record.original,
candidate,
hard_invariants_only=True,
)
if not safety.ok:
return record
repaired = repair_sentence(candidate) if GRAMMAR_FIX_OUTPUT else candidate
if repaired != candidate:
repaired_safety = check_safety(
record.original,
repaired,
hard_invariants_only=True,
)
if not repaired_safety.ok:
repaired = candidate
else:
safety = repaired_safety
prior_status = record.status
prior_reasons = list(record.reasons)
record.rewritten = repaired
record.status = "rewritten"
record.template_id = "forced_cleft"
record.confidence = min(0.60, safety.confidence)
record.reasons = [
f"forced_from_{prior_status}:{reason}" for reason in prior_reasons
] or ["forced_rewrite"]
return record
def _finalize_sentence(
record: SentenceRecord,
*,
lexical_enabled: bool,
force_enabled: bool,
paraphrase_enabled: bool,
require_wording: bool,
lexical_min_wsd: float,
lexical_max_changes: int | None,
lexical_polish: bool,
safety_min: float,
min_confidence: float,
use_minilm: bool,
variation_seed: int | None = None,
) -> SentenceRecord:
# Paraphrase first as the main rewrite when enabled: pick a meaning-safe
# but surface-divergent wording. Structural fallbacks cover gaps. Synonym
# refinement then polishes the chosen rewrite.
record = _apply_paraphrase_fallback(
record,
enabled=paraphrase_enabled,
safety_min=safety_min,
min_confidence=min_confidence,
use_minilm=use_minilm,
variation_seed=variation_seed,
)
record = _apply_forced_rewrite(record, enabled=force_enabled)
record = _apply_phrase_rewrite(
record,
enabled=ENGINE_PHRASE_REWRITE,
polish=lexical_polish,
safety_min=safety_min,
min_confidence=min_confidence,
use_minilm=use_minilm,
)
record = _apply_lexical_refinement(
record,
enabled=lexical_enabled,
min_wsd=lexical_min_wsd,
max_changes=lexical_max_changes,
safety_min=safety_min,
min_confidence=min_confidence,
use_minilm=use_minilm,
polish=lexical_polish,
)
record = _apply_extra_polish(
record,
enabled=lexical_enabled and lexical_polish,
min_wsd=lexical_min_wsd,
max_changes=lexical_max_changes,
safety_min=safety_min,
min_confidence=min_confidence,
use_minilm=use_minilm,
)
return _ensure_wording_changed(
record,
enabled=require_wording,
lexical_min_wsd=lexical_min_wsd,
lexical_max_changes=lexical_max_changes,
lexical_polish=lexical_polish,
safety_min=safety_min,
min_confidence=min_confidence,
use_minilm=use_minilm,
)
def _ensure_wording_changed(
record: SentenceRecord,
*,
enabled: bool,
lexical_min_wsd: float,
lexical_max_changes: int | None,
lexical_polish: bool,
safety_min: float,
min_confidence: float,
use_minilm: bool,
) -> SentenceRecord:
"""Guarantee a meaning-safe wording change when the sentence is still identical."""
if not enabled or record.sentence_type not in _REWRITEABLE_TYPES:
return record
if not _unchanged(record):
return record
# Voice shift is a structural last resort before synonym forcing.
passive = active_to_passive(record.original)
if passive:
accepted = _try_accept_rewrite(
record,
passive,
template_id="ensure_passive",
confidence=0.62,
safety_min=safety_min,
min_confidence=min_confidence,
use_minilm=use_minilm,
reason_prefix="ensure_passive",
)
if accepted is not None:
return accepted
ensured = ensure_wording_change(
record.original,
min_wsd=min(lexical_min_wsd, 0.10),
max_changes=lexical_max_changes,
polish=lexical_polish,
classical_strict=(not use_minilm),
classical_aggressive=(not use_minilm) and ENGINE_CLASSICAL_AGGRESSIVE,
)
if not ensured.changes or ensured.text == record.original:
return record
if not sufficiently_changed(record.original, ensured.text):
return record
safety = check_safety(
record.original,
ensured.text,
min_meaning=min(safety_min, ENGINE_PARAPHRASE_MIN_SIM),
min_confidence=min_confidence,
use_minilm=use_minilm,
protected_entities=None,
protected_auxiliaries=None,
structural_validation=False,
)
if not safety.ok:
hard = check_safety(
record.original,
ensured.text,
hard_invariants_only=True,
)
if not hard.ok:
return record
safety = hard
prior_status = record.status
prior_reasons = list(record.reasons)
record.rewritten = ensured.text
record.lexical_changes = ensured.changes
record.status = "rewritten"
record.template_id = "ensure_wording"
record.confidence = min(0.70, max(0.55, safety.confidence, ensured.confidence))
record.reasons = [
f"ensure_from_{prior_status}:{reason}" for reason in prior_reasons
] or ["ensure_wording_change"]
return record
def _process_sentence(
text: str,
*,
index: int,
block_index: int,
min_confidence: float,
safety_min: float,
use_minilm: bool,
lexical_enabled: bool,
force_enabled: bool,
paraphrase_enabled: bool,
require_wording: bool,
lexical_min_wsd: float,
lexical_max_changes: int | None,
lexical_polish: bool,
last_template: str,
stats: EngineStats,
variation_seed: int | None = None,
) -> tuple[SentenceRecord, str]:
"""Rewrite one sentence with plan → generate → grammar → safety → fallback."""
original = (text or "").strip()
kind = classify_sentence(original)
if not original:
rec = SentenceRecord(
index=index,
original=original,
rewritten=original,
confidence=1.0,
status="passthrough",
sentence_type=kind,
block_index=block_index,
)
stats.passthrough += 1
return rec, ""
plan = build_plan(original, min_confidence=min_confidence)
if not plan.safe:
reason = plan.skip_reason or kind
stats.skipped += 1
stats.bump(reason)
rec = SentenceRecord(
index=index,
original=original,
rewritten=original,
confidence=plan.confidence,
status="skipped",
sentence_type=kind,
reasons=[reason],
block_index=block_index,
)
rec = _finalize_sentence(
rec,
lexical_enabled=lexical_enabled,
force_enabled=force_enabled,
paraphrase_enabled=paraphrase_enabled,
require_wording=require_wording,
lexical_min_wsd=lexical_min_wsd,
lexical_max_changes=lexical_max_changes,
lexical_polish=lexical_polish,
safety_min=safety_min,
min_confidence=min_confidence,
use_minilm=use_minilm,
variation_seed=variation_seed,
)
return rec, rec.template_id if rec.status == "rewritten" else ""
# Prefer alternate template if last sentence used the same time-front family
time_fronts = {
"time_subj_manner_verb_place",
"time_subj_verb_place",
"time_subj_verb_object",
"time_subj_verb_object_place",
"time_front",
}
# Slot templates alone rarely offer more than one survivor, so fold in the
# clause/voice/split transforms. Rotation then has real alternatives to pick
# between instead of always landing on the same fronting.
pool: list[tuple[str, str | None, float]] = [
(cand.template_id, None, cand.confidence) for cand in plan.candidates
]
if variation_seed is not None:
planned = {template_id for template_id, _text, _conf in pool}
pool += [
(template_id, text, confidence)
for template_id, text, confidence in structural_fallback_candidates(
original, include_plan=False
)
# Passivizing reads worse than any reorder, so it stays a last
# resort in the fallback path rather than a rotation option.
if template_id not in planned and template_id != "active_to_passive"
]
candidates = rotate(
pool,
seed=variation_seed,
position=index,
confidence_of=lambda item: item[2],
)
if last_template in time_fronts and candidates:
reordered = [c for c in candidates if c[0] != last_template]
reordered += [c for c in candidates if c[0] == last_template]
candidates = reordered
best: SentenceRecord | None = None
for template_id, prepared, cand_confidence in candidates:
generated = (
prepared
if prepared is not None
else generate_from_plan(plan, template_id=template_id)
)
if not generated:
continue
# Voice flips legitimately move the agent and rebuild the auxiliary
# chain, so slot-level protections do not apply to them.
voice_flip = template_id in {"active_to_passive", "passive_to_active"}
safety = check_safety(
original,
generated,
min_meaning=safety_min,
min_confidence=min_confidence,
use_minilm=use_minilm,
protected_entities=(
None if voice_flip else plan.slots.entities if plan.slots else ()
),
protected_auxiliaries=(
None if voice_flip else plan.slots.auxiliaries if plan.slots else ()
),
structural_validation=not voice_flip,
)
if not safety.ok and (not use_minilm) and ENGINE_CLASSICAL_AGGRESSIVE:
# Aggressive classical: accept structure when hard facts still hold.
hard = check_safety(
original,
generated,
hard_invariants_only=True,
)
if hard.ok:
safety = hard
else:
stats.bump(safety.reasons[0] if safety.reasons else "safety")
continue
elif not safety.ok:
stats.bump(safety.reasons[0] if safety.reasons else "safety")
continue
repaired = repair_sentence(generated) if GRAMMAR_FIX_OUTPUT else generated
if repaired != generated:
repaired_safety = check_safety(
original,
repaired,
min_meaning=safety_min,
min_confidence=min_confidence,
use_minilm=use_minilm,
protected_entities=(
None if voice_flip else plan.slots.entities if plan.slots else ()
),
protected_auxiliaries=(
None
if voice_flip
else plan.slots.auxiliaries if plan.slots else ()
),
structural_validation=False,
)
if not repaired_safety.ok:
stats.bump(
repaired_safety.reasons[0]
if repaired_safety.reasons
else "grammar_safety"
)
continue
safety = repaired_safety
conf = min(cand_confidence, safety.confidence + 0.2)
best = SentenceRecord(
index=index,
original=original,
rewritten=repaired,
confidence=conf,
status="rewritten",
template_id=template_id,
sentence_type=kind,
reasons=[],
block_index=block_index,
)
break
if best is None:
stats.reverted += 1
stats.bump("safety_fallback")
rec = SentenceRecord(
index=index,
original=original,
rewritten=original,
confidence=0.0,
status="reverted",
sentence_type=kind,
reasons=["safety_fallback"],
block_index=block_index,
)
rec = _finalize_sentence(
rec,
lexical_enabled=lexical_enabled,
force_enabled=force_enabled,
paraphrase_enabled=paraphrase_enabled,
require_wording=require_wording,
lexical_min_wsd=lexical_min_wsd,
lexical_max_changes=lexical_max_changes,
lexical_polish=lexical_polish,
safety_min=safety_min,
min_confidence=min_confidence,
use_minilm=use_minilm,
variation_seed=variation_seed,
)
return rec, rec.template_id if rec.status == "rewritten" else ""
stats.rewritten += 1
best = _finalize_sentence(
best,
lexical_enabled=lexical_enabled,
force_enabled=force_enabled,
paraphrase_enabled=paraphrase_enabled,
require_wording=require_wording,
lexical_min_wsd=lexical_min_wsd,
lexical_max_changes=lexical_max_changes,
lexical_polish=lexical_polish,
safety_min=safety_min,
min_confidence=min_confidence,
use_minilm=use_minilm,
variation_seed=variation_seed,
)
return best, best.template_id
def _process_block(
block: DocumentBlock,
*,
sent_offset: int,
min_confidence: float,
safety_min: float,
use_minilm: bool,
lexical_enabled: bool,
force_enabled: bool,
paraphrase_enabled: bool,
require_wording: bool,
lexical_min_wsd: float,
lexical_max_changes: int | None,
lexical_polish: bool,
last_template: str,
stats: EngineStats,
variation_seed: int | None = None,
) -> tuple[DocumentBlock, list[SentenceRecord], str]:
if not block.rewriteable or block.kind != "paragraph":
stats.passthrough += 1
rec = SentenceRecord(
index=sent_offset,
original=block.text,
rewritten=block.text,
confidence=1.0,
status="passthrough",
sentence_type=block.kind,
block_index=block.index,
reasons=[f"block:{block.kind}"],
)
return block, [rec], last_template
masked, prot = mask_protected_spans(block.text)
sentences = split_sentences(masked)
if not sentences:
return block, [], last_template
records: list[SentenceRecord] = []
out_sents: list[str] = []
tid = last_template
for i, sent in enumerate(sentences):
rec, tid = _process_sentence(
sent,
index=sent_offset + i,
block_index=block.index,
min_confidence=min_confidence,
safety_min=safety_min,
use_minilm=use_minilm,
lexical_enabled=lexical_enabled,
force_enabled=force_enabled,
paraphrase_enabled=paraphrase_enabled,
require_wording=require_wording,
lexical_min_wsd=lexical_min_wsd,
lexical_max_changes=lexical_max_changes,
lexical_polish=lexical_polish,
last_template=tid,
stats=stats,
variation_seed=variation_seed,
)
# Unmask protected spans in both sides of the record
rec.original = unmask_protected_spans(rec.original, prot)
rec.rewritten = unmask_protected_spans(rec.rewritten, prot)
records.append(rec)
out_sents.append(rec.rewritten)
new_text = unmask_protected_spans(join_sentences(out_sents), prot)
new_block = DocumentBlock(
text=new_text,
kind=block.kind,
rewriteable=block.rewriteable,
index=block.index,
meta=dict(block.meta),
)
return new_block, records, tid
def rewrite_document(
source: str | bytes | None,
*,
batch_paras: int | None = None,
min_confidence: float | None = None,
safety_min: float | None = None,
use_minilm_safety: bool | None = None,
use_lexical_refinement: bool | None = None,
force_rewrite: bool | None = None,
use_paraphrase: bool | None = None,
require_wording_change: bool | None = None,
lexical_min_wsd: float | None = None,
lexical_max_changes: int | None = None,
lexical_polish: bool | None = None,
preserve_length: bool | None = None,
variation_seed: int | None = None,
) -> EngineResult:
"""
Structural rewrite with local paraphrase and low-impact synonyms.
Safe spaCy templates still run first. When paraphrase primary mode is on,
a CPU T5 model then prefers a meaning-safe but surface-divergent rewrite so
the text looks substantially new. MiniLM guards meaning. Synonym refinement
runs after paraphrase. When require_wording_change is on, a final ensure
pass forces a meaning-safe wording change whenever a safe option exists.
Synonym budget is dynamic from sentence length unless lexical_max_changes
is set explicitly. lexical_polish densifies that budget (UI "word polish").
``variation_seed`` rotates between equally-ranked structural options so the
same text rewrites differently on each request. Pass an explicit seed for
reproducible output; pass 0 to disable rotation entirely.
"""
started = time.perf_counter()
batch_paras = batch_paras if batch_paras is not None else ENGINE_BATCH_PARAS
min_confidence = (
min_confidence if min_confidence is not None else ENGINE_MIN_CONFIDENCE
)
safety_min = safety_min if safety_min is not None else ENGINE_SAFETY_MIN
use_minilm = (
use_minilm_safety
if use_minilm_safety is not None
else ENGINE_USE_MINILM_SAFETY
)
lexical_enabled = (
use_lexical_refinement
if use_lexical_refinement is not None
else ENGINE_LEXICAL_REFINEMENT
)
force_enabled = (
force_rewrite if force_rewrite is not None else ENGINE_FORCE_REWRITE
)
paraphrase_enabled = (
use_paraphrase if use_paraphrase is not None else ENGINE_PARAPHRASE
)
require_wording = (
require_wording_change
if require_wording_change is not None
else ENGINE_REQUIRE_WORDING_CHANGE
)
lexical_min_wsd = (
lexical_min_wsd
if lexical_min_wsd is not None
else ENGINE_LEXICAL_MIN_WSD
)
# None => fully dynamic per-sentence budget inside refine_sentence.
if lexical_max_changes is not None and lexical_max_changes <= 0:
lexical_max_changes = None
polish_enabled = bool(lexical_polish)
keep_length = (
ENGINE_PRESERVE_LENGTH if preserve_length is None else bool(preserve_length)
)
if variation_seed is None:
variation_seed = new_seed() if ENGINE_STRUCTURAL_VARIATION else None
elif variation_seed == 0:
variation_seed = None
if (force_enabled or paraphrase_enabled or require_wording) and min_confidence > 0.40:
# Prefer more structural templates before generative fallback.
min_confidence = 0.40
raw = ingest_text(source)
if not raw.strip():
raise ValueError("Paste some text first.")
normalized = normalize_text(raw)
blocks = detect_blocks(normalized)
stats = EngineStats(blocks=len(blocks))
all_records: list[SentenceRecord] = []
out_blocks: list[DocumentBlock] = []
sent_offset = 0
last_template = ""
for batch in iter_paragraph_batches(blocks, batch_paras=batch_paras):
stats.batches += 1
for block in batch:
new_block, records, last_template = _process_block(
block,
sent_offset=sent_offset,
min_confidence=min_confidence,
safety_min=safety_min,
use_minilm=use_minilm,
lexical_enabled=lexical_enabled,
force_enabled=force_enabled,
paraphrase_enabled=paraphrase_enabled,
require_wording=require_wording,
lexical_min_wsd=lexical_min_wsd,
lexical_max_changes=lexical_max_changes,
lexical_polish=polish_enabled,
last_template=last_template,
stats=stats,
variation_seed=variation_seed,
)
out_blocks.append(new_block)
all_records.extend(records)
# Advance offset by sentence count for rewriteable paras
if block.rewriteable and block.kind == "paragraph":
sent_offset += max(1, len(records))
else:
sent_offset += 1
stats.sentences = len(all_records)
stitched = stitch_blocks(out_blocks)
# Consistency: suppress streaks then rebuild from records if needed
stitched, all_records = apply_consistency(stitched, all_records)
streak_reverts = [r for r in all_records if "template_streak" in r.reasons]
if streak_reverts:
# Rebuild rewriteable paragraph texts from records
by_block: dict[int, list[SentenceRecord]] = {}
for rec in all_records:
by_block.setdefault(rec.block_index, []).append(rec)
rebuilt: list[DocumentBlock] = []
for block in out_blocks:
recs = by_block.get(block.index, [])
if block.rewriteable and block.kind == "paragraph" and recs:
text = join_sentences([r.rewritten for r in recs])
rebuilt.append(
DocumentBlock(
text=text,
kind=block.kind,
rewriteable=True,
index=block.index,
)
)
else:
rebuilt.append(block)
stitched = stitch_blocks(rebuilt)
stitched, all_records = apply_consistency(stitched, all_records)
stitched = tidy(stitched)
if keep_length:
trimmed = enforce_length_budget(
normalized,
stitched,
preserve_length=True,
)
if trimmed != stitched:
stitched = tidy(trimmed)
notes_trim = "length_budget"
else:
notes_trim = ""
else:
notes_trim = ""
stats.rewritten = sum(r.status == "rewritten" for r in all_records)
stats.skipped = sum(r.status == "skipped" for r in all_records)
stats.reverted = sum(r.status == "reverted" for r in all_records)
stats.passthrough = sum(r.status == "passthrough" for r in all_records)
stats.lexical_refined = sum(bool(r.lexical_changes) for r in all_records)
stats.forced_rewrites = sum(
(r.template_id or "").startswith("forced_cleft") for r in all_records
)
stats.paraphrased = sum(
(r.template_id or "").startswith("paraphrase")
or any(
reason.startswith("structure_fallback") for reason in (r.reasons or [])
)
for r in all_records
)
stats.seconds = round(time.perf_counter() - started, 3)
mapping = [(r.original, r.rewritten) for r in all_records]
skipped = [r for r in all_records if r.status in {"skipped", "reverted"}]
in_words = word_count(normalized)
out_words = word_count(stitched)
changed = stitched.strip() != normalized.strip()
sim = _similarity(normalized, stitched)
notes = (
f"structural: rewritten={stats.rewritten} skipped={stats.skipped} "
f"reverted={stats.reverted} lexical={stats.lexical_refined} "
f"paraphrased={stats.paraphrased} forced={stats.forced_rewrites} "
f"batches={stats.batches}"
)
if notes_trim:
notes = f"{notes}; {notes_trim}"
logger.info(
"engine done words=%s→%s rewritten=%s skipped=%s reverted=%s "
"paraphrased=%s lexical=%s batches=%s t=%.2fs",
in_words,
out_words,
stats.rewritten,
stats.skipped,
stats.reverted,
stats.paraphrased,
stats.lexical_refined,
stats.batches,
stats.seconds,
)
return EngineResult(
text=stitched,
sentences=all_records,
skipped=skipped,
mapping=mapping,
stats=stats,
notes=notes,
engine="structural-reorder",
input_words=in_words,
output_words=out_words,
changed=changed,
similarity=round(sim, 4),
)
|