Skip to content

Commit

Permalink
Merge pull request #9199 from shigapov/master (resolves #9129)
Browse files Browse the repository at this point in the history
  • Loading branch information
ines committed Sep 23, 2021
2 parents d2adfe1 + 57b5fc1 commit beb4a8c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
10 changes: 8 additions & 2 deletions spacy/displacy/render.py
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", "")
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
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
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

0 comments on commit beb4a8c

Please sign in to comment.