From 02fc73ca53effae501c97879e1e95dc156d24f0d Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Thu, 15 Nov 2018 11:40:10 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=AB=20Create=20random=20IDs=20for=20SV?= =?UTF-8?q?Gs=20to=20prevent=20ID=20clashes=20(#2927)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolves #2924. ## Description Fixes problem where multiple visualizations in Jupyter notebooks would have clashing arc IDs, resulting in weirdly positioned arc labels. Generating a random ID prefix so even identical parses won't receive the same IDs for consistency (even if effect of ID clash isn't noticable here.) ### Types of change bug fix ## Checklist - [x] I have submitted the spaCy Contributor Agreement. - [x] I ran the tests, and all new and existing tests passed. - [x] My changes don't require a change to the documentation, or if they do, I've added all required information. --- spacy/displacy/render.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spacy/displacy/render.py b/spacy/displacy/render.py index f5c06d06b..0928f9cb9 100644 --- a/spacy/displacy/render.py +++ b/spacy/displacy/render.py @@ -1,6 +1,8 @@ # coding: utf8 from __future__ import unicode_literals +import random + from .templates import TPL_DEP_SVG, TPL_DEP_WORDS, TPL_DEP_ARCS from .templates import TPL_ENT, TPL_ENTS, TPL_FIGURE, TPL_TITLE, TPL_PAGE from ..util import minify_html, escape_html @@ -38,7 +40,10 @@ class DependencyRenderer(object): minify (bool): Minify HTML markup. RETURNS (unicode): Rendered SVG or HTML markup. """ - rendered = [self.render_svg(i, p['words'], p['arcs']) + # Create a random ID prefix to make sure parses don't receive the + # same ID, even if they're identical + id_prefix = random.randint(0, 999) + rendered = [self.render_svg('{}-{}'.format(id_prefix, i), p['words'], p['arcs']) for i, p in enumerate(parsed)] if page: content = ''.join([TPL_FIGURE.format(content=svg)