Finding what you mean, not just what you type.
Imagine searching for "recieve" instead of "receive" or "calender" instead of "calendar". A traditional search would likely return no results because it looks for an exact match. This is where fuzzy search comes in. It's a technique that allows for finding matches that are approximately correct, even if there are misspellings, typos, or variations in the input.
In the real world, user input is rarely perfect. People make mistakes, use abbreviations, or have different ways of spelling words. Exact matching systems can be frustrating and lead to poor user experiences. Fuzzy search addresses these challenges by:
One of the most common algorithms used in fuzzy search is the Levenshtein Distance, also known as Edit Distance. It measures the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one word into another. A smaller Levenshtein distance indicates a higher similarity between two strings.
Enter two words to calculate their Levenshtein distance and visualize the matrix.
The matrix visualization helps understand how the distance is calculated. Each cell dp[i][j]
represents the Levenshtein distance between the first i
characters of word1 and the first j
characters of word2.
function levenshteinDistance(s1, s2) {
const m = s1.length;
const n = s2.length;
const dp = Array(m + 1).fill(0).map(() => Array(n + 1).fill(0));
for (let i = 0; i <= m; i++) {
dp[i][0] = i;
}
for (let j = 0; j <= n; j++) {
dp[0][j] = j;
}
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
const cost = (s1[i - 1] === s2[j - 1]) ? 0 : 1;
dp[i][j] = Math.min(
dp[i - 1][j] + 1, // Deletion
dp[i][j - 1] + 1, // Insertion
dp[i - 1][j - 1] + cost // Substitution
);
}
}
return dp[m][n];
}
The final distance is found in the bottom-right cell of the matrix.
While Levenshtein distance is popular, other algorithms are used for different scenarios:
See fuzzy search in action with a simple dictionary. Type a word, even if misspelled, and see potential matches.
Matches will appear here...
The general flow of a fuzzy search system involves taking user input, comparing it against a dataset using a fuzzy matching algorithm, and then presenting the most relevant results.