Edit Distance (Levenshtein)
Type “recieve” into a search box and you still get results about receiving things. Something had to decide that your typo was one small slip away from a real word. Edit distance is that measurement: the smallest number of single-character insertions, deletions or substitutions that turns one string into another.
How it is computed
The Levenshtein form charges one for each operation, so “kitten” to “sitting” costs three. Trying every possible edit sequence is hopeless, so it is computed by dynamic programming: fill a grid whose cell at row i, column j holds the cheapest alignment of the first i and first j characters, then read the answer from the far corner. Common variants:
- Damerau-Levenshtein adds transposition, since swapping two adjacent letters is one of the commonest human typos.
- Weighted versions charge less for edits between neighbouring keyboard keys, or between letters that sound alike.
- Dividing by the length of the longer string yields a similarity ratio, which is what fuzzy-matching libraries report.
Where it shows up
Spell correction and query suggestion, record linkage when two databases spell a customer's name differently, and evaluation metrics — word error rate is edit distance computed over words instead of characters, and translation edit rate works the same way. Python's difflib and the RapidFuzz library cover the practical cases.
Why it matters
It hands the field a cheap, exact notion of “nearly the same text” that needs no training data at all. Where an embedding judges meaning, edit distance judges surface form: the right tool for a misspelling, the wrong one when “car” has to match “automobile”.
Edit distance measures how different two strings are by counting the fewest single-character edits needed to transform one into the other; the Levenshtein form allows insertion, deletion and substitution at equal cost and is computed by dynamic programming. It underpins spell correction, fuzzy name matching and record linkage, and — applied to words rather than characters — the word error rate used to score transcription.
If you wrote “freind” instead of “friend”, how wrong were you? Not very: swap two letters and it is fixed. Compare that with “fringe”, which needs more work. Counting those small corrections is the whole idea.
Edit distance is the number of one-letter changes — adding a letter, removing one, or replacing one — needed to turn one word into another. Software uses it to guess what you meant to type, to match “Jon Smyth” in one list with “John Smith” in another, and to score how badly a voice assistant misheard a sentence. It compares spellings, not meanings, so “car” and “automobile” look very far apart.