我的系统中安装了gensim。我用gensim进行了总结。现在,我想找到句子与显示错误的相似之处。示例代码如下。我已经下载了Google新闻载体。
from gensim.models import KeyedVectors
#two sample sentences
s1 = 'the first sentence'
s2 = 'the second text'
#model = gensim.models.KeyedVectors.load_word2vec_format('../GoogleNews-vectors-negative300.bin', binary=True)
model = gensim.models.KeyedVectors.load_word2vec_format('./data/GoogleNews-vectors-negative300.bin.gz', binary=True)
#calculate distance between two sentences using WMD algorithm
distance = model.wmdistance(s1, s2)
print ('distance = %.3f' % distance)
错误############################################# ###
****跟踪(最近通话最近):文件“ / home / abhi / Desktop / CHiir / Custering& summarization / .idea / FInal_version / sentence_embedding.py”,第7行,在 模型= gensim.models.KeyedVectors.load_word2vec_format('./ data / GoogleNews-vectors-negative300.bin.gz', binary = True)NameError:未定义名称'gensim'****
答案 0 :(得分:1)
使用from x import y
导入只能让您使用y
,而不能使用x
。
您可以使用import gensim
代替from gensim.models import KeyedVectors
,也可以直接使用导入的KeyedVectors
:
model = KeyedVectors.load_word2vec_format('./data/GoogleNews-vectors-negative300.bin.gz', binary=True)