因为你还没有真正能够展示一个清晰的输出,所以这是我最好的尝试:
list_A = ['email','user','this','email','address','customer']
list_B = ['email','mail','address','netmail']
在上面的两个列表中,我们将找到列表中每个元素与其余元素之间的余弦相似度。即
list_B
中的
email
与
list_A
中的每个元素:
def word2vec(word):
from collections import Counter
from math import sqrt
cw = Counter(word)
sw = set(cw)
lw = sqrt(sum(c*c for c in cw.values()))
return cw, sw, lw
def cosdis(v1, v2):
common = v1[1].intersection(v2[1])
return sum(v1[0][ch]*v2[0][ch] for ch in common)/v1[2]/v2[2]
list_A = ['email','user','this','email','address','customer']
list_B = ['email','mail','address','netmail']
threshold = 0.80
for key in list_A:
for word in list_B:
try:
res = cosdis(word2vec(word), word2vec(key))
print("The cosine similarity between : {} and : {} is: {}".format(word, key, res*100))
except IndexError:
pass
输出:
The cosine similarity between : email and : email is: 100.0
The cosine similarity between : mail and : email is: 89.44271909999159
The cosine similarity between : address and : email is: 26.967994498529684
The cosine similarity between : netmail and : email is: 84.51542547285166
The cosine similarity between : email and : user is: 22.360679774997898
The cosine similarity between : mail and : user is: 0.0
The cosine similarity between : address and : user is: 60.30226891555272
The cosine similarity between : netmail and : user is: 18.89822365046136
The cosine similarity between : email and : this is: 22.360679774997898
The cosine similarity between : mail and : this is: 25.0
The cosine similarity between : address and : this is: 30.15113445777636
The cosine similarity between : netmail and : this is: 37.79644730092272
The cosine similarity between : email and : email is: 100.0
The cosine similarity between : mail and : email is: 89.44271909999159
The cosine similarity between : address and : email is: 26.967994498529684
The cosine similarity between : netmail and : email is: 84.51542547285166
The cosine similarity between : email and : address is: 26.967994498529684
The cosine similarity between : mail and : address is: 15.07556722888818
The cosine similarity between : address and : address is: 100.0
The cosine similarity between : netmail and : address is: 22.79211529192759
The cosine similarity between : email and : customer is: 31.62277660168379
The cosine similarity between : mail and : customer is: 17.677669529663685
The cosine similarity between : address and : customer is: 42.640143271122085
The cosine similarity between : netmail and : customer is: 40.08918628686365
注意:我也在代码中对
threshold
部分进行了注释,以防您只想获取相似度超过某个阈值(即80%)的单词。
编辑:
OP: 但我想要做的不是逐个比较单词,而是逐个比较列表。
使用
Counter
和
math
:
from collections import Counter
import math
counterA = Counter(list_A)
counterB = Counter(list_B)
def counter_cosine_similarity(c1, c2):
terms = set(c1).union(c2)
dotprod = sum(c1.get(k, 0) * c2.get(k, 0) for k in terms)
magA = math.sqrt(sum(c1.get(k, 0)**2 for k in terms))
magB = math.sqrt(sum(c2.get(k, 0)**2 for k in terms))
return dotprod / (magA * magB)
print(counter_cosine_similarity(counterA, counterB) * 100)
输出:
53.03300858899106