Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NEL visualisation (links to entities in a knowledge base) #9199

Merged
merged 10 commits into from
Sep 23, 2021
6 changes: 4 additions & 2 deletions spacy/displacy/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def render_ents(
"""Render entities in text.

text (str): Original text.
spans (list): Individual entity spans and their start, end and label.
spans (list): Individual entity spans and their start, end, label, kb_id and kb_url.
title (str / None): Document title set in Doc.user_data['title'].
"""
markup = ""
Expand All @@ -314,6 +314,8 @@ def render_ents(
label = span["label"]
start = span["start"]
end = span["end"]
kb_id = str(span.get("kb_id") or "")
kb_url = str(span.get("kb_url") or "")
svlandeg marked this conversation as resolved.
Show resolved Hide resolved
svlandeg marked this conversation as resolved.
Show resolved Hide resolved
additional_params = span.get("params", {})
entity = escape_html(text[start:end])
fragments = text[offset:start].split("\n")
Expand All @@ -323,7 +325,7 @@ def render_ents(
markup += "</br>"
if self.ents is None or label.upper() in self.ents:
color = self.colors.get(label.upper(), self.default_color)
ent_settings = {"label": label, "text": entity, "bg": color}
ent_settings = {"label": label, "text": entity, "bg": color, "kb_id": kb_id, "kb_url": kb_url}
ines marked this conversation as resolved.
Show resolved Hide resolved
ent_settings.update(additional_params)
markup += self.ent_template.format(**ent_settings)
else:
Expand Down
1 change: 1 addition & 0 deletions spacy/displacy/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<mark class="entity" style="background: {bg}; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;">
{text}
<span style="font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-left: 0.5rem">{label}</span>
<a style="text-decoration: none; color: black; font-weight: bold" href="{kb_url}">{kb_id}</a>
svlandeg marked this conversation as resolved.
Show resolved Hide resolved
svlandeg marked this conversation as resolved.
Show resolved Hide resolved
ines marked this conversation as resolved.
Show resolved Hide resolved
</mark>
"""

Expand Down
4 changes: 2 additions & 2 deletions website/docs/usage/visualizers.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ position.
>
> ```python
> ex = [{"text": "But Google is starting from behind.",
> "ents": [{"start": 4, "end": 10, "label": "ORG"}],
> "ents": [{"start": 4, "end": 10, "label": "ORG", "kb_id": "Q95", "kb_url": "https://www.wikidata.org/entity/Q95"}],
> "title": None}]
> html = displacy.render(ex, style="ent", manual=True)
> ```
Expand All @@ -323,7 +323,7 @@ position.
### ENT input
{
"text": "But Google is starting from behind.",
"ents": [{"start": 4, "end": 10, "label": "ORG"}],
"ents": [{"start": 4, "end": 10, "label": "ORG", "kb_id": "Q95", "kb_url": "https://www.wikidata.org/entity/Q95"}],
"title": None
}
```
Expand Down