简体   繁体   中英

Number of words in vocabulary gensim word2vec

How can I print the number of words in the model's vocabulary for gensim Word2Vec?

import gensim
model = gensim.models.KeyedVectors.load_word2vec_format("GoogleNews-vectors-negative300.bin", binary=True)

I have tried

len(model.wv.vocab)

But I get this error

Traceback (most recent call last):
  File ".\word2vec.py", line 4, in <module>
    len(model.wv.vocab)
AttributeError: 'KeyedVectors' object has no attribute 'wv'

And this

len(model.vocab)

And it gives this error

Traceback (most recent call last):
  File ".\word2vec.py", line 4, in <module>
    len(model.vocab)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\gensim\models\keyedvectors.py", line 645, in vocab
    raise AttributeError(
AttributeError: The vocab attribute was removed from KeyedVector in Gensim 4.0.0.
Use KeyedVector's .key_to_index dict, .index_to_key list, and methods .get_vecattr(key, attr) and .set_vecattr(key, attr, new_val) instead.

Found it for newer versions on here

len(model.vectors)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM