-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add bounded version of restricted edit distance
- Loading branch information
Showing
9 changed files
with
289 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module Edits | ||
# Comparison helpers | ||
module Compare | ||
# Given a prototype string and an array of strings, determines which | ||
# string is most similar to the prototype. | ||
# | ||
# `most_similar("foo", strings)` is functionally equivalent to | ||
# `strings.min_by { |s| distance("foo", s) }`, leveraging | ||
# {.distance_with_max}. | ||
# | ||
# @example | ||
# most_similar("atom", %w[tram atlas rota racer]) | ||
# # => "atlas" | ||
# @param prototype [String] | ||
# @param strings [<String>] | ||
# @return [String, nil] most similar string, or nil for empty array | ||
def most_similar(prototype, strings) | ||
return nil if strings.empty? | ||
min_s = strings[0] | ||
min_d = distance(prototype, min_s) | ||
|
||
strings[1..-1].each do |s| | ||
return min_s if min_d.zero? | ||
d = distance_with_max(prototype, s, min_d) | ||
if d < min_d | ||
min_d = d | ||
min_s = s | ||
end | ||
end | ||
|
||
min_s | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.