WOTD _polish: enforce the "examples must use the word" contract
Per Codex audit: only accept a polish when there's a gloss AND at least one example sentence that actually contains the word (case-insensitive). Examples that don't use the word are dropped; if none remain, fall back to the raw dictionary def/examples instead of shipping a gloss with empty/irrelevant usage. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+4
-1
@@ -95,7 +95,10 @@ def _polish(client, word: str, part_of_speech: str | None, definition: str) -> d
|
|||||||
return None
|
return None
|
||||||
gloss = " ".join(str(data.get("gloss") or "").split()).strip()
|
gloss = " ".join(str(data.get("gloss") or "").split()).strip()
|
||||||
examples = [" ".join(str(e).split()).strip() for e in (data.get("examples") or []) if str(e).strip()]
|
examples = [" ".join(str(e).split()).strip() for e in (data.get("examples") or []) if str(e).strip()]
|
||||||
if not gloss:
|
# Enforce the contract: keep only sentences that actually use the word, and require at
|
||||||
|
# least one. A gloss with no usable examples falls back to the raw dictionary data.
|
||||||
|
examples = [e for e in examples if word.lower() in e.lower()]
|
||||||
|
if not gloss or not examples:
|
||||||
return None
|
return None
|
||||||
return {"gloss": gloss, "examples": examples[:2]}
|
return {"gloss": gloss, "examples": examples[:2]}
|
||||||
|
|
||||||
|
|||||||
+27
-3
@@ -108,13 +108,37 @@ def test_pick_lazy_polishes_older_words(conn, monkeypatch):
|
|||||||
def test_polish_trims_and_caps_two_examples():
|
def test_polish_trims_and_caps_two_examples():
|
||||||
class C:
|
class C:
|
||||||
def chat_text(self, m):
|
def chat_text(self, m):
|
||||||
return 'sure: {"gloss": " Calm and peaceful. ", "examples": ["One.", "Two.", "Three."]} done'
|
return ('sure: {"gloss": " Calm and peaceful. ", "examples": '
|
||||||
|
'["A serene lake.", "The serene night.", "A serene mood."]} done')
|
||||||
out = wotd._polish(C(), "serene", "adjective", "x")
|
out = wotd._polish(C(), "serene", "adjective", "x")
|
||||||
assert out["gloss"] == "Calm and peaceful." and out["examples"] == ["One.", "Two."]
|
assert out["gloss"] == "Calm and peaceful." and out["examples"] == ["A serene lake.", "The serene night."]
|
||||||
|
|
||||||
|
|
||||||
def test_polish_returns_none_without_a_gloss():
|
def test_polish_returns_none_without_a_gloss():
|
||||||
class C:
|
class C:
|
||||||
def chat_text(self, m):
|
def chat_text(self, m):
|
||||||
return '{"examples": ["x"]}'
|
return '{"examples": ["A serene lake."]}'
|
||||||
assert wotd._polish(C(), "serene", None, "x") is None
|
assert wotd._polish(C(), "serene", None, "x") is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_polish_drops_examples_that_dont_use_the_word():
|
||||||
|
# the word must appear (case-insensitive) — example here keeps only the matching one
|
||||||
|
class C:
|
||||||
|
def chat_text(self, m):
|
||||||
|
return '{"gloss": "Calm.", "examples": ["It was quiet.", "A SERENE harbor."]}'
|
||||||
|
out = wotd._polish(C(), "serene", "adjective", "x")
|
||||||
|
assert out["examples"] == ["A SERENE harbor."]
|
||||||
|
|
||||||
|
|
||||||
|
def test_polish_returns_none_when_no_example_uses_the_word():
|
||||||
|
class C:
|
||||||
|
def chat_text(self, m):
|
||||||
|
return '{"gloss": "A warm clear gloss.", "examples": ["Totally unrelated.", "Still nothing."]}'
|
||||||
|
assert wotd._polish(C(), "serene", "adjective", "x") is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_polish_returns_none_with_empty_examples():
|
||||||
|
class C:
|
||||||
|
def chat_text(self, m):
|
||||||
|
return '{"gloss": "A warm clear gloss.", "examples": []}'
|
||||||
|
assert wotd._polish(C(), "serene", "adjective", "x") is None
|
||||||
|
|||||||
Reference in New Issue
Block a user