Fix #1375 -- out-of-bounds on token.nbor()

This commit is contained in:
Matthew Honnibal 2017-10-24 12:10:39 +02:00
parent a68d89a4f3
commit b66b8f028b
2 changed files with 4 additions and 1 deletions

View File

@ -3,7 +3,7 @@ import pytest
from ...vocab import Vocab
from ...tokens.doc import Doc
@pytest.mark.xfail
def test_issue1375():
'''Test that token.nbor() raises IndexError for out-of-bounds access.'''
doc = Doc(Vocab(), words=['0', '1', '2'])

View File

@ -127,6 +127,9 @@ cdef class Token:
i (int): The relative position of the token to get. Defaults to 1.
RETURNS (Token): The token at position `self.doc[self.i+i]`.
"""
if self.i+i < 0 or (self.i+i >= len(self.doc)):
msg = "Error accessing doc[%d].nbor(%d), for doc of length %d"
raise IndexError(msg % (self.i, i, len(self.doc)))
return self.doc[self.i+i]
def similarity(self, other):