Fix comparison of Token from different docs. Closes #1257

This commit is contained in:
Matthew Honnibal 2017-08-19 16:39:32 +02:00
parent 9b6a5df15e
commit d55d6e1cfa
2 changed files with 22 additions and 2 deletions

View File

@ -0,0 +1,12 @@
'''Test tokens compare correctly'''
from __future__ import unicode_literals
from ..util import get_doc
from ...vocab import Vocab
def test_issue1257():
doc1 = get_doc(Vocab(), ['a', 'b', 'c'])
doc2 = get_doc(Vocab(), ['a', 'c', 'e'])
assert doc1[0] != doc2[0]
assert not doc1[0] == doc2[0]

View File

@ -62,18 +62,26 @@ cdef class Token:
def __richcmp__(self, Token other, int op):
# http://cython.readthedocs.io/en/latest/src/userguide/special_methods.html
cdef Doc my_doc = self.doc
cdef Doc other_doc = other.doc
my = self.idx
their = other.idx if other is not None else None
if op == 0:
return my < their
elif op == 2:
return my == their
if my_doc is other_doc:
return my == their
else:
return False
elif op == 4:
return my > their
elif op == 1:
return my <= their
elif op == 3:
return my != their
if my_doc is other_doc:
return my != their
else:
return True
elif op == 5:
return my >= their
else: