Class: Psych::Merge::MappingMatchRefiner

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

Overview

Match refiner for YAML mapping entries that didn’t match by exact signature.

This refiner uses fuzzy matching to pair mapping entries (key-value pairs) that have:

  • Similar keys (e.g., database_url vs db_url)
  • Keys with typos or naming convention differences
  • Renamed keys that contain similar values

The matching algorithm considers:

  • Key name similarity (Levenshtein distance)
  • Value type similarity (both scalars, both mappings, etc.)
  • Value content similarity for scalars

Examples:

Basic usage

refiner = MappingMatchRefiner.new(threshold: 0.6)
matches = refiner.call(template_nodes, dest_nodes)

With custom weights

refiner = MappingMatchRefiner.new(
  threshold: 0.5,
  key_weight: 0.6,
  value_weight: 0.4
)

See Also:

  • Ast::Merge::MatchRefinerBase

Constant Summary collapse

DEFAULT_KEY_WEIGHT =

Default weight for key similarity

0.7
DEFAULT_VALUE_WEIGHT =

Default weight for value similarity

0.3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(threshold: DEFAULT_THRESHOLD, key_weight: DEFAULT_KEY_WEIGHT, value_weight: DEFAULT_VALUE_WEIGHT, **options) ⇒ MappingMatchRefiner

Initialize a mapping match refiner.

Parameters:

  • threshold (Float) (defaults to: DEFAULT_THRESHOLD)

    Minimum score to accept a match (default: 0.5)

  • key_weight (Float) (defaults to: DEFAULT_KEY_WEIGHT)

    Weight for key similarity (default: 0.7)

  • value_weight (Float) (defaults to: DEFAULT_VALUE_WEIGHT)

    Weight for value similarity (default: 0.3)



47
48
49
50
51
# File 'lib/psych/merge/mapping_match_refiner.rb', line 47

def initialize(threshold: DEFAULT_THRESHOLD, key_weight: DEFAULT_KEY_WEIGHT, value_weight: DEFAULT_VALUE_WEIGHT, **options)
  super(threshold: threshold, **options)
  @key_weight = key_weight
  @value_weight = value_weight
end

Instance Attribute Details

#key_weightFloat (readonly)

Returns Weight for key similarity (0.0-1.0).

Returns:

  • (Float)

    Weight for key similarity (0.0-1.0)



37
38
39
# File 'lib/psych/merge/mapping_match_refiner.rb', line 37

def key_weight
  @key_weight
end

#value_weightFloat (readonly)

Returns Weight for value similarity (0.0-1.0).

Returns:

  • (Float)

    Weight for value similarity (0.0-1.0)



40
41
42
# File 'lib/psych/merge/mapping_match_refiner.rb', line 40

def value_weight
  @value_weight
end

Instance Method Details

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

Find matches between unmatched mapping entries.

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 mapping entry matches



59
60
61
62
63
64
65
66
67
68
# File 'lib/psych/merge/mapping_match_refiner.rb', line 59

def call(template_nodes, dest_nodes, context = {})
  template_entries = template_nodes.select { |n| mapping_entry?(n) }
  dest_entries = dest_nodes.select { |n| mapping_entry?(n) }

  return [] if template_entries.empty? || dest_entries.empty?

  greedy_match(template_entries, dest_entries) do |t_node, d_node|
    compute_entry_similarity(t_node, d_node)
  end
end