Merge pull request #9199 from shigapov/master (resolves #9129)

This commit is contained in:
Ines Montani 2021-09-23 19:41:53 +10:00 committed by GitHub
commit beb4a8c524
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 5 deletions

View File

@ -305,7 +305,7 @@ class EntityRenderer:
"""Render entities in text. """Render entities in text.
text (str): Original 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']. title (str / None): Document title set in Doc.user_data['title'].
""" """
markup = "" markup = ""
@ -314,6 +314,12 @@ class EntityRenderer:
label = span["label"] label = span["label"]
start = span["start"] start = span["start"]
end = span["end"] 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", {}) additional_params = span.get("params", {})
entity = escape_html(text[start:end]) entity = escape_html(text[start:end])
fragments = text[offset:start].split("\n") fragments = text[offset:start].split("\n")
@ -323,7 +329,7 @@ class EntityRenderer:
markup += "</br>" markup += "</br>"
if self.ents is None or label.upper() in self.ents: if self.ents is None or label.upper() in self.ents:
color = self.colors.get(label.upper(), self.default_color) 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) ent_settings.update(additional_params)
markup += self.ent_template.format(**ent_settings) markup += self.ent_template.format(**ent_settings)
else: else:

View File

@ -51,7 +51,9 @@ TPL_ENTS = """
TPL_ENT = """ TPL_ENT = """
<mark class="entity" style="background: {bg}; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;"> <mark class="entity" style="background: {bg}; padding: 0.45em 0.6em; margin: 0 0.25em; line-height: 1; border-radius: 0.35em;">
{text} {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> </mark>
""" """

View File

@ -297,7 +297,7 @@ position.
> >
> ```python > ```python
> ex = [{"text": "But Google is starting from behind.", > 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}] > "title": None}]
> html = displacy.render(ex, style="ent", manual=True) > html = displacy.render(ex, style="ent", manual=True)
> ``` > ```
@ -323,7 +323,7 @@ position.
### ENT input ### ENT input
{ {
"text": "But Google is starting from behind.", "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 "title": None
} }
``` ```