weighted_list_rank 0.5.0 → 0.5.1

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: 3c2d9ea8239b54fa7d9423a36ae922c9de54a4bee54d8df0fdc17847b98eb60d
4
- data.tar.gz: '03808fac0b2fd206b726734eeca8f3a62ad9b3aeab51f58237f22561775d4dbf'
3
+ metadata.gz: afae67c4e97dd094fb6de0be3fd5dd0886372ae2edd873f61fdbc12f9c4b9e87
4
+ data.tar.gz: 8c12da106e959044d21a6889f35c959d91a637ae69ada9113744ce61ceed21fc
5
5
  SHA512:
6
- metadata.gz: 590693e2ea27c84cf861ba36dacfc50f97374f5b12c47b1b6f72693f6652a92d875c5d9a56090809ffeefc2b91e17e76e2288586d3bbd01baa408c3cbbcb91a4
7
- data.tar.gz: 462ed842d02c3a5aa70103b2e9d7dfa40e496eb7f3bc2cd9946a518319e3b72610bfed43007d4b80a71c4927d5d39867e638e4325dace2c6e9bf5f6d680bb2d9
6
+ metadata.gz: 1c3c9b95c5ff8416938a36b8c04d6b066948284e0e2e414f7f037871ae88432441f3f617e32cc50d60d2cd7fad7cb5462735eae1ebacab8a4041c06e4c516b28
7
+ data.tar.gz: 9449fbe8419cb5a25ad1c1d5bdeae92831a14d95f81a43371755f1f369c59c53cbb01f3aefcd24fed8ad31a6de12b76d4bf81a219bf18d1bde0d7f01510b0950
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.5.1] - 2024-07-23
4
+ - Updated the `Exponential` strategy to prevent unranked items from receiving bonus points if the list contains both ranked and unranked items. In such cases, unranked items will now only receive the base weight of the list, ensuring ranked items are always prioritized.
5
+
3
6
  ## [0.5.0] - 2024-07-23
4
7
  - Added `average_list_length` feature to the `Exponential` strategy to adjust the bonus pool based on the average number of items across all lists. This helps prevent smaller lists from receiving disproportionately large bonuses.
5
8
  - Introduced a new configuration option `include_unranked_items` in the `Exponential` strategy. When enabled, unranked items will receive an equal share of the bonus pool, while ranked items will receive an exponential bonus.
@@ -36,32 +36,38 @@ module WeightedListRank
36
36
  # Default score to the list's weight
37
37
  score = list.weight
38
38
 
39
- # Determine the number of ranked and unranked items
39
+ # Total number of items in the list
40
+ total_items = list.items.count
41
+
42
+ # Separate ranked and unranked items
40
43
  ranked_items = list.items.select { |i| i.position }
41
- unranked_items = list.items.reject { |i| i.position }
42
44
  num_ranked_items = ranked_items.count
43
- num_unranked_items = unranked_items.count
45
+
46
+ if num_ranked_items > 0 && item.position.nil?
47
+ # If there are ranked items, unranked items get no bonus, only the list's weight
48
+ return score
49
+ end
44
50
 
45
51
  # Calculate the total bonus pool
46
52
  total_bonus_pool = list.weight * bonus_pool_percentage
47
53
 
48
54
  # Adjust the bonus pool based on the average list length
49
55
  adjusted_bonus_pool = if average_list_length && average_list_length > 0
50
- total_bonus_pool * (list.items.count / average_list_length.to_f)
56
+ total_bonus_pool * (total_items / average_list_length.to_f)
51
57
  else
52
58
  total_bonus_pool
53
59
  end
54
60
 
55
61
  if item.position.nil?
56
- # If the item is unranked, give it an equal share of the remaining bonus pool
57
- if include_unranked_items && num_unranked_items > 0
58
- unranked_bonus = adjusted_bonus_pool / list.items.count
62
+ # Unranked items get no bonus if there are ranked items
63
+ if include_unranked_items && num_ranked_items == 0
64
+ unranked_bonus = adjusted_bonus_pool / total_items
59
65
  score += unranked_bonus
60
66
  end
61
67
  else
62
- # If the item is ranked, calculate its bonus using the exponential formula
63
- exponential_factor = (num_ranked_items + 1 - item.position)**exponent
64
- total_exponential_factor = (1..num_ranked_items).sum { |pos| (num_ranked_items + 1 - pos)**exponent }
68
+ # Ranked items receive a bonus calculated using the exponential formula
69
+ exponential_factor = (total_items + 1 - item.position)**exponent
70
+ total_exponential_factor = (1..total_items).sum { |pos| (total_items + 1 - pos)**exponent }
65
71
 
66
72
  # Allocate a portion of the adjusted bonus pool based on the item's exponential factor
67
73
  item_bonus = (exponential_factor / total_exponential_factor) * adjusted_bonus_pool
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WeightedListRank
4
- VERSION = "0.5.0"
4
+ VERSION = "0.5.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weighted_list_rank
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Sherman