词汇表中的单词数 gensim word2vec

问题描述

如何为 gensim Word2Vec 打印模型词汇表中的单词数?

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

我试过了

len(model.wv.vocab)

但我收到此错误

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

还有这个

len(model.vocab)

它给出了这个错误

Traceback (most recent call last):
  File ".\word2vec.py",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.

解决方法

here 上找到了更新版本

len(model.vectors)