From 69f5c20e2fc5e64b39a7a02e6636f107b268c02f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Trevi=C3=B1o?= Date: Fri, 4 Dec 2026 14:59:24 -0600 Subject: [PATCH] =?UTF-8?q?Autom=C3=A1tico:=20a=20study=20no=20line=20mark?= =?UTF-8?q?s=20is=20routed=20by=20the=20words=20its=20documents=20share?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/draft/documents.py | 9 +++++++-- models/draft/layouts.py | 28 ++++++++++++++++++++++++++-- tests/test_draft_layouts.py | 17 +++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/models/draft/documents.py b/models/draft/documents.py index 19e51fa..fff6174 100644 --- a/models/draft/documents.py +++ b/models/draft/documents.py @@ -25,6 +25,7 @@ from models.draft.layouts import ( marks, phrase, printed_line, + shared_words, spread, ) from persistance import llm @@ -80,7 +81,7 @@ def draft(d, read: Read, rows: list[dict], log) -> None: layout.members.extend(members) for one in answer["studies"]: claimed = {i for o in answer["studies"] if o is not one for i in o["documents"]} - if (sub := apart(one, shown, claimed, log)) is False: + if (sub := apart(one, shown, claimed, log, members)) is False: continue kind = kinds.setdefault((marker.casefold(), one["entity"], (sub or "").casefold()), Kind(one["entity"], one["label"], sub or "", [], @@ -98,7 +99,7 @@ def draft(d, read: Read, rows: list[dict], log) -> None: inputs=[read.id], otherwise=True)) # fmt: skip -def apart(study: dict, shown: list[dict], claimed: set[int], log) -> str | None | bool: +def apart(study: dict, shown: list[dict], claimed: set[int], log, layout=()) -> str | None | bool: """What marks the documents of a layout that report `study`: None when all of them do, or when those the model left out report no other study (`claimed`: those another does; the model is not asked to be exhaustive), a phrase found in each of those it named and @@ -112,10 +113,14 @@ def apart(study: dict, shown: list[dict], claimed: set[int], log) -> str | None rest = [r for i, r in enumerate(shown, 1) if i not in named] if found := phrase(" ".join((study["marker"] or "").split()), mine, rest, share=1.0): return found + if found := shared_words(mine, rest, study["label"], layout, only=True): + return found # the study's own name: what a person would route by telling = lambda line: (-kinds_of(line, shown), len(line)) # noqa: E731 line = printed_line(mine, rest, share=1.0, rank=telling) if line and kinds_of(line, shown) < len(shown): # a value others share: no one's name return line + if found := shared_words(mine, rest, study["label"], layout): + return found log(f"{study['label']}: nothing marks its documents apart from the layout's others") return False diff --git a/models/draft/layouts.py b/models/draft/layouts.py index df514fb..0e0f7c0 100644 --- a/models/draft/layouts.py +++ b/models/draft/layouts.py @@ -79,12 +79,12 @@ def condition(marker: str) -> dict: "value": marker} # fmt: skip -def loose(phrase: str | None) -> str | None: +def loose(phrase: str | None, least: int = 2) -> str | None: """A phrase the model wrote as a pattern its document may write a little apart: its words in order, accents either way, up to two words between them ("MAMA, BIOPSIA" finds "MAMA DERECHA, BIOPSIA:").""" words = re.findall(r"[^\W_]+", phrase or "") - if len(words) < 2: + if len(words) < least: return None def letter(c: str) -> str: @@ -135,3 +135,27 @@ def printed_line(members: list[dict], others: list[dict], share=COVERED, rank=No if holds(line, members, others, share): return line return None + + +def bare(word: str) -> str: + return "".join(c for c in unicodedata.normalize("NFD", word.casefold()) if c.isalpha()) + + +def shared_words(members: list[dict], others: list[dict], first="", layout=(), most=3, only=False): + """The fewest words, in order, all of these documents write and none of the others, + accents and case aside (a breast biopsy writes MAMA, whichever side): when no line + marks them. Words of `first` (the study's name) are tried before the others (`only`: + those alone); a word only one of the `layout`'s documents writes (a name) is never one.""" + words = re.findall(r"[^\W\d_]+", members[0]["text"]) + preferred = {bare(w) for w in re.findall(r"[^\W\d_]+", first)} + written = Counter(w for r in layout or [*members, *others] + for w in {bare(x) for x in re.findall(r"[^\W\d_]+", r["text"])}) # fmt: skip + for n in range(1, most + 1): + grams = [words[i : i + n] for i in range(len(words) - n + 1)] + grams = [g for g in grams if sum(map(len, g)) >= 4 and all(written[bare(w)] > 1 for w in g)] + grams.sort(key=lambda g: not all(bare(w) in preferred for w in g)) # stable + grams = [g for g in grams if not only or all(bare(w) in preferred for w in g)] + for gram in grams: + if holds(found := loose(" ".join(gram), least=1), members, others, 1.0): + return found + return None diff --git a/tests/test_draft_layouts.py b/tests/test_draft_layouts.py index 933d9a6..da14447 100644 --- a/tests/test_draft_layouts.py +++ b/tests/test_draft_layouts.py @@ -53,3 +53,20 @@ def test_a_phrase_the_model_tidied_still_finds_what_the_document_writes(): cervix = {"label": "Biopsia cervical", "documents": [1, 3], "marker": "CÉRVIX, BIOPSIA"} found = apart(cervix, shown, {2, 4}, print) # written without its accent in C assert [marks(found, r["text"]) for r in shown] == [True, False, True, False] + + +def test_a_study_no_line_marks_is_marked_by_the_words_its_documents_share(): + """Each side writes its own specimen line (MAMA DERECHA, MAMA IZQUIERDA) and the model + names the documents without a phrase: the word they all write marks them.""" + cervix = "ESTUDIO PATOLOGICO\nNOMBRE: {n}\n1 - BIOPSIA DIRIGIDA POR COLPOSCOPIA DE {c}" + breast = "ESTUDIO PATOLOGICO\nNOMBRE: {n}\nDx: {dx}\n1 - BIOPSIA CON AGUJA DE CORTE DE {s}" + shown = docs([cervix.format(n="A", c="CÉRVIX"), cervix.format(n="B", c="CERVIX"), + breast.format(n="C", dx="CA DE MAMA", s="MAMA DERECHA"), + breast.format(n="D", dx="NÓDULO MAMARIO", s="MAMA IZQUIERDA")]) # fmt: skip + mama = {"label": "Biopsia de mama", "documents": [3, 4], "marker": None} + found = apart(mama, shown, {1, 2}, print) + assert [marks(found, r["text"]) for r in shown] == [False, False, True, True] + assert "M" in found and "AGUJA" not in found # the study's own word, before any other + cervical = {"label": "Biopsia cervical", "documents": [1, 2], "marker": None} + found = apart(cervical, shown, {3, 4}, print) # CÉRVIX and CERVIX alike + assert [marks(found, r["text"]) for r in shown] == [True, True, False, False]