Class: Ast::Merge::MatchScoreBase
- Inherits:
-
Object
- Object
- Ast::Merge::MatchScoreBase
- Includes:
- Comparable
- Defined in:
- lib/ast/merge/match_score_base.rb
Overview
Base class for computing match scores between two nodes.
Match scores help determine which nodes from a template should be linked to which nodes in a destination document. This is particularly useful for complex nodes like tables where simple signature matching is insufficient.
The scoring algorithm is provided as a callable object (lambda, Proc, or any object responding to :call) which receives the two nodes and returns a score between 0.0 (no match) and 1.0 (perfect match).
Includes Comparable for sorting and comparison operations.
Constant Summary collapse
- DEFAULT_THRESHOLD =
Minimum score threshold for considering two nodes as a potential match
0.5
Instance Attribute Summary collapse
-
#algorithm ⇒ #call
readonly
The algorithm used to compute the match score.
-
#node_a ⇒ Object
readonly
The first node to compare (typically from template).
-
#node_b ⇒ Object
readonly
The second node to compare (typically from destination).
-
#threshold ⇒ Float
readonly
The minimum score to consider a match.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compare two scorers by their scores.
-
#eql?(other) ⇒ Boolean
Check equality for Hash key compatibility.
-
#hash ⇒ Integer
Generate a hash code for this scorer.
-
#initialize(node_a, node_b, algorithm:, threshold: DEFAULT_THRESHOLD) ⇒ MatchScoreBase
constructor
Initialize a match scorer.
-
#match? ⇒ Boolean
Check if the score meets the threshold for a match.
-
#score ⇒ Float
Compute and return the match score.
Constructor Details
#initialize(node_a, node_b, algorithm:, threshold: DEFAULT_THRESHOLD) ⇒ MatchScoreBase
Initialize a match scorer.
64 65 66 67 68 69 70 71 72 |
# File 'lib/ast/merge/match_score_base.rb', line 64 def initialize(node_a, node_b, algorithm:, threshold: DEFAULT_THRESHOLD) raise ArgumentError, "algorithm must respond to :call" unless algorithm.respond_to?(:call) @node_a = node_a @node_b = node_b @algorithm = algorithm @threshold = threshold @score = nil end |
Instance Attribute Details
#algorithm ⇒ #call (readonly)
Returns The algorithm used to compute the match score.
52 53 54 |
# File 'lib/ast/merge/match_score_base.rb', line 52 def algorithm @algorithm end |
#node_a ⇒ Object (readonly)
Returns The first node to compare (typically from template).
46 47 48 |
# File 'lib/ast/merge/match_score_base.rb', line 46 def node_a @node_a end |
#node_b ⇒ Object (readonly)
Returns The second node to compare (typically from destination).
49 50 51 |
# File 'lib/ast/merge/match_score_base.rb', line 49 def node_b @node_b end |
#threshold ⇒ Float (readonly)
Returns The minimum score to consider a match.
55 56 57 |
# File 'lib/ast/merge/match_score_base.rb', line 55 def threshold @threshold end |
Instance Method Details
#<=>(other) ⇒ Integer
Compare two scorers by their scores.
Required by Comparable. Enables <, <=, ==, >=, >, and between? operators.
96 97 98 |
# File 'lib/ast/merge/match_score_base.rb', line 96 def <=>(other) score <=> other.score end |
#eql?(other) ⇒ Boolean
Check equality for Hash key compatibility.
Two scorers are eql? if they have the same node_a, node_b, and score. This is stricter than == from Comparable (which only compares scores).
117 118 119 120 121 |
# File 'lib/ast/merge/match_score_base.rb', line 117 def eql?(other) return false unless other.is_a?(MatchScoreBase) node_a == other.node_a && node_b == other.node_b && score == other.score end |
#hash ⇒ Integer
Generate a hash code for this scorer.
Required for Hash key compatibility. Two scorers with the same node_a, node_b, and score should have the same hash.
106 107 108 |
# File 'lib/ast/merge/match_score_base.rb', line 106 def hash [node_a, node_b, score].hash end |
#match? ⇒ Boolean
Check if the score meets the threshold for a match.
86 87 88 |
# File 'lib/ast/merge/match_score_base.rb', line 86 def match? score >= threshold end |
#score ⇒ Float
Compute and return the match score.
The score is cached after first computation.
79 80 81 |
# File 'lib/ast/merge/match_score_base.rb', line 79 def score @score ||= compute_score end |