any2human / app /engine /consistency /__init__.py
idnameraj's picture
Upload 126 files
39cfcd1 verified
Raw
History Blame Contribute Delete
1.86 kB
"""Lightweight document consistency without reparsing the full document."""
from __future__ import annotations
from app.engine.models import SentenceRecord
_FRONTING_TEMPLATES = frozenset(
{
"time_subj_manner_verb_place",
"time_subj_verb_place",
"time_subj_verb_object",
"time_subj_verb_object_place",
"time_front",
"place_front",
"adv_subj_verb_object",
}
)
def suppress_template_streaks(records: list[SentenceRecord]) -> list[SentenceRecord]:
"""Revert consecutive identical fronting templates to reduce repetition."""
adjusted: list[SentenceRecord] = []
previous = ""
for record in records:
if (
record.status == "rewritten"
and record.template_id in _FRONTING_TEMPLATES
and record.template_id == previous
):
adjusted.append(
SentenceRecord(
index=record.index,
original=record.original,
rewritten=record.original,
confidence=record.confidence,
status="reverted",
template_id="",
sentence_type=record.sentence_type,
reasons=record.reasons + ["template_streak"],
block_index=record.block_index,
)
)
previous = ""
continue
adjusted.append(record)
previous = record.template_id if record.status == "rewritten" else ""
return adjusted
def apply_consistency(
text: str,
records: list[SentenceRecord],
) -> tuple[str, list[SentenceRecord]]:
# Entity spellings are already preserved token-for-token by generation and safety.
return text, suppress_template_streaks(records)