sent1 = nlp("I am happy")
sent2 = nlp("I am sad")
sent3 = nlp("I am joyous")
在这里,有三个句子,我想比较它们的相似性,显然sent1
应该比sent1
更类似于sent3
sent1.similarity(sent2) = 0.9492464724721577
sent1.similarity(sent3) = 0.9239675481730458
正如您从输出中看到的,sent1
与sent2
更相似,我的代码有什么问题
Tags:
此代码计算两个或多个文本文件的相似性:
你的代码没有问题。spaCy中的句子相似性基于单词嵌入,而单词嵌入的一个众所周知的弱点是它们很难区分同义词(happy-joyous)和反义词(happy-sad)
根据您的数字,您可能已经在这样做了,但请确保您正在使用spaCy的大型英语模型
en_core_web_lg
,以获得最佳的单词嵌入为了更准确地嵌入完整的句子,可能值得一试谷歌的通用句子编码器。见:https://tfhub.dev/google/universal-sentence-encoder/4
相关问题 更多 >
编程相关推荐