diff --git a/gensim/models/wrappers/fasttext.py b/gensim/models/wrappers/fasttext.py index 9f68d67ca0..300c6e5a58 100644 --- a/gensim/models/wrappers/fasttext.py +++ b/gensim/models/wrappers/fasttext.py @@ -116,18 +116,14 @@ def init_sims(self, replace=False): def __contains__(self, word): """ - Check if word is present in the vocabulary, or if any word ngrams are present. A vector for the word is - guaranteed to exist if `__contains__` returns True. - + Check if `word` or any character ngrams in `word` are present in the vocabulary. + A vector for the word is guaranteed to exist if `__contains__` returns True. """ if word in self.vocab: return True else: - word_ngrams = set(FastText.compute_ngrams(word, self.min_n, self.max_n)) - if len(word_ngrams & set(self.ngrams.keys())): - return True - else: - return False + char_ngrams = FastText.compute_ngrams(word, self.min_n, self.max_n) + return any(ng in self.ngrams for ng in char_ngrams) class FastText(Word2Vec):