我使用我自己的语料库使用https://github.com/stanfordnlp/GloVe/blob/master/demo.sh提供的代码生成了 GloVe 向量。所以,我有 .bin 文件和 .txt 文件向量。我正在尝试将这些文件导入到 gensim 中,这样我就可以像处理 word2vec 向量一样使用它们。
我尝试使用二进制格式和文本文件格式更改加载,但最终得到一个酸洗错误:
models = gensim.models.Word2Vec.load(file)
我试过忽略 unicode 错误,但没有用。我仍然收到 unicode 错误。
model = gensim.models.KeyedVectors.load_word2vec_format(file, binary=True, unicode_errors='ignore')
这就是我现在的代码:
from gensim.models import KeyedVectors
import gensim
from gensim.models import word2vec
file = 'vectors.bin'
model = KeyedVectors.load_word2vec_format(file, binary=True, unicode_errors='ignore')
model.wv.most_similar(positive=['woman', 'king'], negative=['man'])
这是我不断收到的错误消息:
Traceback (most recent call last):
File "glove_to_word2vec.py", line 6, in <module>
model = KeyedVectors.load_word2vec_format(file, binary=True) # C binary format
File "/home/users/epair/.local/lib/python3.6/site- packages/gensim/models/keyedvectors.py", line 1498, in load_word2vec_format
limit=limit, datatype=datatype)
File "/home/users/epair/.local/lib/python3.6/site-packages/gensim/models/utils_any2vec.py", line 343, in _load_word2vec_format
header = utils.to_unicode(fin.readline(), encoding=encoding)
File "/home/users/epair/.local/lib/python3.6/site-packages/gensim/utils.py", line 359, in any2unicode
return unicode(text, encoding, errors=errors)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe6 in position 0: invalid continuation byte