Recursive AI

Manually align paragraphs before comparing documents in C#

OneDiff is a free, open-source diff library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Stages.Align pairs paragraphs automatically, including moved paragraphs and splits and merges; use Pin when you know which two paragraphs belong together and the automatic pass didn’t pair them.

Terminal
dotnet add package RecursiveAI.OneDiff.Documents

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneDiff.Documents;
using OneDiff.Documents.Model;
 
var oldDoc = CompareDocument.FromText("Red.");
var newDoc = CompareDocument.FromText("Blue.");
var pairs = Stages.Align(oldDoc, newDoc);
// node 1 = first paragraph (node 0 is the section)
var edits = Stages.DiffPairs(pairs.Pin(1, 1));
var changes = Stages.Group(edits);
Console.WriteLine(changes.Changes[0].Kind);

Unzip the example, then dotnet run.

Prints:

Text
Replace

The full program

C#
using OneDiff.Documents;
using OneDiff.Documents.Model;
 
var oldDoc = CompareDocument.FromText(
    "Red.\n\nEnd of notice.");
var newDoc = CompareDocument.FromText(
    "Blue.\n\nEnd of notice.");
var automatic = Stages.Align(oldDoc, newDoc);
var pinned = automatic.Pin(1, 1);
Console.WriteLine($"Automatic: {automatic.LinkOfOld(1)?.Kind.ToString() ?? "unpaired"}");
Console.WriteLine($"Pinned: {oldDoc[1].Text} -> {newDoc[1].Text}");
 
var edits = Stages.DiffPairs(pinned);
var changes = Stages.Group(edits);
foreach (var change in changes.Changes)
    Console.WriteLine($"{change.Kind}: {change.Old.Text} -> {change.New.Text}");

dotnet run prints:

Text
Automatic: unpaired
Pinned: Red. -> Blue.
Replace: Red -> Blue

In these plain-text documents, node 0 is the section and node 1 is the first paragraph. The automatic pass leaves “Red.” unpaired. The pin makes “Red.” and “Blue.” one pair, so the diff reports a replacement. Pin throws ArgumentException if an ID doesn’t exist or the nodes are of different kinds.

Variations

Remove the pin and try automatic alignment again. In the full program, add this after creating pinned:

C#
var link = pinned.LinkOfOld(1)!;
var seed = pinned.Unpin(link.Id);
var retried = Stages.Align(
    oldDoc, newDoc, seed: seed);
Console.WriteLine(
    retried.LinkOfOld(1) is null);
Text
True

The short answer

Run Stages.Align on two CompareDocument values, then call Pin(oldNodeId, newNodeId) on the returned alignment. Pass the result, not the original alignment, to Stages.DiffPairs. Pinning tells OneDiff which blocks to compare. It doesn’t make their text equal or suppress the edits inside the pair.

How do you compare two text paragraphs after matching them by hand?

Use node IDs from the corresponding document, not paragraph positions on the page. CompareDocument.FromText puts a section at the root and separates paragraphs at blank lines. If you build the documents yourself, CompareDocumentBuilder.Add returns the ID of the node it added. Keep those IDs if, for example, your app already associates paragraphs by a stable key and a reviewer needs to correct one pair.

Pin returns a new alignment. It removes any link that used either selected node; the other nodes in those removed links become unpaired. If you want to give those nodes another chance to match, pass the corrected alignment as seed: to Stages.Align on the same documents. The alignment pass keeps pinned links and aligns the rest afresh. An out-of-order pinned pair can be reported as a move rather than an in-order match.

How do you highlight differences between two documents after correcting a match?

Keep the corrected alignment through the rest of the pipeline. Stages.DiffPairs finds edits inside the selected pairs; Stages.Group turns those edits into a change list. To write an HTML redline, a copy with edits marked, finish the comparison and save it. Add this after the full program’s changes line:

C#
var result = Stages.Verify(
    Stages.Classify(changes));
result.SaveAs("redline.html");

Running this writes redline.html beside the project. Starting a fresh comparison instead would run alignment again without your correction.

All onediff recipes