Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions packages/flow/openadapt_flow/form_tx/pack_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,11 @@
"box",
}
)
#: Words that mark a field as another person's (data:
#: ``presets/words/secondary_role_qualifiers.json``). A domain schema may
#: replace them with ``secondary_role_qualifiers``.
_SECONDARY_ROLE_QUALIFIERS = frozenset(
{
"emergency",
"guarantor",
"subscriber",
"employer",
"referring",
"physician",
"provider",
"coapplicant",
"secondary",
"mailing",
"previous",
"prior",
"guardian",
"witness",
}
w.casefold() for w in load_words("secondary_role_qualifiers")
)


Expand Down Expand Up @@ -131,6 +119,15 @@ class CanonicalDomainSchema(BaseModel):
schema_id: str = Field(min_length=1)
fields: tuple[CanonicalFieldSpec, ...]
expected_outputs: tuple[CanonicalFieldSpec, ...] = ()
# Override of the engine's default role qualifiers (see
# ``_SECONDARY_ROLE_QUALIFIERS``). None keeps the default.
secondary_role_qualifiers: Optional[tuple[str, ...]] = None

def role_qualifiers(self) -> frozenset[str]:
"""The role qualifiers induction uses for this domain."""
if self.secondary_role_qualifiers is None:
return _SECONDARY_ROLE_QUALIFIERS
return frozenset(w.casefold() for w in self.secondary_role_qualifiers)


class PackInductionReport(BaseModel):
Expand Down Expand Up @@ -311,13 +308,19 @@ def infer_control_kind(ctrl: ObservedControl) -> ControlKind:
return ControlKind.TEXT


def _spec_allowed_qualifiers(spec: CanonicalFieldSpec) -> set[str]:
def _spec_allowed_qualifiers(
spec: CanonicalFieldSpec, qualifiers: frozenset[str] = _SECONDARY_ROLE_QUALIFIERS
) -> set[str]:
corpus = " ".join((spec.dossier_key, *spec.synonyms, *spec.section_hints))
words = set(_normalize_phrase(corpus).split())
return words & _SECONDARY_ROLE_QUALIFIERS
return words & qualifiers


def _score_control_for_spec(ctrl: ObservedControl, spec: CanonicalFieldSpec) -> int:
def _score_control_for_spec(
ctrl: ObservedControl,
spec: CanonicalFieldSpec,
qualifiers: frozenset[str] = _SECONDARY_ROLE_QUALIFIERS,
) -> int:
"""Return deterministic non-negative match score between ``ctrl`` and ``spec``."""
label_full = _normalize_phrase(ctrl.label_text)
label_core = _normalize_core_label(ctrl.label_text)
Expand All @@ -334,9 +337,9 @@ def _score_control_for_spec(ctrl: ObservedControl, spec: CanonicalFieldSpec) ->
ctrl_words = set(label_full.split()) | set(section_norm.split())
for seq in attr_sequences:
ctrl_words.update(seq)
disallowed_qualifiers = (
ctrl_words & _SECONDARY_ROLE_QUALIFIERS
) - _spec_allowed_qualifiers(spec)
disallowed_qualifiers = (ctrl_words & qualifiers) - _spec_allowed_qualifiers(
spec, qualifiers
)
if disallowed_qualifiers:
return 0

