From 0e8e10ad59e65116f805a36e472de903addef09d Mon Sep 17 00:00:00 2001 From: itsUrcute Date: Fri, 1 Oct 2021 00:17:33 -0500 Subject: [PATCH 1/5] feat: add dice coefficient --- String/DiceCoefficient.js | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 String/DiceCoefficient.js diff --git a/String/DiceCoefficient.js b/String/DiceCoefficient.js new file mode 100644 index 0000000000..8d2d04d62b --- /dev/null +++ b/String/DiceCoefficient.js @@ -0,0 +1,51 @@ +/* The Sørensen–Dice 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. + */ + +// 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 +} + +module.exports = diceCoefficient; From a8140fa72294f23d24c2521cc2dc7d5ac2b92d53 Mon Sep 17 00:00:00 2001 From: itsUrcute Date: Fri, 1 Oct 2021 00:22:03 -0500 Subject: [PATCH 2/5] chore: link to wikipedia article --- String/DiceCoefficient.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/String/DiceCoefficient.js b/String/DiceCoefficient.js index 8d2d04d62b..44ea4a5cc6 100644 --- a/String/DiceCoefficient.js +++ b/String/DiceCoefficient.js @@ -2,12 +2,13 @@ * 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) { +function mapBigrams(string) { const bigrams = new Map() for (let i = 0; i < string.length - 1; i++) { const bigram = string.substring(i, i + 2) @@ -19,7 +20,7 @@ function mapBigrams (string) { // Calculate the number of common bigrams between a map of bigrams and a string -function countCommonBigrams (bigrams, string) { +function countCommonBigrams(bigrams, string) { let count = 0 for (let i = 0; i < string.length - 1; i++) { const bigram = string.substring(i, i + 2) @@ -29,7 +30,7 @@ function countCommonBigrams (bigrams, string) { } // Calculate Dice coeff of 2 strings -function diceCoefficient (stringA, stringB) { +function diceCoefficient(stringA, stringB) { if (stringA === stringB) return 1 else if (stringA.length < 2 || stringB.length < 2) return 0 @@ -48,4 +49,4 @@ function diceCoefficient (stringA, stringB) { return dice } -module.exports = diceCoefficient; +module.exports = diceCoefficient From 6fb835b23c3636d8ac2fef68e8547e5cbe214847 Mon Sep 17 00:00:00 2001 From: itsUrcute Date: Fri, 1 Oct 2021 00:48:32 -0500 Subject: [PATCH 3/5] chore: convert to esm --- String/DiceCoefficient.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/String/DiceCoefficient.js b/String/DiceCoefficient.js index 44ea4a5cc6..d6cd70a75b 100644 --- a/String/DiceCoefficient.js +++ b/String/DiceCoefficient.js @@ -48,5 +48,4 @@ function diceCoefficient(stringA, stringB) { return dice } - -module.exports = diceCoefficient +export { diceCoefficient } From 1a5ce8a29ffbb31b7743fe4295275749d0b809a0 Mon Sep 17 00:00:00 2001 From: itsUrcute Date: Fri, 1 Oct 2021 00:48:56 -0500 Subject: [PATCH 4/5] refactor: add tests --- String/test/DiceCoefficient.test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 String/test/DiceCoefficient.test.js diff --git a/String/test/DiceCoefficient.test.js b/String/test/DiceCoefficient.test.js new file mode 100644 index 0000000000..f61f5848ba --- /dev/null +++ b/String/test/DiceCoefficient.test.js @@ -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) + }) +}) From f005f9acc729fdeb6009165f7e42b5f6888dc1be Mon Sep 17 00:00:00 2001 From: itsUrcute Date: Fri, 1 Oct 2021 01:22:53 -0500 Subject: [PATCH 5/5] chore: formatting --- String/DiceCoefficient.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/String/DiceCoefficient.js b/String/DiceCoefficient.js index d6cd70a75b..b2f3a34be9 100644 --- a/String/DiceCoefficient.js +++ b/String/DiceCoefficient.js @@ -8,7 +8,7 @@ // 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) { +function mapBigrams (string) { const bigrams = new Map() for (let i = 0; i < string.length - 1; i++) { const bigram = string.substring(i, i + 2) @@ -20,7 +20,7 @@ function mapBigrams(string) { // Calculate the number of common bigrams between a map of bigrams and a string -function countCommonBigrams(bigrams, string) { +function countCommonBigrams (bigrams, string) { let count = 0 for (let i = 0; i < string.length - 1; i++) { const bigram = string.substring(i, i + 2) @@ -30,7 +30,7 @@ function countCommonBigrams(bigrams, string) { } // Calculate Dice coeff of 2 strings -function diceCoefficient(stringA, stringB) { +function diceCoefficient (stringA, stringB) { if (stringA === stringB) return 1 else if (stringA.length < 2 || stringB.length < 2) return 0