Recursive AI

Detect numeric changes in documents in C#

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

Separate changed dates, amounts, and durations from ordinary text edits. This example takes plain text; Office files are refused at this release. For PDF input, use the PDF adapter: add RecursiveAI.OneDiff.Pdf and call OneDiff.Pdf.PdfRedline.Register(); once at startup.

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 old = CompareDocument.FromText(
    "Pay within 30 days.");
var next = CompareDocument.FromText(
    "Pay within 60 days.");
var result = Comparison.Of(
    old, next, CompareSettings.Legal);
foreach (var change in result.Changes)
    Console.WriteLine($"{change.Type}: " +
        $"{change.OldValue?.Canonical} -> " +
        $"{change.NewValue?.Canonical}");
Text
Duration: 30 d -> 60 d

Unzip the example, then dotnet run.

The full program

A canonical value is the standard representation used for a recognized value, such as USD 1200 for an amount. Equivalent canonical values still produce substantive changes in this example.

C#
using OneDiff.Documents;
using OneDiff.Documents.Model;
 
var old = CompareDocument.FromText(
    "Fee: USD 1,200.\n\nDue: 2025-03-14.\n\nPay within 30 days.");
var next = CompareDocument.FromText(
    "Fee: USD 1200.\n\nDue: 14 March 2025.\n\nPay within 60 days.");
var result = Comparison.Of(
    old, next, CompareSettings.Legal);
foreach (var change in result.Changes)
    Console.WriteLine($"{change.Type}: " +
        $"{change.OldValue?.Canonical} -> " +
        $"{change.NewValue?.Canonical}; " +
        $"substantive={change.IsSubstantive}");

dotnet run prints:

Text
Amount: USD 1200 -> USD 1200; substantive=True
Date: 2025-03-14 -> 2025-03-14; substantive=True
Duration: 30 d -> 60 d; substantive=True

The amount’s text changed from USD 1,200 to USD 1200, but its value did not. substantive=True is the library’s change flag, not a statement that the canonical values differ. Compare the canonical values separately, after checking that both were resolved:

C#
using OneDiff.Documents;
using OneDiff.Documents.Model;
 
var old = CompareDocument.FromText(
    "Fee: USD 1,200.");
var next = CompareDocument.FromText(
    "Fee: USD 1200.");
var result = Comparison.Of(
    old, next, CompareSettings.Legal);
foreach (var change in result.Changes)
{
    bool sameValue =
        change.OldValue is { } oldValue &&
        change.NewValue is { } newValue &&
        oldValue.Canonical == newValue.Canonical;
    Console.WriteLine("Text changed: " +
        (change.Old.Text != change.New.Text));
    Console.WriteLine($"Same value: {sameValue}");
}
Text
Text changed: True
Same value: True

Variations

Use Strict settings without typed value labels. This is a complete replacement program.

C#
using OneDiff.Documents;
using OneDiff.Documents.Model;
 
var old = CompareDocument.FromText(
    "Rate: 6%.");
var next = CompareDocument.FromText(
    "Rate: 8%.");
var result = Comparison.Of(
    old, next, CompareSettings.Strict);
foreach (var change in result.Changes)
    Console.WriteLine($"{change.Type}; " +
        $"substantive={change.IsSubstantive}");
Text
None; substantive=True

None means Strict settings did not assign a value type. The percentage still changed from 6% to 8%; substantive=True means the edit is not treated as merely formatting or typography.

The short answer

Compare two text documents with CompareSettings.Legal, then inspect each change’s Type, OldValue, NewValue, and IsSubstantive. Typed values distinguish a duration from a date or amount; canonical values let you see when different spellings resolve to the same value. They do not determine legal significance.

How do you detect numeric changes in documents in C#?

The full program uses three paragraphs so each comparison has a clear unit: a fee, a due date, and a payment period. Both fee spellings resolve to USD 1200. Both dates resolve to 2025-03-14. The duration changes from 30 d to 60 d. These are the actual returned canonical values, not strings normalized by the example.

The classification rules require a change side, as a whole, to be one recognized value. When both sides are present, they must be the same kind. Recognized kinds include amount, date, duration, percentage, number, and party. If a change combines several values with surrounding prose, it need not receive a value type. A missing type therefore does not mean that no number changed. Keep the original change text available alongside the resolved fields.

Ambiguous values need separate treatment. A slash date such as 3/4/2025 can be recognized as a date without being resolved; the value on that side stays null. Don’t replace null with a guessed date. Numeric separators also follow explicit rules: a single separator with exactly three digits after it may be a thousands separator, whereas other single-separator cases use a decimal mark. Applications that accept varied number formats should read those rules before treating canonical values as business data.

The variation switches to CompareSettings.Strict. It leaves typed changes off, so a percentage replacement is reported with type None. Strict is appropriate when you want the edits without semantic labels. The Legal preset is a settings choice, not a claim that the result has been reviewed by a lawyer.

How do you compare two documents for changes?

Start with the source text and preserve paragraph boundaries when constructing each CompareDocument. Comparison.Of runs alignment, in-pair diff, grouping, classification, and completeness checks. This example uses text in memory; it does not open an Office file. At this release, Office document inputs are refused. A separate PDF adapter is available for PDF inputs.

Most importantly, a canonical-value match is not an instruction to hide the edit. All three changes in the full program have IsSubstantive=True, including the two equivalent-value replacements. The documented substantive rules exclude formatting, typography and several structural cases; equal canonical values alone are not an exclusion. Use the fields as separate facts: the text changed, the values may be equivalent, and the library’s substantive flag follows its own rules. A review policy can consider those facts, but should not mistake any one of them for a judgment about contractual effect.

All onediff recipes