Switch to unified view

a b/tests/pipelines/misc/test_sections.py
1
from pytest import fixture
2
3
from edsnlp.pipelines.misc.sections import Sections, patterns
4
from edsnlp.utils.examples import parse_example
5
6
sections_text = (
7
    "Le patient est admis pour des douleurs dans le bras droit, "
8
    "mais n'a pas de problème de locomotion. "
9
    "Historique d'AVC dans la famille. pourrait être un cas de rhume.\n"
10
    "NBNbWbWbNbWbNBNbNbWbWbNBNbWbNbNbWbNBNbWbNbNBWbWbNbNbNBWbNb"
11
    "WbNbWBNbNbWbNbNBNbWbWbNbWBNbNbWbNBNbWbWbNb\n"
12
    "Pourrait être un cas de rhume.\n"
13
    "Motif :\n"
14
    "<ent section=motif>Douleurs</ent> dans le bras droit.\n"
15
    "Pas d'anomalie détectée."
16
)
17
18
empty_sections_text = """
19
Antécédents :
20
Conclusion :
21
<ent section=conclusion>Patient</ent> va mieux
22
23
Au total:
24
sortie du patient
25
"""
26
27
28
def test_section_detection(doc):
29
    assert doc.spans["sections"]
30
31
32
@fixture
33
def sections_factory(blank_nlp):
34
35
    default_config = dict(
36
        sections=patterns.sections,
37
        add_patterns=True,
38
        attr="NORM",
39
        ignore_excluded=True,
40
    )
41
42
    def factory(**kwargs):
43
44
        config = dict(**default_config)
45
        config.update(kwargs)
46
47
        return Sections(
48
            nlp=blank_nlp,
49
            **config,
50
        )
51
52
    return factory
53
54
55
def test_sections(blank_nlp, sections_factory):
56
57
    blank_nlp.add_pipe("normalizer")
58
59
    sections = sections_factory()
60
61
    text, entities = parse_example(example=sections_text)
62
63
    doc = blank_nlp(text)
64
    doc.ents = [
65
        doc.char_span(ent.start_char, ent.end_char, "generic") for ent in entities
66
    ]
67
68
    doc = sections(doc)
69
70
    for entity, ent in zip(entities, doc.ents):
71
72
        for modifier in entity.modifiers:
73
74
            assert (
75
                getattr(ent._, modifier.key) == modifier.value
76
            ), f"{modifier.key} labels don't match."
77
78
79
def test_empty_sections(blank_nlp, sections_factory):
80
81
    blank_nlp.add_pipe("normalizer")
82
83
    sections = sections_factory()
84
85
    text, entities = parse_example(example=empty_sections_text)
86
87
    doc = blank_nlp(text)
88
    doc.ents = [
89
        doc.char_span(ent.start_char, ent.end_char, label="ent") for ent in entities
90
    ]
91
92
    doc = sections(doc)
93
94
    for section in doc.spans["sections"]:
95
        for ent in section.ents:
96
            ent._.section = section.label_
97
98
    for entity, ent in zip(entities, doc.ents):
99
        for modifier in entity.modifiers:
100
            assert (
101
                getattr(ent._, modifier.key) == modifier.value
102
            ), f"{modifier.key} labels don't match."