Add slicing support to Span

This commit is contained in:
Yubing (Tom) Dong 2015-10-06 02:45:49 -07:00
parent 5cc2f2b01a
commit 97685aecb7
2 changed files with 38 additions and 1 deletions

View File

@ -50,7 +50,26 @@ cdef class Span:
return 0 return 0
return self.end - self.start 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: if i < 0:
return self.doc[self.end + i] return self.doc[self.end + i]
else: else:

View File

@ -59,6 +59,24 @@ def test_getitem(EN):
span = tokens[1:4] span = tokens[1:4]
assert span[0].orth_ == 'it' 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 @pytest.mark.models