diff --git a/goodnews/wotd.py b/goodnews/wotd.py index 3e0624d..59d7f59 100644 --- a/goodnews/wotd.py +++ b/goodnews/wotd.py @@ -95,7 +95,10 @@ def _polish(client, word: str, part_of_speech: str | None, definition: str) -> d return None 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()] - 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 {"gloss": gloss, "examples": examples[:2]} diff --git a/tests/test_wotd.py b/tests/test_wotd.py index b834a87..784d1f4 100644 --- a/tests/test_wotd.py +++ b/tests/test_wotd.py @@ -108,13 +108,37 @@ def test_pick_lazy_polishes_older_words(conn, monkeypatch): def test_polish_trims_and_caps_two_examples(): class C: 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") - 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(): class C: def chat_text(self, m): - return '{"examples": ["x"]}' + return '{"examples": ["A serene lake."]}' 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