|
| 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 | +displacy.render(doc, style="ent") |
| 28 | +``` |
| 29 | + |
| 30 | +will render like this: |
| 31 | + |
| 32 | +<center> |
| 33 | +<div class="entities" style="line-height: 2.5; direction: ltr">Le patient a le |
| 34 | +<mark class="entity" style="background: #ddd; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;"> |
| 35 | + covid |
| 36 | + <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> |
| 37 | +</mark> |
| 38 | +.</div> |
| 39 | +</center> |
| 40 | + |
| 41 | +## Visualize entities as a table |
| 42 | + |
| 43 | +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. |
| 44 | + |
| 45 | +```{ .python .no-check } |
| 46 | +nlp.pipe([txt]).to_pandas( |
| 47 | + converter="ents", |
| 48 | + # Add any extension you want to display |
| 49 | + span_attributes=["negation", "hypothesis", "family"], |
| 50 | + # Shows the entities in doc.ents by default |
| 51 | + # span_getter=["ents"] |
| 52 | +) |
| 53 | +``` |
| 54 | + |
| 55 | +<div class="md-typeset"> |
| 56 | +<div class="md-typeset__table compact-table"> |
| 57 | + |
| 58 | +<table> |
| 59 | + <thead> |
| 60 | + <tr style="text-align: right;"> |
| 61 | + <th>note_id</th> |
| 62 | + <th>start</th> |
| 63 | + <th>end</th> |
| 64 | + <th>label</th> |
| 65 | + <th>lexical_variant</th> |
| 66 | + <th>span_type</th> |
| 67 | + <th>negation</th> |
| 68 | + <th>hypothesis</th> |
| 69 | + <th>family</th> |
| 70 | + </tr> |
| 71 | + </thead> |
| 72 | + <tbody> |
| 73 | + <tr> |
| 74 | + <td>None</td> |
| 75 | + <td>16</td> |
| 76 | + <td>21</td> |
| 77 | + <td>covid</td> |
| 78 | + <td>covid</td> |
| 79 | + <td>ents</td> |
| 80 | + <td>False</td> |
| 81 | + <td>False</td> |
| 82 | + <td>False</td> |
| 83 | + </tr> |
| 84 | + </tbody> |
| 85 | +</table> |
| 86 | + |
| 87 | +</div> |
| 88 | +</div> |
0 commit comments