"""Tuned hybrid quality regressions (no generative model 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_validator import validate_candidate from app.pipeline.grammar_fix import correct_text from app.pipeline.orchestrator import rewrite_text from app.pipeline.synonym import _lexicon_for ESL = ( "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.\n\n" "Exercise is one of the most important activity that keeps our body fit. " "If a person exercise regularly, they will have less chance to gets diseases. " "Unfortunately, many peoples prefers watching television instead of doing physical activities" ) def test_invention_rejected() -> None: orig = ( "Nowadays many people are living an unhealthy life because they don't have enough time." ) invented = ( "Many people these days are living lives that are often unfulfilling and unhealthy, " "largely due to a lack of time." ) v = validate_candidate(orig, invented, min_meaning=0.5) assert not v.ok assert "invention" in v.reasons print("invention rejected OK") def test_grammar_infinitive_and_one_of() -> None: g = correct_text( "Exercise is one of the most important activity that keeps our body fit. " "They will have less chance to obtains diseases." ) gl = g.lower() assert "activities" in gl assert "to obtain" in gl or "to get" in gl assert "to obtains" not in gl print("grammar infinitive/one-of OK:", g) def test_neutral_lexicon_not_elevated() -> None: lex = _lexicon_for("Neutral", 1) assert lex.get("important", "important") == "important" assert lex.get("many", "many") == "many" assert lex.get("health", "health") == "health" print("neutral lexicon OK") def test_esl_rewrite_no_invention_no_elevate() -> None: r = rewrite_text(ESL, tone="Neutral", strength=1, preserve_length=True, ml_polish=False) out = r.text.lower() assert "unfulfill" not in out assert "numerous" not in out assert "vital activity" not in out # should be important activities or similar assert "regrettably" not in out assert "peoples" not in out assert "enough time" in out assert "unhealthy" in out assert "fast food" in out or "fast foods" in out assert "exercise" in out assert "television" in out or "physical" in out print("esl classical rewrite OK:") print(r.text) def test_esl_hybrid_path() -> None: r = rewrite_text(ESL, tone="Neutral", strength=1, preserve_length=True, ml_polish=True) out = r.text.lower() assert "unfulfill" not in out assert "peoples" not in out assert "unhealthy" in out assert "health" in out assert "exercise" in out # Should not leave broken infinitive if grammar ran assert "to obtains" not in out assert "to gets diseases" not in out assert "a person exercise " not in out print("esl hybrid rewrite OK mode=", r.pipeline_mode) print(r.text) if r.hybrid: print( "hybrid stats:", r.hybrid.units, r.hybrid.classical_kept, r.hybrid.regenerated, r.hybrid.gen_accepted, r.hybrid.reverted_source, ) if __name__ == "__main__": test_invention_rejected() test_grammar_infinitive_and_one_of() test_neutral_lexicon_not_elevated() test_esl_rewrite_no_invention_no_elevate() test_esl_hybrid_path() print("\nALL TUNE TESTS PASSED")