mirror of
https://github.com/explosion/spaCy.git
synced 2025-06-01 03:33:12 +03:00
Make Doc's slicing behavior conform to Python conventions
This commit is contained in:
parent
2fc33e8024
commit
ef2af20cd3
|
@ -16,9 +16,13 @@ cdef class Span:
|
||||||
def __cinit__(self, Doc tokens, int start, int end, int label=0, vector=None,
|
def __cinit__(self, Doc tokens, int start, int end, int label=0, vector=None,
|
||||||
vector_norm=None):
|
vector_norm=None):
|
||||||
if start < 0:
|
if start < 0:
|
||||||
start = tokens.length - start
|
start = tokens.length + start
|
||||||
|
start = min(tokens.length, max(0, start))
|
||||||
|
|
||||||
if end < 0:
|
if end < 0:
|
||||||
end = tokens.length - end
|
end = tokens.length + end
|
||||||
|
end = min(tokens.length, max(start, end))
|
||||||
|
|
||||||
self.doc = tokens
|
self.doc = tokens
|
||||||
self.start = start
|
self.start = start
|
||||||
self.end = end
|
self.end = end
|
||||||
|
|
|
@ -12,17 +12,51 @@ def test_getitem(EN):
|
||||||
with pytest.raises(IndexError):
|
with pytest.raises(IndexError):
|
||||||
tokens[len(tokens)]
|
tokens[len(tokens)]
|
||||||
|
|
||||||
|
def to_str(span):
|
||||||
|
return '/'.join(token.orth_ for token in span)
|
||||||
|
|
||||||
span = tokens[1:1]
|
span = tokens[1:1]
|
||||||
assert not '/'.join(token.orth_ for token in span)
|
assert not to_str(span)
|
||||||
span = tokens[1:4]
|
span = tokens[1:4]
|
||||||
assert '/'.join(token.orth_ for token in span) == 'it/back/!'
|
assert to_str(span) == 'it/back/!'
|
||||||
span = tokens[1:4:1]
|
span = tokens[1:4:1]
|
||||||
assert '/'.join(token.orth_ for token in span) == 'it/back/!'
|
assert to_str(span) == 'it/back/!'
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
tokens[1:4:2]
|
tokens[1:4:2]
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
tokens[1:4:-1]
|
tokens[1:4:-1]
|
||||||
|
|
||||||
|
span = tokens[-3:6]
|
||||||
|
assert to_str(span) == 'He/pleaded'
|
||||||
|
span = tokens[4:-1]
|
||||||
|
assert to_str(span) == 'He/pleaded'
|
||||||
|
span = tokens[-5:-3]
|
||||||
|
assert to_str(span) == 'back/!'
|
||||||
|
span = tokens[5:4]
|
||||||
|
assert span.start == span.end == 5 and not to_str(span)
|
||||||
|
span = tokens[4:-3]
|
||||||
|
assert span.start == span.end == 4 and not to_str(span)
|
||||||
|
|
||||||
|
span = tokens[:]
|
||||||
|
assert to_str(span) == 'Give/it/back/!/He/pleaded/.'
|
||||||
|
span = tokens[4:]
|
||||||
|
assert to_str(span) == 'He/pleaded/.'
|
||||||
|
span = tokens[:4]
|
||||||
|
assert to_str(span) == 'Give/it/back/!'
|
||||||
|
span = tokens[:-3]
|
||||||
|
assert to_str(span) == 'Give/it/back/!'
|
||||||
|
span = tokens[-3:]
|
||||||
|
assert to_str(span) == 'He/pleaded/.'
|
||||||
|
|
||||||
|
span = tokens[4:50]
|
||||||
|
assert to_str(span) == 'He/pleaded/.'
|
||||||
|
span = tokens[-50:4]
|
||||||
|
assert to_str(span) == 'Give/it/back/!'
|
||||||
|
span = tokens[-50:-40]
|
||||||
|
assert span.start == span.end == 0 and not to_str(span)
|
||||||
|
span = tokens[40:50]
|
||||||
|
assert span.start == span.end == 7 and not to_str(span)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.models
|
@pytest.mark.models
|
||||||
def test_serialize(EN):
|
def test_serialize(EN):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user