nltk / wordnet

Stand-alone WordNet API
Other
48 stars 15 forks source link

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

Open goodmami opened 4 years ago

goodmami commented 4 years ago

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 commented 4 years ago

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')
... 
>>>
alvations commented 4 years ago

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

This is fixable. Let me put on the TODO.