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
10 changes: 8 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,12 @@ def render_ents(
label = span["label"]
start = span["start"]
end = span["end"]
kb_id = span.get("kb_id", "")
kb_url = span.get("kb_url", "")
ines marked this conversation as resolved.
Show resolved Hide resolved
if kb_id:
kb_link = """<a style="text-decoration: none; color: black; font-weight: bold" href="{}">{}</a>""".format(kb_url, kb_id)
else:
kb_link = ""
additional_params = span.get("params", {})
entity = escape_html(text[start:end])
fragments = text[offset:start].split("\n")
Expand All @@ -323,7 +329,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_link": kb_link}
ent_settings.update(additional_params)
markup += self.ent_template.format(**ent_settings)
else:
Expand Down
4 changes: 3 additions & 1 deletion spacy/displacy/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
TPL_ENT = """
<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>
<span style="font-size: 0.8em; font-weight: bold; line-height: 1; border-radius: 0.35em; vertical-align: middle; margin-left: 0.5rem">{label}
{kb_link}
</span>
</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