any2human / scripts /test_quality_pipeline.py
idnameraj's picture
Upload 100 files
bb7b252 verified
Raw
History Blame Contribute Delete
6.6 kB
"""Regression tests for quality-first rewrite pipeline (no GPU required)."""
from __future__ import annotations
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
from app.pipeline.candidate_ranker import pick_best_candidate, score_candidate
from app.pipeline.candidate_validator import ValidationResult, validate_candidate
from app.pipeline.grammar_fix import correct_text
from app.pipeline.meaning_safety import polarity_safe
from app.pipeline.orchestrator import rewrite_text
def test_polarity() -> None:
assert polarity_safe(
"people live an unhealthy life",
"people live a poor lifestyle",
)
assert not polarity_safe(
"people live an unhealthy life",
"people live a healthy life",
)
assert not polarity_safe(
"people don't realize how much it affects health",
"causes people to realize how much it affects their health",
)
print("polarity OK")
def test_validator_rejects_truncated() -> None:
orig = (
"Nowadays many people are living an unhealthy life because they don't have enough time. "
"Eating fast foods is becoming very common and people don't realize how much it affects their health."
)
truncated = (
"Fast food is becoming very common because many people don't realize "
"how much it affects their health."
)
v = validate_candidate(orig, truncated, min_meaning=0.5)
assert not v.ok
assert "length" in v.reasons or "coverage" in v.reasons
print("validator rejects truncated OK:", v.reasons)
def test_validator_rejects_flips() -> None:
orig = (
"Nowadays many people are living an unhealthy life because they don't have enough time. "
"Eating fast foods is becoming very common and people don't realize how much it affects their health."
)
bad = (
"Nowadays many people live a healthy life because they don't have time to live. "
"Eating fast food causes people to realize how much it affects their health."
)
v = validate_candidate(orig, bad, min_meaning=0.5)
assert not v.ok
assert "polarity" in v.reasons
print("validator rejects flips OK")
def test_validator_rejects_near_copy() -> None:
orig = (
"Nowadays many people are living an unhealthy life because they don't have enough time. "
"Eating fast foods is becoming very common and people don't realize how much it affects their health."
)
near = (
"Nowadays many people are living an unhealthy life because they don't have enough time. "
"Eating fast foods is becoming very common and people don't realize how much it affects their health"
)
v = validate_candidate(orig, near, min_meaning=0.5)
assert not v.ok
assert "too_similar" in v.reasons or "identical" in v.reasons
print("validator rejects near-copy OK")
def test_ranker_rejects_near_copy_prefers_paraphrase() -> None:
orig = (
"Nowadays many people are living an unhealthy life because they don't have enough time. "
"Eating fast food is becoming very common and people don't realize how much it affects their health."
)
near = orig # identical → filtered
good = (
"These days a lot of people lead unhealthy lifestyles simply because they lack time. "
"Fast food is increasingly common, and many fail to see how badly it harms their health."
)
bad = (
"These days a lot of people lead healthy lifestyles because they have plenty of time. "
"Fast food helps people realize how much it improves their health."
)
picked = pick_best_candidate(
orig,
[near, bad, good],
tone="Neutral",
min_meaning=0.5,
max_surface=0.92,
fallback=orig,
)
assert picked == good or (
"lack time" in picked.lower() or "fail to see" in picked.lower()
)
assert picked != orig
assert "healthy lifestyles because they have plenty" not in picked.lower()
print("ranker prefers paraphrase over near-copy OK:", picked)
def test_ranker_prefers_faithful() -> None:
orig = "Students should complete the assignment before the deadline."
good = "Students ought to finish the assignment ahead of the deadline."
bad = "Students should ignore the assignment after the deadline."
near = "Students should complete the assignment before the deadline."
picked = pick_best_candidate(
orig,
[bad, near, good],
tone="Neutral",
min_meaning=0.5,
max_surface=0.92,
fallback=orig,
)
assert "ignore" not in picked.lower()
# Near-copy must not beat a real paraphrase
assert picked == good or "ought" in picked.lower() or "finish" in picked.lower()
print("ranker OK:", picked)
def test_grammar() -> None:
g = correct_text(
"Nowadays many peoples are living unhealthy life because they don't have enough times. "
"Eating fast foods are becoming very common and peoples don't realizes how much it affect their health."
)
gl = g.lower()
assert "peoples" not in gl
assert "enough time" in gl
assert "an unhealthy life" in gl
assert "don't realize" in gl
assert "it affects" in gl
print("grammar OK")
def test_classical_fallback() -> None:
orig = (
"Nowadays many peoples are living unhealthy life because they don't have enough times. "
"Eating fast foods are becoming very common and peoples don't realizes how much it affect their health."
)
r = rewrite_text(
orig,
tone="Neutral",
strength=1,
preserve_length=True,
ml_polish=False,
)
out = r.text.lower()
assert "peoples" not in out
assert "adequate moments" not in out
assert "rapid food" not in out
assert "don't realize" in out or "do not realize" in out
assert "affects" in out
assert "an unhealthy life" in out
print("classical fallback OK:")
print(r.text)
print("engine:", r.engine)
if __name__ == "__main__":
test_polarity()
test_validator_rejects_flips()
test_validator_rejects_truncated()
test_validator_rejects_near_copy()
test_ranker_rejects_near_copy_prefers_paraphrase()
test_ranker_prefers_faithful()
test_grammar()
test_classical_fallback()
print("\nALL QUALITY PIPELINE TESTS PASSED")