[英]Gensim sentences from ontology corpus Unicode error
我在Windows OS10上,使用python 2.7.15 | 蟒蛇。 每當我跑
mymodel=gensim.models.Word2Vec.load (pretrain)
mymodel.min_count = mincount
sentences =gensim.models.word2vec.LineSentence('ontology_corpus.lst')
mymodel.build_vocab(sentences, update=True) # ERROR HERE ****
我收到此錯誤:
Traceback (most recent call last):
File "runWord2Vec.py", line 23, in <module>
mymodel.build_vocab(sentences, update=True)
File "C:xxxx\lib\site-packages\gensim\models\ba
se_any2vec.py", line 936, in build_vocab
sentences=sentences, corpus_file=corpus_file, progress_per=progress_per, tri
m_rule=trim_rule)
File "C:xxxx\lib\site-packages\gensim\models\wo
rd2vec.py", line 1591, in scan_vocab
total_words, corpus_count = self._scan_vocab(sentences, progress_per, trim_r
ule)
File "C:xxxxx\lib\site-packages\gensim\models\wo
rd2vec.py", line 1560, in _scan_vocab
for sentence_no, sentence in enumerate(sentences):
File "C:xxxx\lib\site-packages\gensim\models\wo
rd2vec.py", line 1442, in __iter__
line = utils.to_unicode(line).split()
File "C:xxxx\lib\site-packages\gensim\utils.py"
, line 359, in any2unicode
return unicode(text, encoding, errors=errors)
File "C:xxxxx\lib\encodings\utf_8.py", line 16,
in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe6 in position 124: invalid
continuation byte
現在,這可以追溯到LineSentence類
class LineSentence(object):
def __init__(self, source, max_sentence_length=MAX_WORDS_IN_BATCH, limit=None):
self.source = source
self.max_sentence_length = max_sentence_length
self.limit = limit
def __iter__(self):
"""Iterate through the lines in the source."""
try:
# Assume it is a file-like object and try treating it as such
# Things that don't have seek will trigger an exception
self.source.seek(0)
for line in itertools.islice(self.source, self.limit):
line = utils.to_unicode(line).split()
i = 0
while i < len(line):
yield line[i: i + self.max_sentence_length]
i += self.max_sentence_length
except AttributeError:
# If it didn't work like a file, use it as a string filename
with utils.smart_open(self.source) as fin:
for line in itertools.islice(fin, self.limit):
line = utils.to_unicode(line).split() # ERROR HERE *************
i = 0
while i < len(line):
yield line[i: i + self.max_sentence_length]
i += self.max_sentence_length
在可以從錯誤中看到的最后一個返回中,我可以將error參數更改為error ='ignore'或更改以下行:
utils.to_unicode(line).split()
對此:
line.split()
ontology_corpus.lst文件示例:
<http://purl.obolibrary.org/obo/GO_0090141> EquivalentTo <http://purl.obolibrary.org/obo/GO_0065007> and <http://purl.obolibrary.org/obo/RO_0002213> some <http://purl.obolibrary.org/obo/GO_0000266>
<http://purl.obolibrary.org/obo/GO_0090141> SubClassOf <http://purl.obolibrary.org/obo/GO_0065007>
問題是它可以正常工作,但是由於忽略了編碼錯誤,恐怕結果將是有缺陷的! 有解決方案嗎?還是我的方法會很好?
這可能是因為文件中的某些行包含未正確UTF8編碼的數據。
如果build_vocab()
否則成功,那么如果損壞是無意的,罕見的或不影響您特別感興趣的單詞向量的,則可能不會對最終結果產生太大的影響。(您的示例行不包含任何UTF8損壞或可能存在編碼問題的字符。)
但是,如果有問題,您可以嘗試自己動手閱讀sentences
以觸發build_vocab()
之外的錯誤,從而找出問題的確切build_vocab()
。 例如:
for i, sentence in enumerate(sentences):
print(i)
在哪里停止(如果是結束迭代的錯誤),或者錯誤消息與行號交錯的地方,則會提示您出現問題的行。 您可以檢查文本編輯器中的字符,以查看涉及哪些字符。 然后,您可以考慮使用所涉及的范圍/字符的知識,考慮刪除/更改這些字符,或嘗試發現文件的真實編碼並將其重新編碼為UTF8。
(關於您的明顯語料庫的另一條注釋:請注意,如果將單個標記的許多替代示例散布在整個語料庫中,並與其他標記的對比示例進行交錯,那么單詞向量訓練是最好的。因此,如果您的語料庫是其他一些標記的轉儲包含所有相關標記的源代碼,例如<http://purl.obolibrary.org/obo/GO_0090141>
,如果在訓練之前改寫這些行,則最終矢量可能會有所改善。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.