Module: StubRequests::Utils::Fuzzy
- Defined in:
- lib/stub_requests/utils/fuzzy.rb
Overview
Provides convenience methods for hashes
Constant Summary collapse
- FILTER_REGEX =
Returns a pattern excluding all except alphanumeric
/(^\w\d)/.freeze
Class Method Summary collapse
-
.compute_distances(original, others) ⇒ Object
:nodoc:.
-
.filter_matches(matches) ⇒ Object
:nodoc:.
-
.jaro_distance(original, other) ⇒ Object
:nodoc:.
-
.match(original, others) ⇒ Array
Find strings that are similar.
-
.normalize_string(value) ⇒ Object
:nodoc:.
Class Method Details
.compute_distances(original, others) ⇒ Object
:nodoc:
49 50 51 52 53 |
# File 'lib/stub_requests/utils/fuzzy.rb', line 49 def self.compute_distances(original, others) others.each_with_object([]) do |other, memo| memo << [jaro_distance(original, other), other] end end |
.filter_matches(matches) ⇒ Object
:nodoc:
40 41 42 43 44 45 46 |
# File 'lib/stub_requests/utils/fuzzy.rb', line 40 def self.filter_matches(matches) suggestions = matches.values return suggestions if suggestions.size <= 3 matches.select { |distance, _| distance >= 0.7 } .values end |
.jaro_distance(original, other) ⇒ Object
:nodoc:
56 57 58 59 60 61 62 |
# File 'lib/stub_requests/utils/fuzzy.rb', line 56 def self.jaro_distance(original, other) JaroWinkler.jaro_distance( normalize_string(original), normalize_string(other), StubRequests.config., ) end |
.match(original, others) ⇒ Array
Find strings that are similar
34 35 36 37 |
# File 'lib/stub_requests/utils/fuzzy.rb', line 34 def self.match(original, others) matches = compute_distances(original, others).sort.reverse filter_matches(matches.to_h) end |
.normalize_string(value) ⇒ Object
:nodoc:
65 66 67 |
# File 'lib/stub_requests/utils/fuzzy.rb', line 65 def self.normalize_string(value) value.to_s.gsub(FILTER_REGEX, "") end |