File size: 3,860 Bytes
4876842
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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")