weighted_list_rank 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rubocop.yml +8 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +72 -0
- data/LICENSE.txt +21 -0
- data/README.md +114 -0
- data/Rakefile +16 -0
- data/lib/weighted_list_rank/context.rb +46 -0
- data/lib/weighted_list_rank/item.rb +15 -0
- data/lib/weighted_list_rank/list.rb +21 -0
- data/lib/weighted_list_rank/strategies/exponential.rb +41 -0
- data/lib/weighted_list_rank/strategy.rb +11 -0
- data/lib/weighted_list_rank/version.rb +5 -0
- data/lib/weighted_list_rank.rb +13 -0
- data/sig/weighted_list_rank.rbs +4 -0
- data/weighted_list_rank.gemspec +42 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7d6af0dabc07041ac8d52dade9dcaf6788e68d1b8809e85e5dc57d9cf032b83c
|
4
|
+
data.tar.gz: 913262cd828e18bc68bc69d4f62bc422b17c2711d82de7132f887d2732d65534
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e3d0d1f24a242c249337c9dd2889d0468bb616fbead79ebabdda1d7bf3ad629c962e6b004d0b7051933d91fb1a8f5aa15602c70858a9ed7b20bd68224c0ec7f1
|
7
|
+
data.tar.gz: 19921c8c23d8ceee3f00a304f7b8e98d1f0229759e186cd25faa507b45633f2e5fb2e382c529697ea8f6aa6f51e13f81ec77f84748f79b2eb9ca83f5ae7702db
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at shane.sherman@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
weighted_list_rank (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
json (2.7.1)
|
11
|
+
language_server-protocol (3.17.0.3)
|
12
|
+
lint_roller (1.1.0)
|
13
|
+
minitest (5.21.2)
|
14
|
+
parallel (1.24.0)
|
15
|
+
parser (3.3.0.5)
|
16
|
+
ast (~> 2.4.1)
|
17
|
+
racc
|
18
|
+
racc (1.7.3)
|
19
|
+
rainbow (3.1.1)
|
20
|
+
rake (13.1.0)
|
21
|
+
regexp_parser (2.9.0)
|
22
|
+
rexml (3.2.6)
|
23
|
+
rubocop (1.59.0)
|
24
|
+
json (~> 2.3)
|
25
|
+
language_server-protocol (>= 3.17.0)
|
26
|
+
parallel (~> 1.10)
|
27
|
+
parser (>= 3.2.2.4)
|
28
|
+
rainbow (>= 2.2.2, < 4.0)
|
29
|
+
regexp_parser (>= 1.8, < 3.0)
|
30
|
+
rexml (>= 3.2.5, < 4.0)
|
31
|
+
rubocop-ast (>= 1.30.0, < 2.0)
|
32
|
+
ruby-progressbar (~> 1.7)
|
33
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
34
|
+
rubocop-ast (1.30.0)
|
35
|
+
parser (>= 3.2.1.0)
|
36
|
+
rubocop-minitest (0.34.5)
|
37
|
+
rubocop (>= 1.39, < 2.0)
|
38
|
+
rubocop-ast (>= 1.30.0, < 2.0)
|
39
|
+
rubocop-performance (1.20.2)
|
40
|
+
rubocop (>= 1.48.1, < 2.0)
|
41
|
+
rubocop-ast (>= 1.30.0, < 2.0)
|
42
|
+
rubocop-rake (0.6.0)
|
43
|
+
rubocop (~> 1.0)
|
44
|
+
ruby-progressbar (1.13.0)
|
45
|
+
standard (1.33.0)
|
46
|
+
language_server-protocol (~> 3.17.0.2)
|
47
|
+
lint_roller (~> 1.0)
|
48
|
+
rubocop (~> 1.59.0)
|
49
|
+
standard-custom (~> 1.0.0)
|
50
|
+
standard-performance (~> 1.3)
|
51
|
+
standard-custom (1.0.2)
|
52
|
+
lint_roller (~> 1.0)
|
53
|
+
rubocop (~> 1.50)
|
54
|
+
standard-performance (1.3.1)
|
55
|
+
lint_roller (~> 1.1)
|
56
|
+
rubocop-performance (~> 1.20.2)
|
57
|
+
unicode-display_width (2.5.0)
|
58
|
+
|
59
|
+
PLATFORMS
|
60
|
+
x86_64-linux
|
61
|
+
|
62
|
+
DEPENDENCIES
|
63
|
+
minitest (~> 5.0)
|
64
|
+
rake (~> 13.0)
|
65
|
+
rubocop (~> 1.21)
|
66
|
+
rubocop-minitest (~> 0.3)
|
67
|
+
rubocop-rake (~> 0.6)
|
68
|
+
standard (~> 1.0)
|
69
|
+
weighted_list_rank!
|
70
|
+
|
71
|
+
BUNDLED WITH
|
72
|
+
2.3.22
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Shane Sherman
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
# WeightedListRank
|
2
|
+
|
3
|
+
The WeightedListRank gem is a robust, flexible system designed for ranking items within weighted lists. It leverages scoring algorithms to evaluate and prioritize items based on defined criteria, making it an essential tool for data analysis, recommendation systems, and more. The only algorithm currently is the Exponential strategy, an algorithm that forms the backbone of The Greatest Books (thegreatestbooks.org).
|
4
|
+
|
5
|
+
WeightedListRank is built with versatility in mind, accommodating different ranking needs and scenarios. By implementing a strategy pattern, it offers the flexibility to apply various algorithms for item scoring and ranking within lists, each considering the list's weight and item positions. The default Exponential strategy underscores the significance of an item's rank in the list, applying an exponential formula to determine its score, thus ensuring that higher-ranked items receive proportionally greater emphasis.
|
6
|
+
|
7
|
+
The system is designed for easy extension, allowing developers to introduce new strategies that cater to specific requirements or experimental approaches.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
$ bundle add weighted_list_rank
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install weighted_list_rank
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
The WeightedListRank system allows for flexible ranking of items within weighted lists using various strategies. Below is how to employ the Exponential strategy for ranking, using text identifiers for better readability.
|
22
|
+
|
23
|
+
Defining Items and Lists
|
24
|
+
You need to define your items and lists to include the required methods from WeightedListRank::Item and WeightedListRank::List interfaces:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class MyItem
|
28
|
+
include WeightedListRank::Item
|
29
|
+
attr_reader :id, :position
|
30
|
+
|
31
|
+
def initialize(id, position)
|
32
|
+
@id = id
|
33
|
+
@position = position
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class MyList
|
38
|
+
include WeightedListRank::List
|
39
|
+
attr_reader :id, :weight, :items
|
40
|
+
|
41
|
+
def initialize(id, weight, items)
|
42
|
+
@id = id
|
43
|
+
@weight = weight
|
44
|
+
@items = items
|
45
|
+
end
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
### Using the Exponential Strategy
|
50
|
+
After setting up your items and list, you can apply the Exponential strategy to rank the items. The strategy allows for customization of the exponential rate at which scores decrease according to rank.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
require 'weighted_list_rank'
|
54
|
+
|
55
|
+
# Initialize items and list with text identifiers
|
56
|
+
items = [
|
57
|
+
MyItem.new("Item 1", 1),
|
58
|
+
MyItem.new("Item 2", 2),
|
59
|
+
MyItem.new("Item 3", 3)
|
60
|
+
]
|
61
|
+
list = MyList.new("List 1", 10, items)
|
62
|
+
|
63
|
+
# Initialize the Exponential strategy with an optional exponent
|
64
|
+
exponential_strategy = WeightedListRank::Strategies::Exponential.new(exponent: 1.5)
|
65
|
+
|
66
|
+
# Create a RankingContext using the Exponential strategy
|
67
|
+
ranking_context = WeightedListRank::RankingContext.new(exponential_strategy)
|
68
|
+
|
69
|
+
# Rank the items
|
70
|
+
ranked_items = ranking_context.rank([list])
|
71
|
+
|
72
|
+
# Display the ranked items
|
73
|
+
ranked_items.each do |item|
|
74
|
+
puts "Item: #{item[:id]}, Total Score: #{item[:total_score]}"
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
### Customization Example
|
79
|
+
Adjust the exponent parameter to change how significantly item rank affects the score. A higher exponent increases the disparity between high and low-ranked items.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
# Customizing the exponent for greater emphasis on higher ranks
|
83
|
+
custom_strategy = WeightedListRank::Strategies::Exponential.new(exponent: 2.0)
|
84
|
+
|
85
|
+
# Ranking with the customized strategy
|
86
|
+
custom_ranking_context = WeightedListRank::RankingContext.new(custom_strategy)
|
87
|
+
custom_ranked_items = custom_ranking_context.rank([list])
|
88
|
+
|
89
|
+
# Output the results
|
90
|
+
custom_ranked_items.each do |item|
|
91
|
+
puts "Item: #{item[:id]}, Custom Total Score: #{item[:total_score]}"
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
|
96
|
+
## Development
|
97
|
+
|
98
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
99
|
+
|
100
|
+
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).
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
We're on the lookout for more algorithm strategies and implementations! If you're passionate about data analysis, algorithm design, or simply have ideas for improving item ranking methodologies, WeightedListRank offers a welcoming platform for your contributions. Whether it's enhancing existing strategies, proposing new ones, or optimizing the framework for broader applications, your input can significantly impact the community and the projects relying on this system.
|
105
|
+
|
106
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ssherman/weighted_list_rank. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/ssherman/weighted_list_rank/blob/master/CODE_OF_CONDUCT.md).
|
107
|
+
|
108
|
+
## License
|
109
|
+
|
110
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
111
|
+
|
112
|
+
## Code of Conduct
|
113
|
+
|
114
|
+
Everyone interacting in the WeightedListRank project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ssherman/weighted_list_rank/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test rubocop]
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module WeightedListRank
|
2
|
+
# RankingContext is responsible for applying a ranking strategy to a collection of lists and their items.
|
3
|
+
# It aggregates scores for each item across all lists, based on the provided strategy.
|
4
|
+
class RankingContext
|
5
|
+
# @strategy: The strategy used for calculating scores.
|
6
|
+
attr_reader :strategy
|
7
|
+
|
8
|
+
# Initializes a new RankingContext with an optional ranking strategy.
|
9
|
+
# @param strategy [Strategy] the strategy to use for ranking items, defaults to Strategies::Exponential.
|
10
|
+
def initialize(strategy = Strategies::Exponential.new)
|
11
|
+
@strategy = strategy
|
12
|
+
end
|
13
|
+
|
14
|
+
# Ranks items across multiple lists according to the strategy's score calculation.
|
15
|
+
# @param lists [Array<List>] an array of List objects to be ranked.
|
16
|
+
# @return [Array<Hash>] a sorted array of item scores, with each item's details including ID, score details, and total score.
|
17
|
+
def rank(lists)
|
18
|
+
items = {}
|
19
|
+
lists.each do |list|
|
20
|
+
list.items.each do |item|
|
21
|
+
score = strategy.calculate_score(list, item)
|
22
|
+
items[item.id] ||= {}
|
23
|
+
# Ensure the list_details array exists, then append the new score detail
|
24
|
+
items[item.id][:list_details] ||= []
|
25
|
+
items[item.id][:list_details] << {list_id: list.id, score: score}
|
26
|
+
|
27
|
+
# Ensure the total_score is initialized, then add the score
|
28
|
+
items[item.id][:total_score] ||= 0
|
29
|
+
items[item.id][:total_score] += score
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Convert hash to a sorted array
|
34
|
+
sorted_items = items.map do |id, details|
|
35
|
+
{
|
36
|
+
id: id,
|
37
|
+
score_details: details[:list_details],
|
38
|
+
total_score: details[:total_score]
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
# Sort the array by total_score in descending order
|
43
|
+
sorted_items.sort_by { |item| -item[:total_score] }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module WeightedListRank
|
2
|
+
module Item
|
3
|
+
# Returns the position of the item within its list. Must be implemented by including class.
|
4
|
+
# @return [Integer] the position of the item
|
5
|
+
def position
|
6
|
+
raise NotImplementedError, "Implement this method to return the item's position"
|
7
|
+
end
|
8
|
+
|
9
|
+
# Returns the unique identifier of the item. Must be implemented by including class.
|
10
|
+
# @return [Integer, String] the unique identifier of the item
|
11
|
+
def id
|
12
|
+
raise NotImplementedError, "Implement this method to return the item's unique identifier"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module WeightedListRank
|
2
|
+
module List
|
3
|
+
# Returns the weight of the list. Must be implemented by including class.
|
4
|
+
# @return [Numeric] the weight of the list
|
5
|
+
def weight
|
6
|
+
raise NotImplementedError, "Implement this method to return the list's weight"
|
7
|
+
end
|
8
|
+
|
9
|
+
# Returns an enumerable collection of items in the list. Must be implemented by including class.
|
10
|
+
# @return [Enumerable<Item>] the items within the list
|
11
|
+
def items
|
12
|
+
raise NotImplementedError, "Implement this method to return the enumerable collection of items"
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns the unique identifier of the list. Must be implemented by including class.
|
16
|
+
# @return [Integer, String] the unique identifier of the list
|
17
|
+
def id
|
18
|
+
raise NotImplementedError, "Implement this method to return the unique identifier of a list"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module WeightedListRank
|
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
|
+
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
|
+
attr_reader :exponent
|
15
|
+
|
16
|
+
# Initializes a new instance of the Exponential strategy with an optional exponent.
|
17
|
+
#
|
18
|
+
# @param exponent [Float] the exponent to use in the score calculation formula, defaults to 1.5.
|
19
|
+
def initialize(exponent: 1.5)
|
20
|
+
@exponent = exponent
|
21
|
+
end
|
22
|
+
|
23
|
+
# Calculates the score of an item within a list based on its rank position, the total number of items,
|
24
|
+
# and the list's weight, using an exponential formula.
|
25
|
+
#
|
26
|
+
# @param list [WeightedListRank::List] the list containing the item being scored.
|
27
|
+
# @param item [WeightedListRank::Item] the item for which to calculate the score.
|
28
|
+
#
|
29
|
+
# @return [Float] the calculated score for the item, adjusted by the list's weight and the specified exponent.
|
30
|
+
def calculate_score(list, item)
|
31
|
+
rank_position = item.position
|
32
|
+
num_items = list.items.count
|
33
|
+
|
34
|
+
contribution = ((num_items + 1 - rank_position)**exponent) / num_items.to_f
|
35
|
+
scaled_contribution = contribution / num_items
|
36
|
+
bonus = scaled_contribution * list.weight
|
37
|
+
list.weight + bonus
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module WeightedListRank
|
2
|
+
class Strategy
|
3
|
+
# Calculates the score for an item within a list. Must be implemented by subclass.
|
4
|
+
# @param list [List] the list containing the item.
|
5
|
+
# @param item [Item] the item to calculate the score for.
|
6
|
+
# @return [Numeric] the calculated score for the item.
|
7
|
+
def calculate_score(list, item)
|
8
|
+
raise NotImplementedError
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "weighted_list_rank/version"
|
4
|
+
require "weighted_list_rank/context"
|
5
|
+
require "weighted_list_rank/item"
|
6
|
+
require "weighted_list_rank/list"
|
7
|
+
require "weighted_list_rank/strategy"
|
8
|
+
require "weighted_list_rank/strategies/exponential"
|
9
|
+
|
10
|
+
module WeightedListRank
|
11
|
+
class Error < StandardError; end
|
12
|
+
# Your code goes here...
|
13
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/weighted_list_rank/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "weighted_list_rank"
|
7
|
+
spec.version = WeightedListRank::VERSION
|
8
|
+
spec.authors = ["Shane Sherman"]
|
9
|
+
spec.email = ["shane.sherman@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "generate ranks of items from weighted lists"
|
12
|
+
spec.description = "generate ranks of items from weighted lists"
|
13
|
+
spec.homepage = "https://github.com/ssherman/weighted_list_rank"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.6.0"
|
16
|
+
|
17
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/ssherman/weighted_list_rank"
|
21
|
+
spec.metadata["changelog_uri"] = "https://github.com/ssherman/weighted_list_rank/CHANGELOG.md"
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(__dir__) do
|
26
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
28
|
+
end
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
|
34
|
+
# Uncomment to register a new dependency of your gem
|
35
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
36
|
+
spec.add_development_dependency "rubocop", "~> 1.0"
|
37
|
+
spec.add_development_dependency "standard", "~> 1.0"
|
38
|
+
spec.add_development_dependency "rubocop-minitest", "~> 0.3"
|
39
|
+
spec.add_development_dependency "rubocop-rake", "~> 0.6"
|
40
|
+
# For more information and examples about making a new gem, check out our
|
41
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weighted_list_rank
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shane Sherman
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-02-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rubocop
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: standard
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubocop-minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop-rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.6'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.6'
|
69
|
+
description: generate ranks of items from weighted lists
|
70
|
+
email:
|
71
|
+
- shane.sherman@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".rubocop.yml"
|
77
|
+
- CHANGELOG.md
|
78
|
+
- CODE_OF_CONDUCT.md
|
79
|
+
- Gemfile
|
80
|
+
- Gemfile.lock
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- lib/weighted_list_rank.rb
|
85
|
+
- lib/weighted_list_rank/context.rb
|
86
|
+
- lib/weighted_list_rank/item.rb
|
87
|
+
- lib/weighted_list_rank/list.rb
|
88
|
+
- lib/weighted_list_rank/strategies/exponential.rb
|
89
|
+
- lib/weighted_list_rank/strategy.rb
|
90
|
+
- lib/weighted_list_rank/version.rb
|
91
|
+
- sig/weighted_list_rank.rbs
|
92
|
+
- weighted_list_rank.gemspec
|
93
|
+
homepage: https://github.com/ssherman/weighted_list_rank
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata:
|
97
|
+
homepage_uri: https://github.com/ssherman/weighted_list_rank
|
98
|
+
source_code_uri: https://github.com/ssherman/weighted_list_rank
|
99
|
+
changelog_uri: https://github.com/ssherman/weighted_list_rank/CHANGELOG.md
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: 2.6.0
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubygems_version: 3.3.7
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: generate ranks of items from weighted lists
|
119
|
+
test_files: []
|