Class: Toml::Merge::TableMatchRefiner
- Inherits:
-
Ast::Merge::MatchRefinerBase
- Object
- Ast::Merge::MatchRefinerBase
- Toml::Merge::TableMatchRefiner
- 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
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
-
#weights ⇒ Hash
readonly
Weights for similarity computation.
Instance Method Summary collapse
-
#call(template_nodes, dest_nodes, context = {}) ⇒ Array<MatchResult>
Find matches between unmatched table nodes.
-
#initialize(threshold: DEFAULT_THRESHOLD, weights: {}, **options) ⇒ TableMatchRefiner
constructor
Initialize a table match refiner.
Constructor Details
#initialize(threshold: DEFAULT_THRESHOLD, weights: {}, **options) ⇒ TableMatchRefiner
Initialize a table match refiner.
47 48 49 50 |
# File 'lib/toml/merge/table_match_refiner.rb', line 47 def initialize(threshold: DEFAULT_THRESHOLD, weights: {}, **) super(threshold: threshold, node_types: [:table], **) @weights = DEFAULT_WEIGHTS.merge(weights) end |
Instance Attribute Details
#weights ⇒ Hash (readonly)
Returns 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.
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 |