mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-24 17:06:29 +03:00
Add slicing support to Span
This commit is contained in:
parent
5cc2f2b01a
commit
97685aecb7
|
@ -50,7 +50,26 @@ cdef class Span:
|
|||
return 0
|
||||
return self.end - self.start
|
||||
|
||||
def __getitem__(self, int i):
|
||||
def __getitem__(self, object i):
|
||||
if isinstance(i, slice):
|
||||
start, end, step = i.start, i.stop, i.step
|
||||
if start is None:
|
||||
start = 0
|
||||
elif start < 0:
|
||||
start += len(self)
|
||||
start = min(len(self), max(0, start))
|
||||
|
||||
if end is None:
|
||||
end = len(self)
|
||||
elif end < 0:
|
||||
end += len(self)
|
||||
end = min(len(self), max(start, end))
|
||||
|
||||
start += self.start
|
||||
end += self.start
|
||||
|
||||
return self.doc[start:end:i.step]
|
||||
|
||||
if i < 0:
|
||||
return self.doc[self.end + i]
|
||||
else:
|
||||
|
|
|
@ -59,6 +59,24 @@ def test_getitem(EN):
|
|||
|
||||
span = tokens[1:4]
|
||||
assert span[0].orth_ == 'it'
|
||||
subspan = span[:]
|
||||
assert to_str(subspan) == 'it/back/!'
|
||||
subspan = span[:2]
|
||||
assert to_str(subspan) == 'it/back'
|
||||
subspan = span[1:]
|
||||
assert to_str(subspan) == 'back/!'
|
||||
subspan = span[:-1]
|
||||
assert to_str(subspan) == 'it/back'
|
||||
subspan = span[-2:]
|
||||
assert to_str(subspan) == 'back/!'
|
||||
subspan = span[1:2]
|
||||
assert to_str(subspan) == 'back'
|
||||
subspan = span[-2:-1]
|
||||
assert to_str(subspan) == 'back'
|
||||
subspan = span[-50:50]
|
||||
assert to_str(subspan) == 'it/back/!'
|
||||
subspan = span[50:-50]
|
||||
assert subspan.start == subspan.end == 4 and not to_str(subspan)
|
||||
|
||||
|
||||
@pytest.mark.models
|
||||
|
|
Loading…
Reference in New Issue
Block a user