如何比较句子与关键字位置的想法?

我想比较这两个句子。例如,sentence1="football is good, cricket is bad" sentence2="cricket is good,football is bad"

通常这些句子没有关系,这意味着它们的含义不同。但是当我与 python nltk 工具进行比较时,它会给出 100% 的相似性。我该如何解决这个问题?我需要帮助。


潇湘沐
浏览 108回答 2
2回答

杨__羊羊

是wup_similarity的,内部使用单个标记的同义词集来计算相似度Wu-Palmer Similarity: Return a score denoting how similar two word senses are, based on the depth of the two senses in the taxonomy and that of their Least Common Subsumer (most specific ancestor node).因为cricket和的祖先节点football是相同的。wup_similarity将返回1。如果你想解决这个问题,使用wup_similarity不是一个好的选择。最简单的基于令牌的方法是拟合 avectorizer然后计算相似度。例如。from sklearn.feature_extraction.text import CountVectorizerfrom sklearn.metrics.pairwise import cosine_similaritycorpus = ["football is good,cricket is bad", "cricket is good,football is bad"]vectorizer = CountVectorizer(ngram_range=(1, 3))vectorizer.fit(corpus)x1 = vectorizer.transform(["football is good,cricket is bad"])x2 = vectorizer.transform(["cricket is good,football is bad"])cosine_similarity(x1, x2)不过,还有更智能的方法可以测量语义相似度。其中一个可以轻松试用的是 Google 的 USE Encoder。看到这个链接

www说

这种方式语义相似性有点棘手,因为即使您使用上下文计数(n-gram > 5),您也无法很好地处理反义词(例如黑色和白色)。在使用不同的方法之前,您可以尝试使用浅解析器或依赖解析器来提取可以用作维度的主谓关系或主谓宾关系(例如 )。如果这不能为您提供预期的相似性(或适合您的应用程序的值),请使用在非常大的数据上训练的词嵌入。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python