fuzzy-search-lib
Version:
A flexible fuzzy search library supporting both MongoDB and PostgreSQL
45 lines • 1.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.diceCoefficient = void 0;
/**
* Generates bigrams (pairs of consecutive characters) from a string.
*
* @param str - Input string
* @returns Array of bigrams
*/
function getBigrams(str) {
const bigrams = [];
for (let i = 0; i < str.length - 1; i++) {
bigrams.push(str.substring(i, i + 2));
}
return bigrams;
}
/**
* Calculates the Sørensen-Dice coefficient between two strings.
*
* @param s1 - First string
* @param s2 - Second string
* @returns Sørensen-Dice coefficient between 0 and 1
*/
function diceCoefficient(s1, s2) {
// Handle edge cases
if (s1 === s2)
return 1;
if (s1.length === 0 || s2.length === 0)
return 0;
if (s1.length === 1 && s2.length === 1)
return s1 === s2 ? 1 : 0;
if (s1.length < 2 || s2.length < 2)
return 0;
const bigrams1 = getBigrams(s1.toLowerCase());
const bigrams2 = getBigrams(s2.toLowerCase());
const set1 = new Set(bigrams1);
const set2 = new Set(bigrams2);
const intersection = new Set([...set1].filter(x => set2.has(x)));
const totalBigrams = set1.size + set2.size;
if (totalBigrams === 0)
return 1;
return (2 * intersection.size) / totalBigrams;
}
exports.diceCoefficient = diceCoefficient;
//# sourceMappingURL=dice-coefficient.js.map