Class: Toml::Merge::TableMatchRefiner

Inherits:
Ast::Merge::MatchRefinerBase
  • Object
show all
Defined in:
lib/toml/merge/table_match_refiner.rb

Overview

Match refiner for TOML tables that didn’t match by exact signature.

This refiner uses string similarity to pair tables that have:

  • Similar but not identical names

  • Similar structure (key counts)

  • Similar key names

Tables are matched using a multi-factor scoring algorithm that considers:

  • Table/section name similarity

  • Key name overlap

  • Position in document

Examples:

Basic usage

refiner = TableMatchRefiner.new(threshold: 0.5)
matches = refiner.call(template_nodes, dest_nodes)

With custom weights

refiner = TableMatchRefiner.new(
  threshold: 0.6,
  weights: {
    name_match: 0.5,
    key_overlap: 0.3,
    position: 0.2
  }
)

See Also:

  • Ast::Merge::MatchRefinerBase

Constant Summary collapse

DEFAULT_WEIGHTS =

Default weights for match scoring

{
  name_match: 0.5,   # Weight for table name similarity
  key_overlap: 0.3,  # Weight for shared keys
  position: 0.2,     # Weight for position similarity
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(threshold: DEFAULT_THRESHOLD, weights: {}, **options) ⇒ TableMatchRefiner

Initialize a table match refiner.

Parameters:

  • threshold (Float) (defaults to: DEFAULT_THRESHOLD)

    Minimum score to accept a match (default: 0.5)

  • weights (Hash) (defaults to: {})

    Custom weights for similarity factors



47
48
49
50
# File 'lib/toml/merge/table_match_refiner.rb', line 47

def initialize(threshold: DEFAULT_THRESHOLD, weights: {}, **options)
  super(threshold: threshold, node_types: [:table], **options)
  @weights = DEFAULT_WEIGHTS.merge(weights)
end

Instance Attribute Details

#weightsHash (readonly)

Returns Weights for similarity computation.

Returns:

  • (Hash)

    Weights for similarity computation



41
42
43
# File 'lib/toml/merge/table_match_refiner.rb', line 41

def weights
  @weights
end

Instance Method Details

#call(template_nodes, dest_nodes, context = {}) ⇒ Array<MatchResult>

Find matches between unmatched table nodes.

Parameters:

  • template_nodes (Array)

    Unmatched nodes from template

  • dest_nodes (Array)

    Unmatched nodes from destination

  • context (Hash) (defaults to: {})

    Additional context

Returns:

  • (Array<MatchResult>)

    Array of table matches



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/toml/merge/table_match_refiner.rb', line 58

def call(template_nodes, dest_nodes, context = {})
  template_tables = extract_tables(template_nodes)
  dest_tables = extract_tables(dest_nodes)

  return [] if template_tables.empty? || dest_tables.empty?

  # Build position information for better matching
  total_template = template_tables.size
  total_dest = dest_tables.size

  greedy_match(template_tables, dest_tables) do |t_node, d_node|
    t_idx = template_tables.index(t_node) || 0
    d_idx = dest_tables.index(d_node) || 0

    compute_table_similarity(t_node, d_node, t_idx, d_idx, total_template, total_dest)
  end
end