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