给你两句话,你能用word2vec计算出余弦相似度吗,用Python代码实现
时间: 2023-06-08 22:06:16 浏览: 181
可以使用Python的gensim库来实现。首先需要安装gensim库,可以通过以下命令来安装:
```
pip install gensim
```
接着,我们可以使用以下代码来计算两个句子的余弦相似度:
``` python
from gensim.models import Word2Vec
from gensim.matutils import softcossim
# 将两个句子分词并放入列表中
sentence_1 = "This is the first sentence"
sentence_2 = "This is the second sentence"
sentences = [sentence_1.split(), sentence_2.split()]
# 初始化Word2Vec模型并载入预训练的词向量
model_path = '[词向量文件路径]' # 需要替换成实际的词向量文件路径
model = Word2Vec.load(model_path)
# 计算两个句子的余弦相似度
similarity_matrix = model.wv.similarity_matrix(dictionary=sentences, tfidf=None, normalize=True)
soft_sim = softcossim(sentences[0], sentences[1], similarity_matrix)
print("余弦相似度:", soft_sim)
```
需要注意的是,这里使用的词向量需要是预训练好的,可以使用Word2Vec模型来训练得到。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![.zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)