The first string to compare.
The second string to compare.
Optional partial match engine configuration.
Similarity score between 0 and 100.
export function calculateSimilarity(
str1: string,
str2: string,
config: Partial<MatchEngineConfig> = {},
): number {
const { caseSensitive } = { ...DEFAULT_MATCH_CONFIG, ...config };
if (!str1 || !str2) return 0;
if (str1 === str2) return 100;
const s1 = normalizeString(str1, caseSensitive);
const s2 = normalizeString(str2, caseSensitive);
if (s1 === s2) return 100;
if (s1.length < 2 || s2.length < 2) return 0;
// Exact substring check (one is contained within the other)
if (s1.includes(s2) || s2.includes(s1)) {
const longerLength = Math.max(s1.length, s2.length);
const shorterLength = Math.min(s1.length, s2.length);
return Math.round((shorterLength / longerLength) * 100);
}
// String similarity calculation using Dice coefficient
const similarity = stringSimilarity.compareTwoStrings(s1, s2);
return Math.round(similarity * 100);
}
Calculates string similarity using multiple algorithms for better accuracy. Returns a score between 0-100.