Every dayEveryday Use

Text Diff
checker.

Paste two versions of a text and see each word or line that was added or removed.

How it's worked out

The tool looks for the longest common subsequence: the largest set of lines, words or characters that appear in both texts in the same order. Hunt and McIlroy set out the idea in 1976 for the Unix diff program, and showed that keeping the most lines unchanged is the same goal as making the fewest edits.

The tool fills in a table of matches for every pair of pieces. P is the length of the best match so far, for the first i pieces of one text and the first j of the other.

Pi,j\displaystyle P_{i,j}=1+Pi−1,j−1 on a match\displaystyle {}= 1 + P_{i-1,j-1} \text{ on a match}
Pi,j\displaystyle P_{i,j}=max⁡(Pi−1,j,Pi,j−1) if not\displaystyle {}= \max(P_{i-1,j},\, P_{i,j-1}) \text{ if not}

Whatever falls outside that common thread is marked as removed from the original or added in the new version. The score is the same ratio that Python uses in its comparison library, with M as the pieces in common and T as the pieces in both texts together. Identical texts score 1, and texts with nothing in common score 0.

similarity\displaystyle \text{similarity}=2MT\displaystyle {}= \frac{2M}{T}

The level you choose decides what a piece is. A line must match in full, a word is any run without a space, and a character is one letter or digit. Spaces count as pieces in word mode, so the score moves a little when spacing changes. Ignore case and Ignore spacing work like the -i and -b options of diff.

Worked examples

A shopping list that changed

Apples, bananas and cherries became apples, blueberries, cherries and dates. By lines, that is 1 removed, 2 added and 2 unchanged, for a score of 57%: twice 2 matches over 7 lines.

Read as two lists, 2 items are in both, bananas is only in the first, and blueberries and dates are only in the second.

Open this example in the tool

A meeting note with a new day and time

Compared by words, only Tuesday and 3 are marked as removed, with Thursday and 4 in their place. That is 2 words out and 2 in.

The level changes the score a lot. By lines this pair scores 0%, because the one line differs. By words it scores 87%, and by characters 93%, since Tuesday and Thursday share most of their letters.

Open this example in the tool

Code that was only re-spaced, plus one new line

With spacing counted, 2 lines are marked as removed and 3 as added, which hides the real change. With Ignore spacing on, 0 lines are removed and only 1 is added: the new call to save on line 3.

Open this example in the tool

Reading your result

A high score does not mean a small change. In a contract, one added "not" can reverse a clause and still leave the page at 99%. Use the score to see how much was rewritten, and read the marked text to judge what it means. Pay special attention to small words, dates and figures.

For a long document, start by lines to see which paragraphs changed at all. Then move to words to read the edits inside them. Turn on Ignore spacing first, because reflowed text can mark nearly every line.

A moved paragraph shows up as removed in one place and added in another, since a common subsequence only keeps pieces in the same order. If you need moves marked, git can color them with its --color-moved option. When order does not matter at all, such as two lists of names, read the list comparison instead, which ignores order and repeats.

Formatting is not part of plain text. Bold, fonts and tracked changes in a Word file do not survive a paste, so only the wording is compared.

Common mistakes

  • Comparing in the wrong direction. Removed and added depend on which text goes first. Swap them if the marks look reversed.
  • Blaming line endings. Files from Windows and Unix end lines differently. Line mode treats both alike, so that pair scores 100%. Word and character modes count those endings as characters, so use lines there.
  • Reading punctuation as a word change. Word mode treats "day." and "day" as different words. Switch to characters to see that only the period moved.
  • Expecting a very long text to work by characters. Very long pairs are refused at the finer levels to keep the page fast. Compare by lines, or split the text into sections.

Questions people ask

Turn on Ignore spacing. In line mode, runs of spaces then count as one and space at the ends of a line is dropped. Git and diff offer the same idea through their -b option.

Sources