"""`discover_captions`: fails if it stops finding fields a source carries that no registered `FieldSpec` names, or starts re-reporting a field the specs already cover as if it were new.""" from extraction.document import BBox, Line from extraction.nlp import FieldKind, FieldSpec, SegmentLine, discover_captions EXPEDIENTE = FieldSpec("pac_id_hospital", FieldKind.IDENTIFIER, labels=("expediente",)) def lines(*texts: str) -> list[SegmentLine]: return [SegmentLine("doc.pdf", 1, Line(text, BBox(0, i / 10, 1, (i + 1) / 10))) for i, text in enumerate(texts)] def test_a_caption_outside_the_known_specs_is_discovered(): found = discover_captions(lines("Tipo de muestra: Base líquida"), [EXPEDIENTE]) assert [(f.caption, f.value) for f in found] == [("Tipo de muestra", "Base líquida")] def test_a_line_a_known_label_already_claims_is_not_rediscovered(): found = discover_captions(lines("Expediente: EXP-0001"), [EXPEDIENTE]) assert found == [] def test_a_line_without_the_caption_shape_is_ignored(): found = discover_captions(lines("Resultado negativo para lesión intraepitelial"), [EXPEDIENTE]) assert found == [] def test_several_undiscovered_captions_come_back_in_line_order(): found = discover_captions(lines("Instrumento: ADOS-2", "Módulo: 3", "Expediente: EXP-0001"), [EXPEDIENTE]) assert [(f.caption, f.value) for f in found] == [("Instrumento", "ADOS-2"), ("Módulo", "3")] def test_several_captions_on_one_line_are_each_discovered(): """`Nombre: ANA Edad: 52 Sexo: F` holds three captions, and an abbreviation or connector belongs to the caption it starts. Fails if `find_captions` reads one caption per line again, or cuts `Fecha de alta` down to its last word.""" found = discover_captions(lines("Nombre: ANA RUIZ Edad: 52 Grupo: B", "Ingreso: 01/02/2020 Fecha de alta: 03/04/2020"), [EXPEDIENTE]) assert [(f.caption, f.value) for f in found] == [ ("Nombre", "ANA RUIZ"), ("Edad", "52"), ("Grupo", "B"), ("Ingreso", "01/02/2020"), ("Fecha de alta", "03/04/2020") ] def test_a_numbered_caption_is_discovered_without_its_number(): """`4.- Motivo de envío: control` yields the caption `Motivo de envío`. Fails if item numbers stop delimiting captions.""" found = discover_captions(lines("4.- Motivo de envío: control anual"), [EXPEDIENTE]) assert [(f.caption, f.value) for f in found] == [("Motivo de envío", "control anual")]