我有一个名为model.txt. 这包含超过一百万个法语单词的 100 个维度向量。这些词包含重音字符,例如é、â、î 或 ô。
让我用下面的例子来解释我的问题:考虑这两个词和它们各自的向量,这两个词都取自model.txt:
etait -0.100460 -0.127720 ...
était 0.094601 -0.266495 ...
这两个词表示相同的含义,但前者没有重音,而后者有重音。
现在我正在尝试使用gensim.models.KeyedVectors以下方式加载这个词嵌入:
model = KeyedVectors.load_word2vec_format(open(model_location, 'r',
encoding='utf8'),
binary=False)
word_vectors = model.wv
我收到以下错误:
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-82-e17c33c552da> in <module>
10 model = KeyedVectors.load_word2vec_format(open(model_location, 'r',
11 encoding='utf8'),
---> 12 binary=False)
13
14 word_vectors = model.wv
D:\Anaconda\lib\site-packages\gensim\models\keyedvectors.py in load_word2vec_format(cls, fname, fvocab, binary, encoding, unicode_errors, limit, datatype)
1547 return _load_word2vec_format(
1548 cls, fname, fvocab=fvocab, binary=binary, encoding=encoding, unicode_errors=unicode_errors,
-> 1549 limit=limit, datatype=datatype)
1550
1551 @classmethod
D:\Anaconda\lib\site-packages\gensim\models\utils_any2vec.py in _load_word2vec_format(cls, fname, fvocab, binary, encoding, unicode_errors, limit, datatype, binary_chunk_size)
286 vocab_size, vector_size, datatype, unicode_errors, binary_chunk_size)
287 else:
--> 288 _word2vec_read_text(fin, result, counts, vocab_size, vector_size, datatype, unicode_errors, encoding)
289 if result.vectors.shape[0] != len(result.vocab):
290 logger.info(
D:\Anaconda\lib\site-packages\gensim\models\utils_any2vec.py in _word2vec_read_text(fin, result, counts, vocab_size, vector_size, datatype, unicode_errors, encoding)
213 def _word2vec_read_text(fin, result, counts, vocab_size, vector_size, datatype, unicode_errors, encoding):
214 for line_no in range(vocab_size):
--> 215 line = fin.readline()
216 if line == b'':
217 raise EOFError("unexpected end of input; is count incorrect or file otherwise damaged?")
D:\Anaconda\lib\codecs.py in decode(self, input, final)
320 # decode input (taking the buffer into account)
321 data = self.buffer + input
--> 322 (result, consumed) = self._buffer_decode(data, self.errors, final)
323 # keep undecoded input until the next call
324 self.buffer = data[consumed:]
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 7110-7111: invalid continuation byte
如果我的文件以不同的格式编码,我认为这是有道理的。但是,使用 git 我尝试检查文件的编码file *并得到:
model.txt: UTF-8 Unicode text, with very long lines
现在,如果我尝试编写上述代码并将编码设置为latin1,则加载此文档没有任何问题,但代价是无法访问任何包含重音的单词。基本上在执行时抛出一个超出词汇的错误:
word_vectors.word_vec('était')
我应该如何解决这个问题?我也有.bin模型的文件,我应该尝试用它来加载我的单词及其对应的向量吗?