Diff of /docs/tutorials/reason.md [000000] .. [cad161]

Switch to unified view

a b/docs/tutorials/reason.md
1
# Detecting Reason of Hospitalisation
2
3
In this tutorial we will use the pipe `eds.reason` to :
4
5
- Identify spans that corresponds to the reason of hospitalisation
6
- Check if there are named entities overlapping with my span of 'reason of hospitalisation'
7
- Check for all named entities if they are tagged `is_reason`
8
9
```python
10
import edsnlp, edsnlp.pipes as eds
11
12
text = """COMPTE RENDU D'HOSPITALISATION du 11/07/2018 au 12/07/2018
13
MOTIF D'HOSPITALISATION
14
Monsieur Dupont Jean Michel, de sexe masculin, âgée de 39 ans, née le 23/11/1978,
15
a été hospitalisé du 11/08/2019 au 17/08/2019 pour attaque d'asthme.
16
17
ANTÉCÉDENTS
18
Antécédents médicaux :
19
Premier épisode d'asthme en mai 2018."""
20
21
nlp = edsnlp.blank("eds")
22
23
# Extraction d'entités nommées
24
nlp.add_pipe(
25
    eds.matcher(
26
        terms=dict(
27
            respiratoire=[
28
                "asthmatique",
29
                "asthme",
30
                "toux",
31
            ]
32
        ),
33
    ),
34
)
35
nlp.add_pipe(eds.normalizer())
36
nlp.add_pipe(eds.sections())
37
nlp.add_pipe(eds.reason(use_sections=True))
38
39
doc = nlp(text)
40
```
41
42
The pipe `reason` will add a key of spans called `reasons`. We check the first item in this list.
43
44
```python
45
# ↑ Omitted code above ↑
46
47
reason = doc.spans["reasons"][0]
48
reason
49
# Out: hospitalisé du 11/08/2019 au 17/08/2019 pour attaque d'asthme.
50
```
51
52
Naturally, all spans included the `reasons` key have the attribute `#!python reason._.is_reason == True`.
53
54
```python
55
# ↑ Omitted code above ↑
56
57
reason._.is_reason
58
# Out: True
59
```
60
61
<!-- no-check -->
62
63
```python
64
# ↑ Omitted code above ↑
65
66
for e in reason._.ents_reason:  # (1)
67
    print(
68
        "Entity:",
69
        e.text,
70
        "-- Label:",
71
        e.label_,
72
        "-- is_reason:",
73
        e._.is_reason,
74
    )
75
# Out: Entity: asthme -- Label: respiratoire -- is_reason: True
76
```
77
78
1. We check if the span include named entities, their labels and the attribute is_reason
79
80
We can verify that named entities that do not overlap with the spans of reason, have their attribute `#!python reason._.is_reason == False`:
81
82
```{ .python .no-check }
83
for e in doc.ents:
84
    print(e.start, e, e._.is_reason)
85
# Out: 42 asthme True
86
# Out: 54 asthme False
87
```