【问题标题】:Similarity between two sentences using word2vec [closed]使用word2vec的两个句子之间的相似性[关闭]
【发布时间】:2018-06-13 08:33:24
【问题描述】:

sentence1 = "这是一个句子" sentence2 = "这是第 2 句" 我想找到这两个句子之间的相似之处。有人可以使用 Word2Vec

帮助我提供 完整代码

【问题讨论】:

  • StackOverflow 不是免费的代码服务。我们可以帮助您解决有关代码的问题,但我们不会为您编写代码。如果您希望有人为您编写代码,请支付开发人员。

标签: python machine-learning deep-learning sentence-similarity


【解决方案1】:

假设您有任何 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-02-02
    • 2019-02-06
    • 2021-03-29
    • 1970-01-01
    • 2019-04-27
    • 2017-05-28
    • 2020-04-21
    • 2011-06-14
    • 2013-04-18
    相关资源
    最近更新 更多