Make Span hashable. Closes #1019

This commit is contained in:
Matthew Honnibal 2017-04-26 19:01:05 +02:00
parent 24c4c51f13
commit 4d98511db7
2 changed files with 16 additions and 0 deletions

View File

@ -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)

View File

@ -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: