Gensim saved dictionary no id2token

I saved the Gensim dictionary to disk. When I load it, the id2tokendict attribute is not populated.

A simple piece of code that stores a dictionary:

dictionary = corpora.Dictionary(tag_docs)
dictionary.save("tag_dictionary_lda.pkl")

Now, when I load it (load it into jupyter notebook), it still works great for matching tokens with identifiers, but it id2tokendoesn’t work (I can not match identifiers with tokens), but actually it id2tokendoesn't fill at all.

> dictionary = corpora.Dictionary.load("../data/tag_dictionary_lda.pkl")
> dictionary.token2id["love"]
Out: 1613

> dictionary.doc2bow(["love"])
Out: [(1613, 1)]

> dictionary.id2token[1613]
Out: 
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input> in <module>()
----> 1 dictionary.id2token[1613]

KeyError: 1613

> list(dictionary.id2token.keys())
Out: []

Any thoughts?

+4
source share
1 answer

You do not need to dictionary.id2token[1613], as you can use directly dictionary[1613].

, dictionary.id2token , . , dictionary.id2token ( Dictionary).

+6

Source: https://habr.com/ru/post/1676778/


All Articles