weighted_list_rank 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f50527ee43cba5bd55144f8c76bc4b702cd2e84a797008aa4bed9cfa9b055a9
4
- data.tar.gz: 0427e71591900a28fadcc5e68dae0bfbd71c7b97b00896953b5ecaec80ccbad7
3
+ metadata.gz: 801eb04183d7dec5f99bfbcb76a902a920dcfee105f83f0c0c9a98667b73e6e4
4
+ data.tar.gz: c5c5665b7205bc0fd52c4c7d23f34103ba2dfd764579e0a575897f8ce36e95af
5
5
  SHA512:
6
- metadata.gz: 2da62414b58dafb3ac4e49cbc18e0cb1b24c09edb78e0888365a3ac68e16e764974a83be8379dabe3a4291673170d7d1e80429a1a1c97189aa7ee3f1edbae597
7
- data.tar.gz: 545f22a6c95c3713698c743e7f5fb1f2495034fc57ee81b4a745a8d064594cc9fa818fd5481d6443152a51e1fdaa5a7f1f362e17297f72a345a6e59a36255d12
6
+ metadata.gz: 210d7a10b0978195b26a0fd3cbdfd068b93e09066ac332984e165d011170abb544837fc4aafd6cb410055bd7b412199733ca2d37e1190630aa5bd650f8f5df9a
7
+ data.tar.gz: 65adb83584d5b1ec5d2282a0f8411edb147eb700f3030e9ab4c80def5ca8d2d7963166091cda87e8ea8a603a1c7f7168d28b661c2cbf0a4c6807b3617c47551c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2024-02-03
4
+
5
+ - refactored the exponential algorithm to only add bonus points out of a pool of 50% of a list's weight
6
+
7
+ ## [0.1.3] - 2024-02-03
8
+
9
+ - sort the score_details by the highest score from each list first
10
+
3
11
  ## [0.1.2] - 2024-02-03
4
12
 
5
13
  - Added list weight to rank result
@@ -34,7 +34,8 @@ module WeightedListRank
34
34
  sorted_items = items.map do |id, details|
35
35
  {
36
36
  id: id,
37
- score_details: details[:list_details],
37
+ # Sort the score_details array by score in descending order before including it
38
+ score_details: details[:list_details].sort_by { |detail| -detail[:score] },
38
39
  total_score: details[:total_score]
39
40
  }
40
41
  end
@@ -1,20 +1,9 @@
1
1
  module WeightedListRank
2
2
  module Strategies
3
- # The Exponential strategy calculates the score of an item within a list using an exponential formula.
4
- # This strategy emphasizes the significance of an item's rank within the list, where items with higher
5
- # ranks (closer to 1) are exponentially more valuable than those with lower ranks.
6
- #
7
- # The exponential nature of the calculation is controlled by the +exponent+ attribute, allowing for
8
- # flexible adjustment of how steeply the score decreases as rank increases.
9
3
  class Exponential < WeightedListRank::Strategy
10
- # The exponent used in the score calculation formula. Higher values increase the rate at which
11
- # scores decrease as item rank increases.
12
- #
13
- # @return [Float] the exponent value
14
4
  attr_reader :exponent
15
5
 
16
6
  # Initializes a new instance of the Exponential strategy with an optional exponent.
17
- #
18
7
  # @param exponent [Float] the exponent to use in the score calculation formula, defaults to 1.5.
19
8
  def initialize(exponent: 1.5)
20
9
  @exponent = exponent
@@ -22,23 +11,28 @@ module WeightedListRank
22
11
 
23
12
  # Calculates the score of an item within a list based on its rank position, the total number of items,
24
13
  # and the list's weight, using an exponential formula.
14
+ # The bonus pool is set to 50% of the list's weight.
25
15
  #
26
16
  # @param list [WeightedListRank::List] the list containing the item being scored.
27
17
  # @param item [WeightedListRank::Item] the item for which to calculate the score.
28
18
  #
29
19
  # @return [Float] the calculated score for the item, adjusted by the list's weight and the specified exponent.
30
20
  def calculate_score(list, item)
31
- rank_position = item.position
32
-
33
- # if there are no positions, then just return the list weight
34
- return list.weight if rank_position.nil?
21
+ # Return the list weight if there are no positions
22
+ return list.weight if item.position.nil?
35
23
 
36
24
  num_items = list.items.count
25
+ total_bonus_pool = list.weight * 0.5 # Bonus pool is 50% of the list's weight
26
+
27
+ # Calculate the exponential factor for the item's rank position
28
+ exponential_factor = (num_items + 1 - item.position)**exponent
29
+ total_exponential_factor = (1..num_items).sum { |pos| (num_items + 1 - pos)**exponent }
30
+
31
+ # Allocate a portion of the total bonus pool based on the item's exponential factor
32
+ item_bonus = (exponential_factor / total_exponential_factor) * total_bonus_pool
37
33
 
38
- contribution = ((num_items + 1 - rank_position)**exponent) / num_items.to_f
39
- scaled_contribution = contribution / num_items
40
- bonus = scaled_contribution * list.weight
41
- list.weight + bonus
34
+ # The final score is the list's weight plus the item's allocated bonus
35
+ list.weight + item_bonus
42
36
  end
43
37
  end
44
38
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WeightedListRank
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weighted_list_rank
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Sherman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-03 00:00:00.000000000 Z
11
+ date: 2024-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop