File size: 5,404 Bytes
32508e6 312c142 32508e6 | 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 | """Classical-strict gates (no MiniLM): block second-pass synonym drift."""
from __future__ import annotations
from app.engine.lexical import refine_sentence
from app.engine.phrase import rewrite_phrases
SECOND_PASS = (
"For the success of any business, supplying excellent customer service is "
"vital. Customers appreciate organizations that answer promptly to their "
"questions, decide issues efficiently, and treat them with respect.\n\n"
"Employees who communicate clearly and keep a positive attitude help create "
"a satisfying customer experience. Areas for improvement can be identified "
"by businesses that actively listen to customer feedback and consolidate "
"customer loyalty.\n\n"
"By consistently delivering high-quality service, organizations can "
"establish a strong reputation, gain customer retention, and spur positive "
"word-of-mouth recommendations."
)
def test_phrase_blocks_found_and_present_reputation_service():
source = (
"By consistently delivering high-quality service, organizations can "
"establish a strong reputation."
)
result = rewrite_phrases(
source,
polish=True,
use_t5=False,
max_changes=2,
classical_strict=True,
)
low = result.text.lower()
assert "found a strong reputation" not in low
assert "presenting high-quality service" not in low
assert "launch" not in low
assert "prove a strong reputation" not in low
def test_lexical_blocks_prove_reputation_and_win_retention():
source = (
"Organizations can establish a strong reputation and gain customer "
"retention."
)
result = refine_sentence(
source,
min_wsd=0.10,
polish=True,
classical_strict=True,
)
low = result.text.lower()
assert "prove a strong reputation" not in low
assert "win customer retention" not in low
def test_lexical_blocks_resolve_to_decide_and_respond_to_answer():
source = (
"Customers appreciate organizations that respond promptly to their "
"questions, resolve issues efficiently, and treat them with respect."
)
result = refine_sentence(
source,
min_wsd=0.10,
polish=True,
classical_strict=True,
)
low = result.text.lower()
assert "decide issues" not in low
assert "resolve issues" in low
# "answer promptly to" is ungrammatical; keep respond.
assert "answer promptly to" not in low
def test_lexical_still_allows_maintain_to_keep_headword():
source = "Employees who maintain a positive attitude help customers."
result = refine_sentence(
source,
min_wsd=0.10,
polish=True,
classical_strict=True,
)
# Headword upgrade may apply; must not invent bad VO pairs.
low = result.text.lower()
assert "decide" not in low
assert "attitude" in low
if result.changes:
assert any(
c.original.lower() == "maintain" and c.replacement.lower() == "keep"
for c in result.changes
)
def test_phrase_blocks_decide_determine_define_issues():
source = (
"Customers appreciate organizations that decide issues efficiently."
)
result = rewrite_phrases(
source,
polish=True,
use_t5=False,
max_changes=2,
classical_strict=True,
)
low = result.text.lower()
assert "determine issues" not in low
assert "define issues" not in low
def test_phrase_blocks_keep_to_maintain_cycle():
source = "Employees who keep a positive attitude help customers."
result = rewrite_phrases(
source,
polish=False,
use_t5=False,
max_changes=2,
classical_strict=True,
)
low = result.text.lower()
assert "maintain a positive attitude" not in low
def test_lexical_blocks_delivering_to_functioning():
source = "By consistently delivering high-quality service, firms succeed."
result = refine_sentence(
source,
min_wsd=0.10,
polish=True,
classical_strict=True,
)
low = result.text.lower()
assert "functioning" not in low
assert "serving high-quality service" not in low
def test_second_pass_sample_stays_stable_under_classical_strict():
phrase = rewrite_phrases(
SECOND_PASS,
polish=True,
use_t5=False,
max_changes=2,
classical_strict=True,
)
lexical = refine_sentence(
SECOND_PASS,
min_wsd=0.14,
polish=True,
classical_strict=True,
)
combined = (phrase.text + "\n" + lexical.text).lower()
for bad in (
"launch a strong reputation",
"found a strong reputation",
"prove a strong reputation",
"presenting high-quality",
"functioning high-quality",
"serving high-quality service",
"define issues",
"determine issues",
"maintain a positive attitude",
"win customer retention",
):
assert bad not in combined
def test_classical_strict_skips_aggressive_ensure_path():
from app.engine.lexical import ensure_wording_change
source = "The report assists students with useful information."
result = ensure_wording_change(source, classical_strict=True)
assert result.text == source
assert result.reason == "classical_strict_no_ensure"
|