ranking_distance 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2bbbf46b3640915b503b8836f5509690f6c0f479f7d0d8e8b4fbeb71039e6bc4
4
+ data.tar.gz: e3b58bd3d424ae183c5f82633dd356e136e371b3fa0a716e50b9173b61557ceb
5
+ SHA512:
6
+ metadata.gz: 57cf4109193102f0ad780e6ba1bb07d2ae8d5f4677daf6dd616c353d6110f5ed779faa9e2d2de4616cc96410d114f259e45c79d327e18280182e7b4b86d55266
7
+ data.tar.gz: 521c03eb613b6996a3d7266c1c3abbf5f1b928654897cbbde11a53808fb4c0117b357194dcdb1e758728689989a335a512a876a3286cab5ca0eb3221ddea13af
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ranking_distance.gemspec
6
+ gemspec
7
+
8
+ group :development, :test do
9
+ gem "rake", "~> 13.0"
10
+ gem "rspec-core", "~> 3.12"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ranking_distance (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (13.0.6)
10
+ rspec-core (3.12.2)
11
+ rspec-support (~> 3.12.0)
12
+ rspec-support (3.12.0)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ rake (~> 13.0)
19
+ ranking_distance!
20
+ rspec-core (~> 3.12)
21
+
22
+ BUNDLED WITH
23
+ 2.3.5
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Mathieu Alexandre
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # RankingDistance
2
+
3
+ RankingDistance is a Ruby gem that provides a simple and efficient way to compute the distance between two rankings. Rankings are arrays of elements, and the distance is a positive number that is small when the rankings are similar and bigger when they are not.
4
+
5
+ Unlike other edit distances, such as the Levenshtein distance, RankDistance weights more the insertions at the front of the rankings and gives less weight to insertions at the back of the rankings. This makes the distance computed by RankDistance more reflective of the percived similarity between the rankings.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ranking_distance'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ranking_distance
22
+
23
+ ## Usage
24
+
25
+ 3 Methods are available:
26
+
27
+ `RankingDistance.absolute_distance(arr1, arr2)`
28
+
29
+ Computes the absolute distance between two arrays.
30
+ The absolute distance is computed as follows:
31
+ - if the two arrays are equal, the distance is 0
32
+ - if the two arrays are different, the distance is the number of swaps and insertions needed to transform one array into the other
33
+ - Each swap cost is dependent on the distance between the two swapped elements
34
+ - Each insertion cost is dependent on the position of the inserted element (inserting at the end is cheaper than inserting at the start)
35
+ The absolute distance is thus always a positive integer.
36
+
37
+ `RankingDistance.relative_distance(arr1, arr2)`
38
+
39
+ Computes the relative distance between two arrays.
40
+ The relative distance is computed by dividing the absolute distance by the maximum possible distance between the two arrays.
41
+ This means that the relative distance is always a float between 0 and 1.
42
+ 0 if the two arrays are equal, 1 if the two arrays are built with completely different elements.
43
+
44
+ `RankingDistance.relative_proximity(arr1, arr2)`
45
+
46
+ Computes the relative proximity between two arrays.
47
+ The relative proximity is computed by substracting the relative distance from 1.
48
+ This means that the relative proximity is always a float between 0 and 1.
49
+ 1 if the two arrays are equal, 0 if the two arrays are built with completely different elements.
50
+
51
+
52
+ ## Development
53
+
54
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
55
+
56
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/almathie/ranking_distance.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RankingDistance
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ranking_distance/version"
4
+
5
+ module RankingDistance
6
+ class Error < StandardError; end
7
+
8
+ # A class used to represent a unique element in an array.
9
+ # This class is used to fill arrays with a unique element so that the arrays can be compared.
10
+ # This class is used internally by the RankingDistance module and should not be used directly.
11
+ class UniqueElement
12
+ def ==(_)
13
+ false
14
+ end
15
+ end
16
+
17
+ #
18
+ # Computes the absolute distance between two arrays.
19
+ # The absolute distance is computed as follows:
20
+ # - if the two arrays are equal, the distance is 0
21
+ # - if the two arrays are different, the distance is the number of swaps and insertions needed to transform one array into the other
22
+ # - Each swap cost is dependent on the distance between the two swapped elements
23
+ # - Each insertion cost is dependent on the position of the inserted element (inserting at the end is cheaper than inserting at the start)
24
+ # The absolute distance is thus always a positive integer.
25
+ #
26
+ # @param arr1 [Array] the first array
27
+ # @param arr2 [Array] the second array
28
+ # @return [Integer] the absolute distance between the two arrays
29
+ #
30
+ def self.absolute_distance(arr1, arr2)
31
+ return 0 if arr1 == arr2
32
+
33
+ shortest_arr, longest_arr = [arr1.dup, arr2.dup].sort_by(&:length)
34
+ shortest_arr << UniqueElement.new until shortest_arr.length == longest_arr.length
35
+
36
+ edit_distance = 0
37
+ start_index = 0
38
+ while start_index < longest_arr.length
39
+ if longest_arr[start_index] != shortest_arr[start_index]
40
+ element = longest_arr[start_index]
41
+ element_index_in_truncated_short_array = shortest_arr.drop(start_index).index(element)
42
+ if element_index_in_truncated_short_array # Element found -> swap
43
+ edit_distance += element_index_in_truncated_short_array
44
+ shortest_arr.insert(start_index, shortest_arr.delete_at(element_index_in_truncated_short_array + start_index))
45
+ else # Element not found -> insert
46
+ edit_distance += (longest_arr.length - start_index)
47
+ shortest_arr.insert(start_index, element)
48
+ end
49
+ end
50
+ start_index += 1
51
+ end
52
+
53
+ return edit_distance
54
+ end
55
+
56
+ #
57
+ # Computes the relative distance between two arrays.
58
+ # The relative distance is computed by dividing the absolute distance by the maximum possible distance between the two arrays.
59
+ # This means that the relative distance is always a float between 0 and 1.
60
+ # 0 if the two arrays are equal, 1 if the two arrays are built with completely different elements.
61
+ #
62
+ # @param arr1 [Array] the first array
63
+ # @param arr2 [Array] the second array
64
+ # @return [Float] the relative distance between the two arrays
65
+ #
66
+ def self.relative_distance(arr1, arr2)
67
+ return 0 if arr1.empty? && arr2.empty?
68
+
69
+ _, longest_arr = [arr1, arr2].sort_by(&:length)
70
+ max_distance = longest_arr.length * (longest_arr.length + 1) / 2
71
+ return absolute_distance(arr1, arr2).to_f / max_distance
72
+ end
73
+
74
+ #
75
+ # Computes the relative proximity between two arrays.
76
+ # The relative proximity is computed by substracting the relative distance from 1.
77
+ # This means that the relative proximity is always a float between 0 and 1.
78
+ # 1 if the two arrays are equal, 0 if the two arrays are built with completely different elements.
79
+ #
80
+ # @param arr1 [Array] the first array
81
+ # @param arr2 [Array] the second array
82
+ # @return [Float] the relative proximity between the two arrays
83
+ #
84
+ def self.relative_proximity(arr1, arr2)
85
+ return 1 - relative_distance(arr1, arr2)
86
+ end
87
+ end
@@ -0,0 +1,4 @@
1
+ module RankingDistance
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ranking_distance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexandre Mathieu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-06-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Helps determine how closely related two rankings are
14
+ email:
15
+ - almathie@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Gemfile
21
+ - Gemfile.lock
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - lib/ranking_distance.rb
26
+ - lib/ranking_distance/version.rb
27
+ - sig/ranking_distance.rbs
28
+ homepage: https://github.com/almathie/ranking_distance
29
+ licenses: []
30
+ metadata:
31
+ homepage_uri: https://github.com/almathie/ranking_distance
32
+ source_code_uri: https://github.com/almathie/ranking_distance
33
+ changelog_uri: https://github.com/almathie/ranking_distance
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 2.6.0
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.3.26
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: Compute a distance between two arrays
53
+ test_files: []