chore: merge "Dice" (#703)

* feat: add dice coefficient

* chore: link to wikipedia article

* chore: convert to esm

* refactor: add tests

* chore: formatting
This commit is contained in:
Urcute
2021-10-01 03:27:07 -05:00
committed by GitHub
parent ab65e2ac76
commit 8c2f2ca15a
2 changed files with 72 additions and 0 deletions

51
String/DiceCoefficient.js Normal file
View File

@ -0,0 +1,51 @@
/* The SørensenDice coefficient is a statistic used to gauge 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 stings,
* 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
console.log('Dice coefficient of', stringA, 'and', stringB, 'is', dice)
return dice
}
export { diceCoefficient }

View File

@ -0,0 +1,21 @@
import { diceCoefficient } from '../DiceCoefficient'
describe('diceCoefficient', () => {
it('should calculate edit distance between two strings', () => {
// equal strings return 1 (max possible value)
expect(diceCoefficient('abc', 'abc')).toBe(1)
expect(diceCoefficient('', '')).toBe(1)
// string length needs to be atleast 2 (unless equal)
expect(diceCoefficient('a', '')).toBe(0)
expect(diceCoefficient('', 'a')).toBe(0)
expect(diceCoefficient('skate', 'ate')).toBe(0.66)
expect(diceCoefficient('money', 'honey')).toBe(0.75)
expect(diceCoefficient('love', 'hate')).toBe(0)
expect(diceCoefficient('skilled', 'killed')).toBe(0.9)
})
})