-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create string utils with methods for normalizing, computing similarit…
…y score, re #10780
- Loading branch information
1 parent
dab8c82
commit 63c0d30
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
define(['jquery', 'knockout', 'arches'], function($, ko, arches) { | ||
const stringUtils = { | ||
compareTwoStrings: function(str1, str2) { | ||
// uses dice coefficient for string similarity score | ||
if (str1.length < 2 || str2.length < 2) return 0; | ||
let set1 = new Set(); | ||
let set2 = new Set(); | ||
for (let i = 0; i < str1.length - 1; i++) { | ||
const bigram = str1.substr(i, 2); | ||
set1.add(bigram); | ||
} | ||
for (let i = 0; i < str2.length - 1; i++) { | ||
const bigram = str2.substr(i, 2); | ||
set2.add(bigram); | ||
} | ||
const intersection = new Set([...set1].filter(x => set2.has(x))); | ||
return (2 * intersection.size) / (set1.size + set2.size); | ||
}, | ||
normalizeText: function(text) { return text.toLowerCase().replace(/\W+/g, ''); }, | ||
}; | ||
return stringUtils; | ||
}); |