diff --git a/spacy/tests/spans/test_span.py b/spacy/tests/spans/test_span.py index 14c176edc..d22fa52ae 100644 --- a/spacy/tests/spans/test_span.py +++ b/spacy/tests/spans/test_span.py @@ -77,3 +77,15 @@ def test_spans_override_sentiment(en_tokenizer): assert doc[:2].sentiment == 10.0 assert doc[-2:].sentiment == 10.0 assert doc[:-1].sentiment == 10.0 + + +def test_spans_are_hashable(en_tokenizer): + """Test spans can be hashed.""" + text = "good stuff bad stuff" + tokens = en_tokenizer(text) + span1 = tokens[:2] + span2 = tokens[2:4] + assert hash(span1) != hash(span2) + span3 = tokens[0:2] + assert hash(span3) == hash(span1) + diff --git a/spacy/tokens/span.pyx b/spacy/tokens/span.pyx index 7e2f62171..fb1e5c732 100644 --- a/spacy/tokens/span.pyx +++ b/spacy/tokens/span.pyx @@ -66,6 +66,10 @@ cdef class Span: elif op == 5: return self.start_char >= other.start_char + def __hash__(self): + return hash((self.doc, self.label, self.start_char, self.end_char)) + + def __len__(self): self._recalculate_indices() if self.end < self.start: