word2vec得到编码错误

时间:2015-07-31 07:25:08

标签: python word2vec

执行代码时出现以下错误

Traceback (most recent call last):
  File "test.py", line 21, in <module>
    print model.most_similar(positive=['男人'])
  File "/usr/local/lib/python2.7/dist-packages/gensim/models/word2vec.py", line 660, in most_similar
    raise KeyError("word '%s' not in vocabulary" % word)
KeyError: "word '\xe7\x94\xb7\xe4\xba\xba' not in vocabulary"

我的代码在这里

 # -*- coding: utf8 -*    
    from gensim.models import word2vec
    import logging

logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
        sentences = word2vec.Text8Corpus('/tmp/text8')
        model = word2vec.
    Word2Vec(sentences, size=200)
        model.most_similar(['男人'])

1 个答案:

答案 0 :(得分:1)

  

&#34;它通过以下更改起作用。 model.most_similar([U&#39;男人&#39;])&#34;

这意味着您正在使用utf-8编码的字符串而不是unicode字符串,一个好的做法是使用unicode解码输入工作,然后在输出上进行编码。

.decode('utf-8')你的字符串