Class: Guardrails::TokenMatcher
- Inherits:
-
Object
- Object
- Guardrails::TokenMatcher
- Defined in:
- lib/guardrails/token_matcher.rb
Defined Under Namespace
Classes: Match
Constant Summary collapse
- NEAR_MATCH_THRESHOLD =
4
Instance Method Summary collapse
-
#initialize(tokens, near_match_threshold: NEAR_MATCH_THRESHOLD) ⇒ TokenMatcher
constructor
A new instance of TokenMatcher.
- #match(value) ⇒ Object
Constructor Details
#initialize(tokens, near_match_threshold: NEAR_MATCH_THRESHOLD) ⇒ TokenMatcher
Returns a new instance of TokenMatcher.
11 12 13 14 15 |
# File 'lib/guardrails/token_matcher.rb', line 11 def initialize(tokens, near_match_threshold: NEAR_MATCH_THRESHOLD) @tokens = tokens @lookup = tokens.to_h { |t| [HexNormalizer.normalize(t.value), t] } @near_match_threshold = near_match_threshold end |
Instance Method Details
#match(value) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/guardrails/token_matcher.rb', line 17 def match(value) return nil if value.nil? exact = @lookup[HexNormalizer.normalize(value)] return Match.new(token: exact, kind: :exact, distance: 0) if exact best = nil best_distance = @near_match_threshold + 1 @tokens.each do |t| d = HexNormalizer.distance(value, t.value) next unless d next if d.zero? if d < best_distance best = t best_distance = d end end return nil unless best Match.new(token: best, kind: :near, distance: best_distance) end |