-3

sentence1 = "this is a sentence" sentence2 = "this is sentence 2" 我想找出这两个句子之间的相似性。有人可以帮助我使用Word2Vec的完整代码吗

4

1 回答 1

0

假设您有任何 word2vec 实用程序word2vec

import numpy as np

words1 = sentence1.split(' ')
words2 = sentence2.split(' ')

#The meaning of the sentence can be interpreted as the average of its words
sentence1_meaning = word2vec(words1[0])
count = 1
for w in words1[1:]:
    sentence1_meaning = np.add(sentence1_meaning, word2vec(w))
    count += 1
sentence1_meaning /= count

sentence2_meaning = word2vec(words2[0])
count = 1
for w in words2[1:]:
    sentence2_meaning = np.add(sentence2_meaning, word2vec(w))
    count += 1
sentence2_meaning /= count

#Similarity is the cosine between the vectors
similarity = np.dot(sentence1_meaning, sentence2_meaning)/(np.linalg.norm(sentence1_meaning)*np.linalg.norm(sentence2_meaning))
于 2018-06-13T08:43:14.030 回答