wordnet
wordnet copied to clipboard
Retrieving synsets while in a loop over WordNet.words() raises RuntimeError
Somehow looking up the synsets for a word changes the underlying dictionary while iterating over it.
>>> import wn
>>> w = wn.WordNet()
>>> for word in w.words(lang='eng'):
... w.synsets(word, pos='n')
...
[]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/goodmami/postdoc/wnsd/env/lib/python3.8/site-packages/wn/__init__.py", line 184, in all_lemma_names
for lemma_name in _lemma_pos_offset_map:
RuntimeError: dictionary changed size during iteration
One workaround is to build the words list first:
>>> import wn
>>> w = wn.WordNet()
>>> words = list(w.words(lang='eng'))
>>> for word in words:
... w.synsets(word, pos='n')
...
>>>
Ah, because of that "smart" caching thing that original NLTK WN API was doing.
This is fixable. Let me put on the TODO.