@technobuddha/library
Version:
A large library of useful functions
26 lines (25 loc) • 1.19 kB
JavaScript
import levenshteinDistance from '../levenshteinDistance';
import diceCoefficient from '../diceCoefficient';
import longestCommonSubstring from '../longestCommonSubstring';
export function fuzzyMatch(input, comparedTo, { caseInsensitive = true, weightLevenshteinDistance = 5, weightDiceCoefficient = 3, weightLongestCommonSubstring = 2, } = {}) {
const len = Math.max(input.length, comparedTo.length);
let wgt = 0;
let sum = 0;
if (len) {
if (weightLevenshteinDistance) {
sum += weightLevenshteinDistance *
(1.0 - levenshteinDistance(input, comparedTo, { caseInsensitive }) / len);
wgt += weightLevenshteinDistance;
}
if (weightDiceCoefficient) {
sum += weightDiceCoefficient * (diceCoefficient(input, comparedTo, { caseInsensitive }));
wgt += weightDiceCoefficient;
}
if (weightLongestCommonSubstring) {
sum += weightLongestCommonSubstring * (longestCommonSubstring(input, comparedTo, { caseInsensitive }).length / len);
wgt += weightLongestCommonSubstring;
}
}
return wgt === 0 ? 0 : sum / wgt;
}
export default fuzzyMatch;