Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tcrouch committed Apr 28, 2019
1 parent eb24dc8 commit 296e028
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 13 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/edits/damerau_levenshtein.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions lib/edits/hamming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions lib/edits/jaro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/edits/jaro_winkler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions lib/edits/levenshtein.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ module Edits
# * Insertion
# * Deletion
# * Substitution
#
# @see https://en.wikipedia.org/wiki/Levenshtein_distance
module Levenshtein
extend Compare

# Calculate the Levenshtein (edit) distance of two sequences.
#
# @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

Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/edits/restricted_edit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions spec/edits/hamming_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
["2173896", "2233796", 3],
["foo", "bar", 3],
["toned", "roses", 3],
["explorer", "exploded", 2],

["", "abc", 3],
["abc", "", 3],
Expand Down

0 comments on commit 296e028

Please sign in to comment.