1 import os 2 # 用于转换并加载glove预训练词向量 3 from gensim.test.utils import datapath, get_tmpfile 4 from gensim.models import KeyedVectors 5 # 将glove转换为word2vec 6 from gensim.scripts.glove2word2vec import glove2word2vec 7 path=os.getcwd() 8 glove_file=datapath(os.path.join(path, "glove.840B.300d.txt")) 9 tmp_file=get_tmpfile(os.path.join(path,"word2vec.txt")) 10 glove2word2vec(glove_file, tmp_file)
执行完以上代码后,就在本地生成word2vector形式的预训练词向量。执行以上代码的前提是你下载了glove.840B.300d.txt
下面是加载转换后的预训练词向量
1 # 需要先将glove转为vector才能用 2 wvmodel = gensim.models.KeyedVectors.load_word2vec_format('word2vec.txt',binary=False, encoding='utf-8')