diff --git a/spacy/lexeme.pyx b/spacy/lexeme.pyx index 866f62da1..459c9c199 100644 --- a/spacy/lexeme.pyx +++ b/spacy/lexeme.pyx @@ -68,6 +68,9 @@ cdef class Lexeme: else: raise NotImplementedError(op) + def __hash__(self): + return self.c.orth + def set_flag(self, attr_id_t flag_id, bint value): Lexeme.c_set_flag(self.c, flag_id, value) diff --git a/spacy/tests/vocab/test_lexeme.py b/spacy/tests/vocab/test_lexeme.py index 9d6e7c96c..e278fd707 100644 --- a/spacy/tests/vocab/test_lexeme.py +++ b/spacy/tests/vocab/test_lexeme.py @@ -30,3 +30,13 @@ def test_lexeme_lt(en_vocab): assert noun < opera assert opera > noun + +def test_lexeme_hash(en_vocab): + '''Test that lexemes are hashable.''' + phantom = en_vocab['phantom'] + + opera = en_vocab['opera'] + + lexes = {phantom: phantom, opera: opera} + assert lexes[phantom].orth_ == 'phantom' + assert lexes[opera].orth_ == 'opera'