From e233328d38f2acb8388f5d90985ea75026801c89 Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Tue, 27 Sep 2016 13:22:30 +0200 Subject: [PATCH] Fix Issue #371: Lexeme objects were unhashable. --- spacy/lexeme.pyx | 3 +++ spacy/tests/vocab/test_lexeme.py | 10 ++++++++++ 2 files changed, 13 insertions(+) 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'