From 595f9dc2e4e29d73ef3fe8f991bbd0324d2faa31 Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Thu, 3 Sep 2020 23:05:41 +0200 Subject: [PATCH] Make displacy color registry consistent with others This was the only registry that expected the registered objects to be dictionaries instead of functions that return something. We can still support plain dicts but we should also support functions for consistency --- spacy/displacy/render.py | 6 ++++++ spacy/errors.py | 2 ++ 2 files changed, 8 insertions(+) diff --git a/spacy/displacy/render.py b/spacy/displacy/render.py index 07550f9aa..984971812 100644 --- a/spacy/displacy/render.py +++ b/spacy/displacy/render.py @@ -249,6 +249,12 @@ class EntityRenderer: colors = dict(DEFAULT_LABEL_COLORS) user_colors = registry.displacy_colors.get_all() for user_color in user_colors.values(): + if callable(user_color): + # Since this comes from the function registry, we want to make + # sure we support functions that *return* a dict of colors + user_color = user_color() + if not isinstance(user_color, dict): + raise ValueError(Errors.E925.format(obj=type(user_color))) colors.update(user_color) colors.update(options.get("colors", {})) self.default_color = DEFAULT_ENTITY_COLOR diff --git a/spacy/errors.py b/spacy/errors.py index be71de820..165714d9e 100644 --- a/spacy/errors.py +++ b/spacy/errors.py @@ -476,6 +476,8 @@ class Errors: E199 = ("Unable to merge 0-length span at doc[{start}:{end}].") # TODO: fix numbering after merging develop into master + E925 = ("Invalid color values for displaCy visualizer: expected dictionary " + "mapping label names to colors but got: {obj}") E926 = ("It looks like you're trying to modify nlp.{attr} directly. This " "doesn't work because it's an immutable computed property. If you " "need to modify the pipeline, use the built-in methods like "