Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to check if word is in NLTK synsets?

I want to check if certain words is present in NLTK's synsets. The following code does that,

from nltk.corpus import wordnet
if wordnet.synsets(word): 
    ... do something ...

but it is rather slow if you have a lot of words to check. Is there a faster way? I don't need the actual synset object, just a yes/no if there's anything there. I don't have the list of words ahead of time, so I can't precompute the answers.

like image 226
mhucka Avatar asked Jan 04 '23 00:01

mhucka


1 Answers

Since all you need to know is which words match, form a set of all lemmas and look up your words there. Forming the set is very fast (and of course set lookups are even faster).

wn_lemmas = set(wordnet.all_lemma_names())
...
if word in wn_lemmas:
    <do something>
like image 83
alexis Avatar answered Jan 05 '23 15:01

alexis