为什么单个单词的2个文档的相似度为0.00?

时间:2017-08-03 16:49:17

标签: spacy

为什么下面的代码给出了0.00与两个具有相同单词宇航员的文档的相似度?

import spacy
nlp = spacy.en.English()
print (nlp('astronaut').similarity(nlp('astronaut')))
# Result: 0.0

1 个答案:

答案 0 :(得分:1)

原因是单个术语的单词向量是点,而你不能在点之间得到cosine distance similarity

参见多维向量与点的向量比较:

>>> a = nlp(u'astronaut eating apple banana cherry')
>>> b = nlp(u'astronaut eating apple banana fruit')
>>> a.similarity(b)
0.96363932891327542
>>> a.similarity(a)
0.99999997666693974
>>> b.similarity(b)
1.000000996690289
>>> a = nlp(u'astronaut')
>>> b = nlp(u'astronaut')
>>> a.similarity(a)
0.0
>>> b = nlp(u'cosmonaut')
>>> a.similarity(a)
0.0
>>> b.similarity(b)
0.0
>>> a.similarity(b)
0.0
>>> c = nlp(u'single')
>>> a.similarity(c)
0.0