|
a |
|
b/docs/tutorials/visualization.md |
|
|
1 |
# Visualization |
|
|
2 |
|
|
|
3 |
Let's see how to display the output of a pipeline on a single text. |
|
|
4 |
|
|
|
5 |
```python |
|
|
6 |
import edsnlp, edsnlp.pipes as eds |
|
|
7 |
|
|
|
8 |
nlp = edsnlp.blank("eds") |
|
|
9 |
nlp.add_pipe(eds.normalizer()) |
|
|
10 |
nlp.add_pipe(eds.sentences()) |
|
|
11 |
nlp.add_pipe(eds.covid()) |
|
|
12 |
nlp.add_pipe(eds.negation()) |
|
|
13 |
nlp.add_pipe(eds.hypothesis()) |
|
|
14 |
nlp.add_pipe(eds.family()) |
|
|
15 |
|
|
|
16 |
txt = "Le patient a le covid." |
|
|
17 |
``` |
|
|
18 |
|
|
|
19 |
## Visualize entities in a document |
|
|
20 |
|
|
|
21 |
To print a text and highlight the entities in it, you can use `spacy.displacy`. |
|
|
22 |
|
|
|
23 |
```{ .python .no-check } |
|
|
24 |
from spacy import displacy |
|
|
25 |
|
|
|
26 |
doc = nlp(txt) |
|
|
27 |
options = { |
|
|
28 |
# Optional colors |
|
|
29 |
|
|
|
30 |
# "colors": { |
|
|
31 |
# "covid": "orange", |
|
|
32 |
# "respiratoire": "steelblue", |
|
|
33 |
# }, |
|
|
34 |
} |
|
|
35 |
displacy.render(doc, style="ent", options=options) |
|
|
36 |
``` |
|
|
37 |
|
|
|
38 |
will render like this: |
|
|
39 |
|
|
|
40 |
<center> |
|
|
41 |
<div class="entities" style="line-height: 2.5; direction: ltr">Le patient a le |
|
|
42 |
<mark class="entity" style="background: #ddd; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;"> |
|
|
43 |
covid |
|
|
44 |
<span style="font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-left: 0.5rem">covid</span> |
|
|
45 |
</mark> |
|
|
46 |
.</div> |
|
|
47 |
</center> |
|
|
48 |
|
|
|
49 |
## Visualize entities as a table |
|
|
50 |
|
|
|
51 |
To quickly visualize the output of a pipeline on a document, including the annotated extensions/qualifiers, you can convert the output to a DataFrame and display it. |
|
|
52 |
|
|
|
53 |
```{ .python .no-check } |
|
|
54 |
nlp.pipe([txt]).to_pandas( |
|
|
55 |
converter="ents", |
|
|
56 |
# Add any extension you want to display |
|
|
57 |
span_attributes=["negation", "hypothesis", "family"], |
|
|
58 |
# Shows the entities in doc.ents by default |
|
|
59 |
# span_getter=["ents"] |
|
|
60 |
) |
|
|
61 |
``` |
|
|
62 |
|
|
|
63 |
<div class="md-typeset"> |
|
|
64 |
<div class="md-typeset__table compact-table"> |
|
|
65 |
|
|
|
66 |
<table> |
|
|
67 |
<thead> |
|
|
68 |
<tr style="text-align: right;"> |
|
|
69 |
<th>note_id</th> |
|
|
70 |
<th>start</th> |
|
|
71 |
<th>end</th> |
|
|
72 |
<th>label</th> |
|
|
73 |
<th>lexical_variant</th> |
|
|
74 |
<th>span_type</th> |
|
|
75 |
<th>negation</th> |
|
|
76 |
<th>hypothesis</th> |
|
|
77 |
<th>family</th> |
|
|
78 |
</tr> |
|
|
79 |
</thead> |
|
|
80 |
<tbody> |
|
|
81 |
<tr> |
|
|
82 |
<td>None</td> |
|
|
83 |
<td>16</td> |
|
|
84 |
<td>21</td> |
|
|
85 |
<td>covid</td> |
|
|
86 |
<td>covid</td> |
|
|
87 |
<td>ents</td> |
|
|
88 |
<td>False</td> |
|
|
89 |
<td>False</td> |
|
|
90 |
<td>False</td> |
|
|
91 |
</tr> |
|
|
92 |
</tbody> |
|
|
93 |
</table> |
|
|
94 |
|
|
|
95 |
</div> |
|
|
96 |
</div> |