mirror of
https://github.com/explosion/spaCy.git
synced 2025-01-12 18:26:30 +03:00
Raise error for negative arc indices (closes #3917)
This commit is contained in:
parent
69e70ffae1
commit
cc76a26fe8
|
@ -6,6 +6,8 @@ import uuid
|
||||||
from .templates import TPL_DEP_SVG, TPL_DEP_WORDS, TPL_DEP_ARCS, TPL_ENTS
|
from .templates import TPL_DEP_SVG, TPL_DEP_WORDS, TPL_DEP_ARCS, TPL_ENTS
|
||||||
from .templates import TPL_ENT, TPL_ENT_RTL, TPL_FIGURE, TPL_TITLE, TPL_PAGE
|
from .templates import TPL_ENT, TPL_ENT_RTL, TPL_FIGURE, TPL_TITLE, TPL_PAGE
|
||||||
from ..util import minify_html, escape_html, get_entry_points
|
from ..util import minify_html, escape_html, get_entry_points
|
||||||
|
from ..errors import Errors
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_LANG = "en"
|
DEFAULT_LANG = "en"
|
||||||
DEFAULT_DIR = "ltr"
|
DEFAULT_DIR = "ltr"
|
||||||
|
@ -124,6 +126,9 @@ class DependencyRenderer(object):
|
||||||
i (int): Unique ID, typically arrow index.
|
i (int): Unique ID, typically arrow index.
|
||||||
RETURNS (unicode): Rendered SVG markup.
|
RETURNS (unicode): Rendered SVG markup.
|
||||||
"""
|
"""
|
||||||
|
if start < 0 or end < 0:
|
||||||
|
error_args = dict(start=start, end=end, label=label, dir=direction)
|
||||||
|
raise ValueError(Errors.E156.format(**error_args))
|
||||||
level = self.levels.index(end - start) + 1
|
level = self.levels.index(end - start) + 1
|
||||||
x_start = self.offset_x + start * self.distance + self.arrow_spacing
|
x_start = self.offset_x + start * self.distance + self.arrow_spacing
|
||||||
if self.direction == "rtl":
|
if self.direction == "rtl":
|
||||||
|
|
|
@ -428,6 +428,10 @@ class Errors(object):
|
||||||
E154 = ("Either the `nlp` model or the `vocab` should be specified.")
|
E154 = ("Either the `nlp` model or the `vocab` should be specified.")
|
||||||
E155 = ("The `nlp` object should have access to pre-trained word vectors, cf. "
|
E155 = ("The `nlp` object should have access to pre-trained word vectors, cf. "
|
||||||
"https://spacy.io/usage/models#languages.")
|
"https://spacy.io/usage/models#languages.")
|
||||||
|
E156 = ("Can't render negative values for dependency arc start or end. "
|
||||||
|
"Make sure that you're passing in absolute token indices, not "
|
||||||
|
"relative token offsets.\nstart: {start}, end: {end}, label: "
|
||||||
|
"{label}, direction: {dir}")
|
||||||
|
|
||||||
|
|
||||||
@add_codes
|
@add_codes
|
||||||
|
|
|
@ -3,6 +3,7 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from spacy import displacy
|
from spacy import displacy
|
||||||
|
from spacy.displacy.render import DependencyRenderer
|
||||||
from spacy.tokens import Span
|
from spacy.tokens import Span
|
||||||
from spacy.lang.fa import Persian
|
from spacy.lang.fa import Persian
|
||||||
|
|
||||||
|
@ -42,6 +43,17 @@ def test_displacy_parse_deps(en_vocab):
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_displacy_invalid_arcs():
|
||||||
|
renderer = DependencyRenderer()
|
||||||
|
words = [{"text": "This", "tag": "DET"}, {"text": "is", "tag": "VERB"}]
|
||||||
|
arcs = [
|
||||||
|
{"start": 0, "end": 1, "label": "nsubj", "dir": "left"},
|
||||||
|
{"start": -1, "end": 2, "label": "det", "dir": "left"},
|
||||||
|
]
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
renderer.render([{"words": words, "arcs": arcs}])
|
||||||
|
|
||||||
|
|
||||||
def test_displacy_spans(en_vocab):
|
def test_displacy_spans(en_vocab):
|
||||||
"""Test that displaCy can render Spans."""
|
"""Test that displaCy can render Spans."""
|
||||||
doc = get_doc(en_vocab, words=["But", "Google", "is", "starting", "from", "behind"])
|
doc = get_doc(en_vocab, words=["But", "Google", "is", "starting", "from", "behind"])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user