diff --git a/openadapt/transducer_obligations.py b/openadapt/transducer_obligations.py index 79b65a8c30..51c635db48 100644 --- a/openadapt/transducer_obligations.py +++ b/openadapt/transducer_obligations.py @@ -204,10 +204,35 @@ def _labels(ob: CompiledFieldObligation) -> list[str]: } +#: Words in front of a free-text label's head noun that point at another +#: record or at text that isn't the saved value: ``Previous Notes``, ``Draft +#: Comment``, ``Expected Summary``. A label with one of them never names a +#: phrase obligation through its head noun. +_OTHER_RECORD_MODIFIERS = frozenset( + { + "previous", "prior", "old", "older", "original", "former", "last", + "earlier", "draft", "pending", "proposed", "suggested", "expected", + "example", "sample", "template", "default", "rejected", "deleted", + "archived", "superseded", "merged", "parent", "linked", "related", + "other", "unsaved", "new", + } +) # fmt: skip +_FREE_TEXT_KINDS = frozenset({"phrase", "narrative_criterion"}) + + def _label_names_obligation(ob: CompiledFieldObligation, label: str) -> bool: """Every word of the page label names the obligation: a word of its kind (``_KIND_LABEL_WORDS``), of its own label, or, for an ID, its prefix or - a listed name for it (``_PREFIX_WORDS``).""" + a listed name for it (``_PREFIX_WORDS``). + + A free-text obligation (a quoted phrase) is also named by a compound + field label whose head noun, its last word, is a free-text word + (``Customer Details``, ``Clinical Notes``, ``Appeal Reason``), as long as + no word before the head points at another record or an unsaved state + (``Previous Notes``, ``Draft Comment``). Free-text fields are usually + named for what they describe, so requiring every word to be a kind word + rejected correct saves. Amounts, dates, codes and IDs keep the full rule. + """ words = re.findall(r"[a-z]+", _abbreviated_label(label)) if not words: @@ -218,7 +243,14 @@ def _label_names_obligation(ob: CompiledFieldObligation, label: str) -> bool: prefix = _id_prefix(ob) if prefix is not None: allowed |= {prefix.lower()} | _PREFIX_WORDS.get(prefix, frozenset()) - return set(words) <= allowed + if set(words) <= allowed: + return True + return ( + ob.kind in _FREE_TEXT_KINDS + and len(words) <= 4 + and words[-1] in _KIND_LABEL_WORDS["phrase"] + and not set(words[:-1]) & _OTHER_RECORD_MODIFIERS + ) def _abbreviated_label(label: str) -> str: diff --git a/tests/test_transducer_obligations.py b/tests/test_transducer_obligations.py index 20dcf1ee63..5aeb720806 100644 --- a/tests/test_transducer_obligations.py +++ b/tests/test_transducer_obligations.py @@ -404,3 +404,58 @@ def test_page_labels_must_name_the_obligation_and_carry_no_failure(spec, readbac r = diff_readback_against_obligations(obs, readback) assert not r.all_verified assert all(o.status is not ObligationStatus.VERIFIED for o in obs) + + +_PHRASE = "Ships quarterly to three regional warehouses." + + +@pytest.mark.parametrize( + "readback", + [ + f"Customer Name: Acme\nCustomer Details: {_PHRASE}\nTerritory: All", + f"Clinical Notes: {_PHRASE}", + f"Appeal Reason: {_PHRASE}", + ], +) +def test_phrase_verifies_as_whole_value_of_compound_free_text_label(readback): + obs = compile_obligations_from_spec(f'Set the details field to: "{_PHRASE}" Save.') + assert [o.kind for o in obs] == ["phrase"] + assert diff_readback_against_obligations(obs, readback).all_verified + + +@pytest.mark.parametrize( + "readback", + [ + # A modifier that points at another record or an unsaved state. + f"Previous Notes: {_PHRASE}", + f"Draft Comment: {_PHRASE}", + f"Expected Summary: {_PHRASE}", + # A head noun that isn't a free-text word. + f"Customer Name: {_PHRASE}", + # Extra context inside the bound value, or a failure on the line. + f"Customer Details: {_PHRASE} (not saved)", + f"Customer Details: {_PHRASE} Error", + f"Error: Customer Details: {_PHRASE}", + # Only part of the phrase. + "Customer Details: Ships quarterly", + # One label, two different values. + f"Customer Details: {_PHRASE}\nCustomer Details: Something else", + ], +) +def test_phrase_compound_label_still_rejects_loose_reads(readback): + obs = compile_obligations_from_spec(f'Set the details field to: "{_PHRASE}" Save.') + assert not diff_readback_against_obligations(obs, readback).all_verified + + +def test_compound_label_rule_never_loosens_numbers(): + # The adversarial case behind the strict matcher: 5 never matches "50 mg". + dose = compile_obligations_from_spec("", {"dose": "5"}) + assert not diff_readback_against_obligations(dose, "Dose: 50 mg").all_verified + phrase = compile_obligations_from_spec('Record "5" in the notes.') + assert not diff_readback_against_obligations( + phrase, "Clinical Notes: 50 mg" + ).all_verified + amount = compile_obligations_from_spec("Charge $250.00.") + assert not diff_readback_against_obligations( + amount, "Charge Details: $1,250.00" + ).all_verified