From 6a4575a56c5e02b62be55fd39fd24b371821aea9 Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Wed, 3 Apr 2019 10:13:16 +0200 Subject: [PATCH] Don't make "settings" or "title" required in displaCy data (closes #3531) --- spacy/displacy/render.py | 12 +++++---- spacy/tests/regression/test_issue3531.py | 33 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 spacy/tests/regression/test_issue3531.py diff --git a/spacy/displacy/render.py b/spacy/displacy/render.py index 500e49989..86b933eef 100644 --- a/spacy/displacy/render.py +++ b/spacy/displacy/render.py @@ -50,8 +50,9 @@ class DependencyRenderer(object): rendered = [] for i, p in enumerate(parsed): if i == 0: - self.direction = p["settings"].get("direction", DEFAULT_DIR) - self.lang = p["settings"].get("lang", DEFAULT_LANG) + settings = p.get("settings", {}) + self.direction = settings.get("direction", DEFAULT_DIR) + self.lang = settings.get("lang", DEFAULT_LANG) render_id = "{}-{}".format(id_prefix, i) svg = self.render_svg(render_id, p["words"], p["arcs"]) rendered.append(svg) @@ -254,9 +255,10 @@ class EntityRenderer(object): rendered = [] for i, p in enumerate(parsed): if i == 0: - self.direction = p["settings"].get("direction", DEFAULT_DIR) - self.lang = p["settings"].get("lang", DEFAULT_LANG) - rendered.append(self.render_ents(p["text"], p["ents"], p["title"])) + settings = p.get("settings", {}) + self.direction = settings.get("direction", DEFAULT_DIR) + self.lang = settings.get("lang", DEFAULT_LANG) + rendered.append(self.render_ents(p["text"], p["ents"], p.get("title"))) if page: docs = "".join([TPL_FIGURE.format(content=doc) for doc in rendered]) markup = TPL_PAGE.format(content=docs, lang=self.lang, dir=self.direction) diff --git a/spacy/tests/regression/test_issue3531.py b/spacy/tests/regression/test_issue3531.py new file mode 100644 index 000000000..7b9d0bd2a --- /dev/null +++ b/spacy/tests/regression/test_issue3531.py @@ -0,0 +1,33 @@ +# coding: utf8 +from __future__ import unicode_literals + +from spacy import displacy + + +def test_issue3531(): + """Test that displaCy renderer doesn't require "settings" key.""" + example_dep = { + "words": [ + {"text": "But", "tag": "CCONJ"}, + {"text": "Google", "tag": "PROPN"}, + {"text": "is", "tag": "VERB"}, + {"text": "starting", "tag": "VERB"}, + {"text": "from", "tag": "ADP"}, + {"text": "behind.", "tag": "ADV"}, + ], + "arcs": [ + {"start": 0, "end": 3, "label": "cc", "dir": "left"}, + {"start": 1, "end": 3, "label": "nsubj", "dir": "left"}, + {"start": 2, "end": 3, "label": "aux", "dir": "left"}, + {"start": 3, "end": 4, "label": "prep", "dir": "right"}, + {"start": 4, "end": 5, "label": "pcomp", "dir": "right"}, + ], + } + example_ent = { + "text": "But Google is starting from behind.", + "ents": [{"start": 4, "end": 10, "label": "ORG"}], + } + dep_html = displacy.render(example_dep, style="dep", manual=True) + assert dep_html + ent_html = displacy.render(example_ent, style="ent", manual=True) + assert ent_html