Is there any solution to get score of similarity between lists of words?

与世无争的帅哥 提交于 2020-01-06 08:03:01

问题


I want to calculate the similarity between lists of words, for example :

import math,re
from collections import Counter

test = ['address','ip']
list_a = ['identifiant', 'ip', 'address', 'fixe', 'horadatee', 'cookie', 'mac', 'machine', 'network', 'cable']
list_b = ['address','city']

def counter_cosine_similarity(c1, c2):
    terms = set(c1).union(c2)
    print(c2.get('ip',0)**2)
    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)

counter1 = Counter(test)
counter2 = Counter(list_a)
counter3 = Counter(list_b)
score = counter_cosine_similarity(counter1,counter2)
print(score) # output : 0.4472135954999579
score = counter_cosine_similarity(counter1,counter3)
print(score) # output : 0.4999999999999999

For me it's not exactly the score I want to get, the score must be the opposite because list_a contains address and ip so it's a 100% test match I know that cosine similarity does the comparison in this case with test and list_a so since there is some element on the list_a which is not in test it is for that the score is low, so that I will do exactly it is compared that test compared to list_a in one way not in the two way.

Desired output

score = counter_cosine_similarity(counter1,counter2)
print(score) # output : score higher than list_b = 1.0 may be
score = counter_cosine_similarity(counter1,counter3)
print(score) # output : score less the list_a = 0.5 may be

回答1:


If you want a higher value the more terms are the same, use this code:

 score = len(set(test).intersection(set(list_x)))

That will tell you how many common terms the two lists have. If you want to score repetitions higher, then try

 commonTerms = set(test).intersection(set(list_x))
 counter = Counter(list_x)
 score = sum((counter.get(term) for term in commonTerms)) #edited

If you need scaling the score to [0..1], I need to know more about your data sets.



来源:https://stackoverflow.com/questions/55398968/is-there-any-solution-to-get-score-of-similarity-between-lists-of-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!