diff --git a/spacy/displacy/render.py b/spacy/displacy/render.py index 406824eb4..14d741a3d 100644 --- a/spacy/displacy/render.py +++ b/spacy/displacy/render.py @@ -3,7 +3,7 @@ import uuid from .templates import TPL_DEP_SVG, TPL_DEP_WORDS, TPL_DEP_WORDS_LEMMA, TPL_DEP_ARCS from .templates import TPL_ENT, TPL_ENT_RTL, TPL_FIGURE, TPL_TITLE, TPL_PAGE -from .templates import TPL_ENTS +from .templates import TPL_ENTS, TPL_KB_LINK from ..util import minify_html, escape_html, registry from ..errors import Errors @@ -315,11 +315,8 @@ class EntityRenderer: 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 = "" + kb_url = span.get("kb_url", "#") + kb_link = TPL_KB_LINK.format(kb_id=kb_id, kb_url=kb_url) if kb_id else "" additional_params = span.get("params", {}) entity = escape_html(text[start:end]) fragments = text[offset:start].split("\n") @@ -329,7 +326,12 @@ 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, "kb_link": kb_link} + 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 53fcc1873..e7d3d4266 100644 --- a/spacy/displacy/templates.py +++ b/spacy/displacy/templates.py @@ -51,19 +51,22 @@ TPL_ENTS = """ TPL_ENT = """ {text} - {label} - {kb_link} - + {label}{kb_link} """ TPL_ENT_RTL = """ {text} - {label} + {label}{kb_link} """ +# Important: this needs to start with a space! +TPL_KB_LINK = """ + {kb_id} +""" + TPL_PAGE = """ diff --git a/website/docs/usage/visualizers.md b/website/docs/usage/visualizers.md index cb643aed1..072718f91 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", "kb_id": "Q95", "kb_url": "https://www.wikidata.org/entity/Q95"}], +> "ents": [{"start": 4, "end": 10, "label": "ORG"}], > "title": None}] > html = displacy.render(ex, style="ent", manual=True) > ``` @@ -321,6 +321,15 @@ position. ```python ### ENT input +{ + "text": "But Google is starting from behind.", + "ents": [{"start": 4, "end": 10, "label": "ORG"}], + "title": None +} +``` + +```python +### ENT input with knowledge base links { "text": "But Google is starting from behind.", "ents": [{"start": 4, "end": 10, "label": "ORG", "kb_id": "Q95", "kb_url": "https://www.wikidata.org/entity/Q95"}],