mirror of
				https://github.com/explosion/spaCy.git
				synced 2025-11-04 01:48:04 +03:00 
			
		
		
		
	Make Span hashable. Closes #1019
This commit is contained in:
		
							parent
							
								
									24c4c51f13
								
							
						
					
					
						commit
						4d98511db7
					
				| 
						 | 
				
			
			@ -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)
 | 
			
		||||
 
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user