Files
ISSSTELEON/Extraction/tests/test_context.py
T
2026-11-13 11:53:07 -06:00

81 lines
4.0 KiB
Python

"""What a report says about a mention (`extraction.nlp.context`): denied,
supposed, quoted from the clinical request or from history, asked as a
form's caption, or affirmed; and which section of the report it sits in.
Fixtures: none; every line is written inline and folded with `fold`.
"""
from extraction.nlp.context import Assertion, Section, assess, heading_section, sections
from extraction.nlp.text import fold
def _assess(text: str, mention: str, section: Section = Section.GENERAL):
folded = fold(text)
start = folded.index(fold(mention))
return assess(folded, start, start + len(mention), section)
def test_a_trigger_before_the_mention_denies_it():
"""Fails if pre-mention negation triggers ("sin evidencia de") stop applying."""
assert _assess("Sin evidencia de displasia en los cortes examinados", "displasia").assertion is Assertion.NEGADO
def test_a_form_answer_after_the_mention_denies_or_affirms_it():
"""`Término: NO` denies, `Término: SI` affirms. Fails if post-mention
answers stop being read, which would turn a form's "NO" into a finding."""
assert _assess("Carcinoma invasor: NO", "Carcinoma invasor").assertion is Assertion.NEGADO
assert _assess("Displasia: SI", "Displasia").assertion is Assertion.AFIRMADO
def test_a_caption_with_its_answer_elsewhere_is_not_a_finding():
"""A form row that only asks ("Carcinoma invasor:") with the answer in
another cell says nothing yet. Fails if a caption is read as an
affirmed mention."""
context = _assess("Carcinoma invasor: Otro rubro:", "Carcinoma invasor")
assert context.assertion is Assertion.HIPOTETICO
def test_explanatory_text_is_hypothetical():
"""Fails if conditional wording ("en caso de") stops marking a mention
as hypothetical, so a printed recommendation would read as a result."""
context = _assess("En caso de lesión de alto grado se sugiere colposcopía", "lesión de alto grado")
assert context.assertion is Assertion.HIPOTETICO
def test_a_terminator_ends_the_trigger_reach():
"""A negation in a previous sentence does not reach the next one.
Fails if clauses stop ending at a period."""
assert _assess("No se tomó biopsia. Displasia moderada.", "Displasia moderada").assertion is Assertion.AFIRMADO
def test_a_pseudo_trigger_does_not_negate():
""""sin embargo" contains "sin" but negates nothing. Fails if pseudo
triggers stop being masked."""
assert _assess("Muestra escasa, sin embargo displasia leve evidente", "displasia leve").assertion is Assertion.AFIRMADO
def test_a_caption_naming_a_clinical_section_makes_its_content_presumed():
""""Diagnóstico de envío:" is what the requesting physician suspected.
Fails if inline section captions stop qualifying what follows them."""
context = _assess("Edad: 40. Diagnóstico de envío: NIC III.", "NIC III")
assert (context.assertion, context.section) == (Assertion.PRESUNTIVO, Section.CLINICA)
def test_standalone_headings_open_sections_and_captions_only_their_line():
"""A heading alone on its line qualifies the lines under it; a caption
followed by its content only its own line. Fails if a one-line caption
("Método: PCR") swallows the rest of the report."""
lines = [fold(t) for t in ("Método: PCR en tiempo real", "VPH: POSITIVO", "Valores de referencia", "Un positivo indica infección")]
assert sections(lines) == [Section.REFERENCIA, Section.GENERAL, Section.REFERENCIA, Section.REFERENCIA]
def test_the_longest_heading_names_the_section():
""""Diagnóstico clínico" is clinical, "Diagnóstico" a result. Fails if
the shorter heading wins."""
assert heading_section(fold("Diagnóstico clínico:")) is Section.CLINICA
assert heading_section(fold("Diagnóstico:")) is Section.RESULTADO
def test_mentions_in_reference_text_are_hypothetical():
"""Fails if a reference section stops qualifying its mentions."""
assert _assess("Positivo indica ADN viral", "Positivo", Section.REFERENCIA).assertion is Assertion.HIPOTETICO