diff --git a/spacy/displacy/render.py b/spacy/displacy/render.py index ba56beca3..406824eb4 100644 --- a/spacy/displacy/render.py +++ b/spacy/displacy/render.py @@ -305,7 +305,7 @@ class EntityRenderer: """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 = "" @@ -314,6 +314,12 @@ class EntityRenderer: 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 = """{}""".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") @@ -323,7 +329,7 @@ class EntityRenderer: markup += "
" 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: diff --git a/spacy/displacy/templates.py b/spacy/displacy/templates.py index b9cbf717b..53fcc1873 100644 --- a/spacy/displacy/templates.py +++ b/spacy/displacy/templates.py @@ -51,7 +51,9 @@ TPL_ENTS = """ TPL_ENT = """ {text} - {label} + {label} + {kb_link} + """ diff --git a/website/docs/usage/visualizers.md b/website/docs/usage/visualizers.md index cc73e7e67..cb643aed1 100644 --- a/website/docs/usage/visualizers.md +++ b/website/docs/usage/visualizers.md @@ -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) > ``` @@ -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 } ```