File size: 6,600 Bytes
2f3072b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb7b252
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f3072b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c28715
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f3072b
 
 
 
7c28715
2f3072b
 
7c28715
2f3072b
 
7c28715
2f3072b
 
 
7c28715
 
2f3072b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb7b252
7c28715
 
2f3072b
 
 
 
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
"""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")