[英]sentence similarity using word embedding
我是一名博士研究人员,并开始使用word2vec进行研究。 我只想用它来计算句子相似度。 我搜索后发现链接很少,但无法运行。 我在看以下内容:
import numpy as np
from scipy import spatial
index2word_set = set(model.wv.index2word)
def avg_feature_vector(sentence, model, num_features, index2word_set):
words = sentence.split()
feature_vec = np.zeros((num_features, ), dtype='float32')
n_words = 0
for word in words:
if word in index2word_set:
n_words += 1
feature_vec = np.add(feature_vec, model[word])
if (n_words > 0):
feature_vec = np.divide(feature_vec, n_words)
return feature_vec
s1_afv = avg_feature_vector('this is a sentence', model=model, num_features=300, index2word_set=index2word_set)
s2_afv = avg_feature_vector('this is also sentence', model=model,num_features=300, index2word_set=index2word_set)
sim = 1 - spatial.distance.cosine(s1_afv, s2_afv)
print(sim)
不幸的是,由于我不知道如何找到“ index2word_set”,所以我无法运行它。 另外,我应该分配model =吗? 或者,是否有任何简单的命令或指令来实现?
将模型分配给您生成的模型或要使用的任何预定义word2vec模型,对于index2word_set,可以将其设置为model.wv
那应该可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.