Text tools

Levenshtein distance

Levenshtein distance between a file and a second text you paste in. Runs entirely on your device.

Runs on your device. The file is never uploaded.

Levenshtein distance counts the single-character insertions, deletions and substitutions between the file you drop and the compareTo text, printing that number beside both lengths. The work is the classic dynamic programming table, one row held at a time. A pairing needing over 25 million cells is turned away before any of it starts.

Input

Options

Questions

What does the number mean?

The smallest number of single-character edits that turn one text into the other, where an edit is an insertion, a deletion or a substitution, each costing 1. The output line also repeats the two lengths, so you can judge the distance against the size of the inputs. A distance of 0 means the two are identical character for character.

How do I give it the second text?

The file you drop is the first side, and the compareTo option is the second. This tool takes exactly one file, and you paste or type the text to compare it against into that field. That makes it well suited to checking one file against a known-good string, and less suited to comparing two large files, since one of them has to be typed in.

Why was my comparison refused as too large?

Because the matrix would have been enormous. The implementation is the classic dynamic programming algorithm, so the work is the product of the two lengths, and anything over 25 million cells is refused up front with "inputs too large for this Levenshtein implementation", the cell count, and a note to trim one side. Roughly, two inputs of 5000 characters each are at the limit.

How is this different from Hamming distance?

Levenshtein allows insertions and deletions, so it can compare texts of different lengths and will tell you that adding one character at the start costs 1. Hamming compares position by position only and cannot express a shift, so the same one-character insertion makes almost every later position differ. Use Levenshtein for fuzzy text matching, and Hamming for fixed-width codes and hashes.

Does it compare characters or bytes?

JavaScript string units, which is UTF-16. That means a character outside the Basic Multilingual Plane, an emoji for example, counts as two positions and can contribute 2 to the distance. For ordinary text the count matches what you would call characters. Case matters, whitespace matters, and there is no normalisation step before the comparison.

Related Text tools