The semantic tree diff algorithm is currently experimental and under active development. While functional and tested, the API and behavior may change in future releases. Use with caution in production environments.

1. Purpose

The Semantic algorithm is Canon’s intelligent, experimental algorithm for document comparison. It provides signature-based matching with operation detection (INSERT, DELETE, UPDATE, MOVE).

This page explains when to use the Semantic algorithm, how it differs from DOM, and how to configure it effectively.

2. When to Use

The Semantic algorithm is for advanced use cases where intelligence is worth the performance cost.

2.1. Use Semantic Algorithm When

  • ✓ You need to detect element moves and reordering

  • ✓ Documents have significant restructuring

  • ✓ You need operation-level analysis (INSERT, DELETE, UPDATE, MOVE)

  • ✓ You want statistical analysis of changes

  • ✓ You’re analyzing document evolution

  • ✓ You’re willing to accept experimental status

  • ✓ Working with smaller documents (< 10KB)

2.2. Characteristics

Feature Semantic Algorithm

Status

Experimental, under development

Performance

Slower - O(n²) worst case

Memory Usage

Higher - builds tree structures

Matching Strategy

Signature-based similarity matching

Move Detection

Yes - detects MOVE operations

Output Format

Operation-based (INSERT, DELETE, UPDATE, MOVE)

Best For

Restructured documents, operation analysis

Document Size

Best for smaller documents (< 10KB)

3. How It Works

The Semantic algorithm uses a sophisticated three-phase matching process:

3.1. Phase 1: Hash-Based Exact Matching

Matches nodes with identical structure and content:

  • Fast - O(n) performance

  • Eliminates unchanged subtrees

  • Reduces problem size for later phases

3.2. Phase 2: Similarity-Based Matching

Matches similar but not identical nodes:

  • Compares node names, attributes, text, structure

  • Scores similarity using weighted metrics

  • Threshold - Default 0.95 (95% similar)

3.3. Phase 3: Structural Propagation

Improves match quality using context:

  • Top-down - Propagate from matched parents

  • Bottom-up - Propagate from matched children

  • Resolves ambiguous matches

3.4. Signature-Based Matching

Unlike DOM’s position-based comparison, Semantic uses signatures:

Example 1. Signature-based comparison example
<!-- Document 1 -->
<book>
  <title>Canon Guide</title>
  <author>Alice</author>
</book>

<!-- Document 2 -->
<book>
  <author>Alice</author>
  <title>Canon Guide</title>
</book>

Semantic algorithm: 1. Calculates signature for each element 2. <author>Alice</author> has same signature in both documents 3. Detects as MOVE operation (moved from position 2 to position 1)

Result: 1 MOVE operation detected (author element moved)

4. Operation Detection

The Semantic algorithm detects eight operation types:

4.1. Basic Operations (Level 1)

INSERT

New node added

+ <chapter id="3">New Chapter</chapter>
DELETE

Node removed

- <chapter id="old">Removed Chapter</chapter>
UPDATE

Node content/attributes changed

~ <title>Old → New</title>

4.2. Structural Operations (Level 2)

MOVE

Node relocated to different position

→ <author>Alice</author> (moved from position 2 to 1)

4.3. Semantic Operations (Level 3)

MERGE

Multiple nodes combined into one

⊕ <section> (merged from 2 separate sections)
SPLIT

One node divided into multiple

⊖ <section> (split into 2 separate sections)
UPGRADE

Node promoted to higher level

↑ <section> (promoted from depth 3 to depth 2)
DOWNGRADE

Node demoted to lower level

↓ <section> (demoted from depth 2 to depth 3)

5. Configuration

5.1. Basic Usage

Ruby API:

# Explicitly specify semantic algorithm
Canon::Comparison.equivalent?(doc1, doc2,
  diff_algorithm: :semantic
)

CLI:

canon diff file1.xml file2.xml --diff-algorithm semantic

5.2. With Similarity Threshold

Control how strict matching is:

Canon::Comparison.equivalent?(doc1, doc2,
  diff_algorithm: :semantic,
  match: {
    similarity_threshold: 0.90  # More lenient (default: 0.95)
  }
)
  • Higher (0.99) - Very conservative, only nearly identical nodes match

  • Lower (0.80) - More aggressive, allows less similar nodes to match

  • Default (0.95) - Balanced for most use cases

5.3. With Match Options

Semantic algorithm interprets match options for signature calculation:

Canon::Comparison.equivalent?(doc1, doc2,
  diff_algorithm: :semantic,
  match: {
    text_content: :normalize,      # Affects text signatures
    attribute_order: :ignore,       # Always ignored (unordered in signatures)
    element_position: :ignore       # MOVEs become informative
  }
)

See Algorithm-Specific Behavior for details.

5.4. With Diff Formatting

Semantic works best with by_object mode:

# Operation-based output (natural fit for Semantic)
Canon::Comparison.equivalent?(doc1, doc2,
  diff_algorithm: :semantic,
  diff_mode: :by_object,  # Shows operations
  verbose: true
)

# Traditional output (also works)
Canon::Comparison.equivalent?(doc1, doc2,
  diff_algorithm: :semantic,
  diff_mode: :by_line,  # Traditional format
  verbose: true
)

6. Output Format

6.1. Operation-Based Output (Default)

The Semantic algorithm naturally produces operation-based output:

Example 2. Operation-based diff example
UPDATE: book/title: "Old Title" → "New Title"
MOVE:   book/author → book/author (position 2 → 1)

