1 import os 2 # used to convert and load glove pre-trained word vector 3 from gensim.test.utils import datapath, get_tmpfile 4 from gensim.models import KeyedVectors 5 # convert glove to 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)
After the above code is executed, a pre-trained word vector in the form of word2vector is generated locally. The premise of executing the above code is that you downloaded glove.840B.300d.txt
The following is the loaded pre-trained word vector
1 # need to first glove converted to vector by 2 wvmodel = gensim.models.KeyedVectors.load_word2vec_format ( ' word2vec.txt ' , binary = False, encoding = ' UTF-. 8 ' )