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

Displacy vizualization for entity linking #9129

Closed
shigapov opened this issue Sep 3, 2021 · 5 comments · Fixed by #9199
Closed

Displacy vizualization for entity linking #9129

shigapov opened this issue Sep 3, 2021 · 5 comments · Fixed by #9199
Labels
enhancement Feature requests and improvements feat / nel Feature: Named Entity linking feat / visualizers Feature: Built-in displaCy and other visualizers

Comments

@shigapov
Copy link
Contributor

shigapov commented Sep 3, 2021

Displacy can vizualize the enitity recognizer. It would be nice to have vizualization for entity linking pipelines as well. So that the links to entities in a knowledge base are also shown.

@polm polm added enhancement Feature requests and improvements feat / visualizers Feature: Built-in displaCy and other visualizers labels Sep 4, 2021
@polm
Copy link
Contributor

polm commented Sep 4, 2021

Thanks for the suggestion!

@shigapov
Copy link
Contributor Author

shigapov commented Sep 8, 2021

Let's add those links using "manual" option in displacy.

In Rendering data manually there is an example:

ex = [{"text": "But Google is starting from behind.",
       "ents": [{"start": 4, "end": 10, "label": "ORG"}],
       "title": None}]
html = displacy.render(ex, style="ent", manual=True)

I propose to add two more keys to "ents" ("kb_id" and "kb_url"):

ex = [{"text": "But Google is starting from behind.",
       "ents": [{"start": 4, "end": 10, "label": "ORG", "kb_id": "Q95", "kb_url": "https://www.wikidata.org/wiki/Q95"}],
       "title": None}]
html = displacy.render(ex, style="ent", manual=True)

Then in templates.py we need to add one more line to "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;">
    {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>
    <a style="text-decoration: none; color: black; font-weight: bold" href="{kb_url}">{kb_id}</a>
</mark>
"""

And in render.py we need to add two more lines (to specify kb_url and kb_id) and replace ent_settings = {"label": label, "text": entity, "bg": color}:

kb_id = str(span.get("kb_id") or "")
kb_url = str(span.get("kb_url") or "")
ent_settings = {"label": label, "text": entity, "bg": color, "kb_id": kb_id, "kb_url": kb_url}

If that's fine, I could make PR.

@shigapov
Copy link
Contributor Author

shigapov commented Sep 8, 2021

Then it looks like
Screenshot_2021-09-08 displaCy
with clickable links to entities.

@polm
Copy link
Contributor

polm commented Sep 12, 2021

That looks good to me. If you go ahead and open a PR someone more familiar with the related bits of code can review it.

@github-actions
Copy link
Contributor

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Feature requests and improvements feat / nel Feature: Named Entity linking feat / visualizers Feature: Built-in displaCy and other visualizers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants