Dice系数计算

  Dice距离用于度量两个集合的相似性,因为可以把字符串理解为一种集合,因此Dice距离也会用于度量字符串的相似性。此外,Dice系数的一个非常著名的使用即实验性能评测的F1值。Dice系数定义如下:

  Dice 系数可以计算两个字符串的相似度:
$Dice(s1,s2)=frac{2*comm(s1,22)}{leng(s1)+leng(s2)}$
  其中,comm (s1,s2)是s1、s2 中相同字符的个数leng(s1),leng(s2)是字符串s1、s2 的长度。
 
Python代码实现:
def dice_coefficient(a, b):
    '''dice coefficient '''
    a_bigrams = set(a)
    b_bigrams = set(b)
    overlap = len(a_bigrams & b_bigrams)
    return overlap * 2.0 / (len(a_bigrams) + len(b_bigrams))
原文地址:https://www.cnblogs.com/liuxiaochong/p/14542641.html