From 296e028acad04887573effef895f58b9c4db516b Mon Sep 17 00:00:00 2001 From: Tom Crouch Date: Sun, 28 Apr 2019 14:37:36 +0100 Subject: [PATCH] docs --- README.md | 7 +++++++ lib/edits/damerau_levenshtein.rb | 2 +- lib/edits/hamming.rb | 6 ++++-- lib/edits/jaro.rb | 4 ++-- lib/edits/jaro_winkler.rb | 6 +++--- lib/edits/levenshtein.rb | 8 +++++--- lib/edits/restricted_edit.rb | 4 ++-- spec/edits/hamming_spec.rb | 1 + 8 files changed, 25 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 15ca5fb..d73bb28 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,13 @@ Edits::JaroWinkler.distance "information", "informant" # => 0.05858585858585863 ``` +### Hamming + +```ruby +Edits::Hamming.distance("explorer", "exploded") +# => 2 +``` + ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. diff --git a/lib/edits/damerau_levenshtein.rb b/lib/edits/damerau_levenshtein.rb index 517fcfa..c98a5bd 100644 --- a/lib/edits/damerau_levenshtein.rb +++ b/lib/edits/damerau_levenshtein.rb @@ -16,7 +16,7 @@ module DamerauLevenshtein # # => 3 # @param seq1 [String, Array] # @param seq2 [String, Array] - # @return [Integer] + # @return [Integer] distance, 0 (identical) or greater (more distant) def self.distance(seq1, seq2) seq1, seq2 = seq2, seq1 if seq1.length > seq2.length diff --git a/lib/edits/hamming.rb b/lib/edits/hamming.rb index 56547f4..b2d3920 100644 --- a/lib/edits/hamming.rb +++ b/lib/edits/hamming.rb @@ -8,10 +8,12 @@ module Hamming # Calculate the Hamming distance between two sequences. # # @note A true distance metric, satisfies triangle inequality. - # + # @example + # Edits::Hamming.distance("explorer", "exploded") + # # => 2 # @param seq1 [String, Array] # @param seq2 [String, Array] - # @return [Integer] Hamming distance + # @return [Integer] distance, 0 (identical) or greater (more distant) def self.distance(seq1, seq2) # if seq1.is_a?(Integer) && seq2.is_a?(Integer) # return (seq1 ^ seq2).to_s(2).count("1") diff --git a/lib/edits/jaro.rb b/lib/edits/jaro.rb index 578902c..3614328 100644 --- a/lib/edits/jaro.rb +++ b/lib/edits/jaro.rb @@ -16,7 +16,7 @@ module Jaro # # => 0.9023569023569024 # @param seq1 [String, Array] # @param seq2 [String, Array] - # @return [Float] similarity, between 0.0 (none) and 1.0 (identical) + # @return [Float] similarity, from 0.0 (none) to 1.0 (identical) def self.similarity(seq1, seq2) return 1.0 if seq1 == seq2 return 0.0 if seq1.empty? || seq2.empty? @@ -39,7 +39,7 @@ def self.similarity(seq1, seq2) # Edits::Jaro.distance("information", "informant") # # => 0.09764309764309764 # @param (see #distance) - # @return [Float] distance, between 0.0 (identical) and 1.0 (distant) + # @return [Float] distance, from 0.0 (identical) to 1.0 (distant) def self.distance(str1, str2) 1.0 - similarity(str1, str2) end diff --git a/lib/edits/jaro_winkler.rb b/lib/edits/jaro_winkler.rb index 94ba11f..780444c 100644 --- a/lib/edits/jaro_winkler.rb +++ b/lib/edits/jaro_winkler.rb @@ -9,7 +9,7 @@ module JaroWinkler # Should not exceed 0.25 or metric range will leave 0..1 WINKLER_PREFIX_WEIGHT = 0.1 - # Threshold for boosting Jaro with winkler prefix multiplier. + # Threshold for boosting Jaro with Winkler prefix multiplier. # Default is 0.7 WINKLER_THRESHOLD = 0.7 @@ -31,7 +31,7 @@ module JaroWinkler # @param seq2 [String, Array] # @param threshold [Float] threshold for applying Winkler prefix weighting # @param weight [Float] weighting for common prefix, should not exceed 0.25 - # @return [Float] similarity, between 0.0 (none) and 1.0 (identical) + # @return [Float] similarity, from 0.0 (none) to 1.0 (identical) def self.similarity( seq1, seq2, threshold: WINKLER_THRESHOLD, @@ -59,7 +59,7 @@ def self.similarity( # Edits::JaroWinkler.distance("information", "informant") # # => 0.05858585858585863 # @param (see #distance) - # @return [Float] distance, between 0.0 (identical) and 1.0 (distant) + # @return [Float] distance, from 0.0 (identical) to 1.0 (distant) def self.distance( seq1, seq2, threshold: WINKLER_THRESHOLD, diff --git a/lib/edits/levenshtein.rb b/lib/edits/levenshtein.rb index 47defd6..82c6abf 100644 --- a/lib/edits/levenshtein.rb +++ b/lib/edits/levenshtein.rb @@ -7,6 +7,8 @@ module Edits # * Insertion # * Deletion # * Substitution + # + # @see https://en.wikipedia.org/wiki/Levenshtein_distance module Levenshtein extend Compare @@ -14,11 +16,11 @@ module Levenshtein # # @note A true distance metric, satisfies triangle inequality. # @example - # Levenshtein.distance('sand', 'hands') + # Levenshtein.distance("sand", "hands") # # => 2 # @param seq1 [String, Array] # @param seq2 [String, Array] - # @return [Integer] + # @return [Integer] distance, 0 (identical) or greater (more distant) def self.distance(seq1, seq2) seq1, seq2 = seq2, seq1 if seq1.length > seq2.length @@ -74,7 +76,7 @@ def self.distance(seq1, seq2) # @param seq1 [String, Array] # @param seq2 [String, Array] # @param max [Integer] maximum distance - # @return [Integer] + # @return [Integer] distance, from 0 (identical) to max (more distant) def self.distance_with_max(seq1, seq2, max) seq1, seq2 = seq2, seq1 if seq1.length > seq2.length diff --git a/lib/edits/restricted_edit.rb b/lib/edits/restricted_edit.rb index bcd1c26..39ded52 100644 --- a/lib/edits/restricted_edit.rb +++ b/lib/edits/restricted_edit.rb @@ -24,7 +24,7 @@ module RestrictedEdit # # => 3 # @param seq1 [String, Array] # @param seq2 [String, Array] - # @return [Integer] + # @return [Integer] distance, 0 (identical) or greater (more distant) def self.distance(seq1, seq2) seq1, seq2 = seq2, seq1 if seq1.length > seq2.length @@ -96,7 +96,7 @@ def self.distance(seq1, seq2) # @param seq1 [String, Array] # @param seq2 [String, Array] # @param max [Integer] maximum distance - # @return [Integer] + # @return [Integer] distance, from 0 (identical) to max (more distant) def self.distance_with_max(seq1, seq2, max) seq1, seq2 = seq2, seq1 if seq1.length > seq2.length diff --git a/spec/edits/hamming_spec.rb b/spec/edits/hamming_spec.rb index 162a91b..4856024 100644 --- a/spec/edits/hamming_spec.rb +++ b/spec/edits/hamming_spec.rb @@ -14,6 +14,7 @@ ["2173896", "2233796", 3], ["foo", "bar", 3], ["toned", "roses", 3], + ["explorer", "exploded", 2], ["", "abc", 3], ["abc", "", 3],