Expand Down Expand Up @@ -518,7 +521,7 @@ def _induce_summary_check(
for ctrl in eligible_controls:
if ctrl.control_id in claimed_control_ids:
continue
sc = _score_control_for_spec(ctrl, out_spec)
sc = _score_control_for_spec(ctrl, out_spec, domain.role_qualifiers())
if sc > 0:
scored.append((sc, ctrl))
if not scored:
Expand Down Expand Up @@ -596,7 +599,7 @@ def _induce_summary_check(
for ctrl in readonly_controls:
if ctrl.control_id in claimed_control_ids:
continue
sc = _score_control_for_spec(ctrl, field_spec)
sc = _score_control_for_spec(ctrl, field_spec, domain.role_qualifiers())
if sc > 0:
scored_rb.append((sc, ctrl))
if not scored_rb:
Expand Down Expand Up @@ -699,7 +702,7 @@ def induce_form_pack(
for s_idx, screen_ctrls in enumerate(editable_by_screen):
scored_on_screen: list[tuple[int, int, ObservedControl]] = []
for c_idx, ctrl in screen_ctrls:
sc = _score_control_for_spec(ctrl, spec)
sc = _score_control_for_spec(ctrl, spec, domain.role_qualifiers())
if sc > 0:
scored_on_screen.append((sc, c_idx, ctrl))
if not scored_on_screen:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"schema": "openadapt.form-word-list/v1",
"name": "secondary_role_qualifiers",
"about": "Words that mark a field as belonging to someone other than the form's main person (an emergency contact, a guarantor). During induction a control whose label or attributes carry one of these matches only a canonical field that names the same word. A domain schema may give its own list.",
"words": [
"emergency",
"guarantor",
"subscriber",
"employer",
"referring",
"physician",
"provider",
"coapplicant",
"secondary",
"mailing",
"previous",
"prior",
"guardian",
"witness"
]
}
50 changes: 49 additions & 1 deletion packages/flow/tests/test_form_tx_presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def test_each_moved_preset_ships_as_json(preset: str) -> None:
path = Path(pack_compiler.__file__).with_name("presets") / f"{preset}.json"
data = json.loads(path.read_text(encoding="utf-8"))
assert data["schema"] == PRESET_SCHEMA
assert load_domain_schema(preset).model_dump(mode="json") == data["preset"]
stored = pack_compiler.CanonicalDomainSchema.model_validate(data["preset"])
assert load_domain_schema(preset) == stored


def test_an_unknown_preset_is_refused() -> None:
Expand Down Expand Up @@ -129,6 +130,10 @@ def _next_button_pattern() -> object:
)


def _secondary_role_qualifiers() -> object:
return pack_compiler._SECONDARY_ROLE_QUALIFIERS


#: word list: (current value, the commit before the move, file, constant)
MOVED_WORDS = {
"primary_entity_kinds": (
Expand All @@ -137,6 +142,12 @@ def _next_button_pattern() -> object:
"packages/flow/openadapt_flow/form_tx/reconciler.py",
"_PRIMARY_ENTITY_KINDS",
),
"secondary_role_qualifiers": (
_secondary_role_qualifiers,
"2f506d6c463aef8098fc8fcc2fd5bc46e55c1829",
"packages/flow/openadapt_flow/form_tx/pack_compiler.py",
"_SECONDARY_ROLE_QUALIFIERS",
),
}


Expand Down Expand Up @@ -193,3 +204,40 @@ def test_a_malformed_word_list_is_refused(
presets.load_words("bad")
finally:
presets.load_words.cache_clear()


def test_a_domain_schema_can_override_the_role_qualifiers() -> None:
from openadapt_flow.form_tx.types import ObservedControl

spec = pack_compiler.CanonicalFieldSpec(
dossier_key="phone", synonyms=("phone", "phone number")
)
ctrl = ObservedControl.model_construct(
control_id="c1",
dom_selector="#g",
label_text="Guarantor phone",
name_attr="guarantor_phone",
id_attr="guarantor_phone",
section_text="",
input_type="text",
role="textbox",
tag="input",
visible=True,
disabled=False,
readonly=False,
options=(),
group_label="",
aria_label="",
placeholder="",
)
domain = pack_compiler.build_patient_registration_domain_schema()
assert domain.role_qualifiers() == pack_compiler._SECONDARY_ROLE_QUALIFIERS
blocked = pack_compiler._score_control_for_spec(
ctrl, spec, domain.role_qualifiers()
)
assert blocked == 0
open_domain = domain.model_copy(update={"secondary_role_qualifiers": ()})
allowed = pack_compiler._score_control_for_spec(
ctrl, spec, open_domain.role_qualifiers()
)
assert allowed > 0
Loading