Statistics:
  INSERT: 0
  DELETE: 0
  UPDATE: 1
  MOVE:   1
  • Shows what changed (operation type)

  • Shows where it changed (element path)

  • Provides statistics (operation counts)

6.2. Accessing Operations

result = Canon::Comparison.equivalent?(doc1, doc2,
  diff_algorithm: :semantic,
  verbose: true
)

# Access operations
result.operations.each do |op|
  puts "Type: #{op.type}"        # :insert, :delete, :update, :move
  puts "Path: #{op.path}"        # Element path
  puts "Details: #{op.details}"  # Operation-specific info
end

# Access statistics
puts "Moves: #{result.statistics.moves}"
puts "Updates: #{result.statistics.updates}"

7. Advantages

7.1. Intelligent Matching

  • Detects moves - Tracks content relocation

  • Handles restructuring - Works with heavily modified documents

  • Signature-based - Matches similar content anywhere

Table 1. Move detection example
DOM Algorithm Semantic Algorithm Advantage

Shows as DELETE + INSERT

Shows as MOVE

Clearer understanding

Many false positives

Accurate detection

Better analysis

Position-dependent

Position-independent

Handles reordering

7.2. Rich Analysis

  • Operation counts - Statistical view of changes

  • Operation paths - Precise location information

  • Confidence scores - Match quality indicators

8. Limitations

8.1. Performance

The Semantic algorithm is significantly slower:

Table 2. Performance comparison
Document Size DOM Time Semantic Time Ratio

1 KB

~1 ms

~10 ms

10x slower

10 KB

~10 ms

~150 ms

15x slower

100 KB

~100 ms

~3000 ms

30x slower

Workaround: Use DOM algorithm for large documents, Semantic for smaller ones

8.2. Experimental Status

  • API may change - Not stable yet

  • Behavior may change - Under active development

  • Edge cases - May have unexpected results

Workaround: Test thoroughly before relying on Semantic in production

8.3. Complex Matching

  • False matches - May match unrelated but similar content

  • Ambiguity - Multiple similar candidates can confuse matching

  • Tuning needed - May require similarity threshold adjustment

Workaround: Adjust similarity_threshold or use DOM algorithm

9. Common Use Cases

9.1. Use Case 1: Detecting Document Reorganization

# Analyze how document was restructured
result = Canon::Comparison.equivalent?(old_doc, new_doc,
  diff_algorithm: :semantic,
  verbose: true,
  diff_mode: :by_object
)

# Analyze operations
puts "Content moved: #{result.statistics.moves} times"
puts "Sections merged: #{result.statistics.merges}"
puts "Sections split: #{result.statistics.splits}"

9.2. Use Case 2: Content Evolution Tracking

# Track how content evolved over time
versions = [v1, v2, v3, v4]

versions.each_cons(2) do |old, new|
  result = Canon::Comparison.equivalent?(old, new,
    diff_algorithm: :semantic,
    verbose: true
  )

  log_operations(result.operations)
end

9.3. Use Case 3: Intelligent Test Assertions

# Allow reordering in tests
RSpec.describe "Content generation" do
  it "generates correct content regardless of order" do
    actual = generate_content

    expect(actual).to be_xml_equivalent_to(expected)
      .with_options(
        diff_algorithm: :semantic,
        element_position: :ignore  # Ignores moves
      )
  end
end

10. Best Practices

10.1. Start with DOM, Upgrade to Semantic When Needed

Use DOM algorithm as default, switch to Semantic only when move detection is required.

10.2. Adjust Similarity Threshold

Start conservative (0.95+), lower gradually if under-matching:

# Try different thresholds to find sweet spot
[0.95, 0.90, 0.85].each do |threshold|
  result = Canon::Comparison.equivalent?(doc1, doc2,
    diff_algorithm: :semantic,
    match: { similarity_threshold: threshold }
  )
  puts "Threshold #{threshold}: #{result.statistics.total} operations"
end

10.3. Use Appropriate Match Options

Configure dimensions to match your needs:

# Ignore cosmetic differences
Canon::Comparison.equivalent?(doc1, doc2,
  diff_algorithm: :semantic,
  match: {
    structural_whitespace: :ignore,
    element_position: :ignore
  }
)

11. Troubleshooting

11.1. Too Many Operations Detected

Problem: Everything shows as changed

Solution: Increase similarity threshold

match: { similarity_threshold: 0.98 }  # Was 0.95

11.2. Too Few Matches

Problem: Similar content shows as DELETE + INSERT

Solution: Decrease similarity threshold

match: { similarity_threshold: 0.85 }  # Was 0.95

11.3. Performance Issues

Problem: Comparison is very slow

Solution: Use DOM algorithm or limit document size

# Conditionally use Semantic only for small docs
algorithm = doc_size < 10_000 ? :semantic : :dom
Canon::Comparison.equivalent?(doc1, doc2,
  diff_algorithm: algorithm
)

12. Migration from DOM

12.1. Expected Changes

When switching from DOM to Semantic:

  1. MOVEs detected - Reordered content shows as MOVE instead of DELETE+INSERT

  2. Different output - Operations instead of line-based diff

  3. Slower performance - Accept longer comparison time

  4. New capabilities - Access to rich operation analysis

12.2. Migration Steps

  1. Test on small subset - Verify behavior on sample documents

  2. Compare outputs - Review DOM vs Semantic results side-by-side

  3. Adjust threshold - Tune similarity_threshold for your needs

  4. Update assertions - Adapt tests to operation-based output

  5. Monitor performance - Ensure acceptable speed

13. See Also