python - 2个句子的语义相似性度量

标签 python nlp semantics wordnet

<分区>

我需要衡量两个句子之间的相似度。例如:

s1 = "she is good a dog "
s2 = "she is nice a heel"

我需要证明 "good""nice" 相似。对于名词和动词,路径相似性度量的工作原理如下伪代码:

def get max :
for loop
(wn.synset ('dog ')).path_similarity(wn.synset ('animal'))

结果:.33,这是一个很高的值,那么这些词是相关的,可以说是相似的。但是对于副词("nice""good"),值 .09 很低!

有什么想法吗?

最佳答案

您可以找到good 的所有synsetspath_similarity 然后选择最大值:

>>> from nltk.corpus import wordnet as wn
>>> n=wn.synsets('nice')
>>> g=wn.synsets('good')
>>> [i.path_similarity(n[0]) for i in g]
[0.0625, 0.06666666666666667, 0.07142857142857142, 0.09090909090909091, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]

>>> max(i.path_similarity(n[0]) for i in g)
0.09090909090909091

请注意,一个词的同义词集包含一个词的多种形式,例如verb、none、adj、...所以您需要选择正确的一个!

您还可以使用 wup_similarity 作为另一种选择:

>>> round(max(i.wup_similarity(n[0]) for i in g), 1)
0.4

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).

阅读更多关于 Synsets 的信息 http://www.nltk.org/howto/wordnet.html

关于python - 2个句子的语义相似性度量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29063118/

相关文章:

python - 按 Dataframe 中的位置替换数组中的元素 - Pyspark

python - scypy python 模块中的 odeint() 如何工作?

python - 除了 punkt tokenizer 之外,NLTK 中还有其他句子 tokenizer

java - 使用java创建解释器的工具

ios - objective c error "No visible @interface for ' NSString' declares the selector 'timeIntervalSinceDate:' "

python - 类型错误 : unsupported format string passed to Tensor. __format__

python - statsmodel 中的 MNLogit 返回 nan

machine-learning - 忽略测试数据上缺失的单词特征的方法

parsing - 通过修剪句子来减少斯坦福解析器的解析时间

html - 将 p 或 div 放在节中的 H1 之前