def load_word_vec(path, word2idx_dict=None, type='glove'):
    """Load word embedding from local file"""
    fin = open(path, 'r', encoding='utf-8', newline='\n', errors='ignore')
    if type == 'glove':
        word2vec_dict = {}
        for line in fin:
            tokens = line.rstrip().split()
            if word2idx_dict is None or tokens[0] in word2idx_dict.keys():
                word2vec_dict[tokens[0]] = np.asarray(tokens[1:], dtype='float32')
    elif type == 'word2vec':
        import gensim
        word2vec_dict = gensim.models.KeyedVectors.load_word2vec_format(path, binary=True)
    else:
        raise NotImplementedError('No such type: %s' % type)
    return word2vec_dict
Load word embedding from local file
utils/text_process.py