Fancy DSA Fancy DSA
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)

Jonsam NG

想的更多,也要想的更远
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)
  • 开始上手
  • Plan 计划
  • Roadmap 路线
  • 算法简介
  • Sort 排序

  • Search 搜索

  • Recursive 递归

  • Graph 图

  • Tree 树

  • Math 数学

  • Hash 哈希

  • String 字符串

    • AlphaNumericPalindrome [回文串]
    • AlternativeStringArrange [交替合并字符串]
    • BoyerMoore [博耶-穆尔字符串搜索算法、BM 算法]
    • CheckAnagram [易位构词]
    • NamingConvention [命名规则]
    • CheckExceeding [Exceeding words]
    • CheckPangram [全字母句]
    • CheckWordOccurrence [单词计数]
    • CountVowels [元音字母计数]
    • CreatePermutations [全排列]
    • DiceCoefficient [Dice系数]
      • 介绍
      • 实现
      • 扩展
      • 参考
    • FormatPhoneNumber [格式化电话号码]
    • GenerateGUID [生成GUID、UUID]
    • HammingDistance [汉明距离]
    • KMPPatternSearching [KMP字符串匹配]
  • BitManipulation 位操纵

  • Backtracking 回溯

  • DynamicProgramming 动态规划

  • Cache 缓存

  • Array 数组

  • Ciphers 密码学

  • Conversions 转换

  • ProjectEuler 欧拉计划

  • 其他

  • 算法
  • String 字符串
jonsam
2022-09-26
目录

DiceCoefficient [Dice系数]

# 介绍

骰子系数(Dice coefficient),也称索伦森 - 骰子系数(Sørensen–Dice coefficient),是一种集合相似度度量函数,通常用于计算两个样本的相似度(也可以度量字符串的相似性)。

Dice 系数可以计算两个字符串的相似度: Dice(s1,s2)=2*comm(s1,s2)/(leng(s1)+leng(s2)) 。其中, comm (s1,s2) 是 s1、s2 中相同字符的个数 leng(s1),leng(s2) 是字符串 s1、s2 的长度。

# 实现

# JavaScript

Dice 系数是通过比较两个字符串的 bigrams 来计算的, 一个 bigrams 是长度为 2 的字符串的一个子串。

/* The Sørensen–Dice coefficient(系数) is a statistic used to (估计) the similarity of two samples.
 * Applied to strings, it can give you a value between 0 and 1 (included) which tells you how similar they are.
 * Dice coefficient is calculated by comparing the bigrams of both strings,
 * a bigram is a substring of the string of length 2.
 * read more: https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient
 */

// Time complexity: O(m + n), m and n being the sizes of string A and string B

// Find the bistrings of a string and return a hashmap (key => bistring, value => count)
function mapBigrams(string) {
 const bigrams = new Map();
 for (let i = 0; i < string.length - 1; i++) {
  const bigram = string.substring(i, i + 2);
  const count = bigrams.get(bigram);
  bigrams.set(bigram, (count || 0) + 1);
 }
 return bigrams;
}

// Calculate the number of common bigrams between a map of bigrams and a string

function countCommonBigrams(bigrams, string) {
 let count = 0;
 for (let i = 0; i < string.length - 1; i++) {
  const bigram = string.substring(i, i + 2);
  if (bigrams.has(bigram)) count++;
 }
 return count;
}

// Calculate Dice coeff of 2 strings
function diceCoefficient(stringA, stringB) {
 if (stringA === stringB) return 1;
 else if (stringA.length < 2 || stringB.length < 2) return 0;

 const bigramsA = mapBigrams(stringA);

 const lengthA = stringA.length - 1;
 const lengthB = stringB.length - 1;

 let dice = (2 * countCommonBigrams(bigramsA, stringB)) / (lengthA + lengthB);

 // cut 0.xxxxxx to 0.xx for simplicity
 dice = Math.floor(dice * 100) / 100;

 return dice;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  • substring() (opens new window) 方法返回一个字符串在开始索引到结束索引之间的一个子集,或从开始索引直到字符串的末尾的一个子集。

# 扩展

  • 医学影像分割 ---Dice Loss - 知乎 (opens new window)

# 参考

  • Sørensen–Dice coefficient - Wikiwand (opens new window)
  • Dice 系数 - Wikiwand (opens new window)
编辑 (opens new window)
上次更新: 2022/10/20, 20:45:42
CreatePermutations [全排列]
FormatPhoneNumber [格式化电话号码]

← CreatePermutations [全排列] FormatPhoneNumber [格式化电话号码]→

最近更新
01
0-20题解
10-31
02
本章导读
10-31
03
算法与转换:Part1
10-28
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Fancy DSA | Made by Jonsam by ❤
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式