wordnet icon indicating copy to clipboard operation
wordnet copied to clipboard

Retrieving synsets while in a loop over WordNet.words() raises RuntimeError

Open goodmami opened this issue 5 years ago • 2 comments

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

goodmami avatar Jun 03 '20 10:06 goodmami

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')
... 
>>>

goodmami avatar Jun 03 '20 10:06 goodmami

Ah, because of that "smart" caching thing that original NLTK WN API was doing.

This is fixable. Let me put on the TODO.

alvations avatar Jun 03 '20 14:06 alvations