From d55d6e1cfab6d0380255f7ffb3d7b6791f536054 Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Sat, 19 Aug 2017 16:39:32 +0200 Subject: [PATCH] Fix comparison of Token from different docs. Closes #1257 --- spacy/tests/regression/test_issue1257.py | 12 ++++++++++++ spacy/tokens/token.pyx | 12 ++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 spacy/tests/regression/test_issue1257.py diff --git a/spacy/tests/regression/test_issue1257.py b/spacy/tests/regression/test_issue1257.py new file mode 100644 index 000000000..de6b014a6 --- /dev/null +++ b/spacy/tests/regression/test_issue1257.py @@ -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] diff --git a/spacy/tokens/token.pyx b/spacy/tokens/token.pyx index 5b8c276d8..7b11d6efa 100644 --- a/spacy/tokens/token.pyx +++ b/spacy/tokens/token.pyx @@ -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: