weighted_random 0.0.2
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.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.rdoc +75 -0
- data/Rakefile +2 -0
- data/lib/weighted_random/activerecord/base.rb +7 -0
- data/lib/weighted_random/version.rb +3 -0
- data/lib/weighted_random.rb +20 -0
- data/weighted_random.gemspec +21 -0
- metadata +56 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
= Weighted Random
|
2
|
+
Weighted randomization extension for ActiveRecord.
|
3
|
+
|
4
|
+
Loads records with weight value for randomization and gives ability of weighted randomization of them.
|
5
|
+
|
6
|
+
== Setup
|
7
|
+
|
8
|
+
Simply put the latest gem from into your Gemfile:
|
9
|
+
|
10
|
+
gem 'weighted_random'
|
11
|
+
|
12
|
+
== Usage
|
13
|
+
|
14
|
+
=== Migration
|
15
|
+
|
16
|
+
Put these two lines into your migration file of model for which you want weighted randomization ability:
|
17
|
+
|
18
|
+
t.integer :weight
|
19
|
+
t.integer :cumulative_weight
|
20
|
+
|
21
|
+
=== Load methods
|
22
|
+
|
23
|
+
Load methods for weighted randomization into your desired class model:
|
24
|
+
|
25
|
+
class LastName << ActiveRecord::Base
|
26
|
+
weighted_randomizable
|
27
|
+
...
|
28
|
+
end
|
29
|
+
|
30
|
+
=== Load records
|
31
|
+
|
32
|
+
Load records with weight property for randomization, here is an example of loading last names and their frequencies from CSV file:
|
33
|
+
|
34
|
+
last_names.csv:
|
35
|
+
Smith,10
|
36
|
+
Johnson,8
|
37
|
+
Williams,7
|
38
|
+
Jones,6
|
39
|
+
Brown,6
|
40
|
+
Davis,5
|
41
|
+
Miller,4
|
42
|
+
Wilson,3
|
43
|
+
|
44
|
+
seeds.rb:
|
45
|
+
csv = CSV.open(File.expand_path("seeds/first_names.csv", File.dirname(__FILE__)))
|
46
|
+
names = csv.collect { |name, weight| {:name => name, :weight => to_i} }
|
47
|
+
csv.close
|
48
|
+
LastName.create_with_cumulative_weight(names)
|
49
|
+
|
50
|
+
Simply you have to supply collection of hashes, each with :weight integer property, to create_with_cumulative_weight
|
51
|
+
method of your desired model class and it will load them all into database with additional cumulative_weight attribute.
|
52
|
+
|
53
|
+
=== Weighted randomization
|
54
|
+
|
55
|
+
To randomize some record in a weighted way, simply run:
|
56
|
+
|
57
|
+
LastName.weighted_rand
|
58
|
+
|
59
|
+
== Demonstration
|
60
|
+
|
61
|
+
10.times { puts FirstName.weighted_rand.name }
|
62
|
+
Johnson
|
63
|
+
Brown
|
64
|
+
Johnson
|
65
|
+
Smith
|
66
|
+
Smith
|
67
|
+
Jones
|
68
|
+
Smith
|
69
|
+
Williams
|
70
|
+
Miller
|
71
|
+
Jones
|
72
|
+
|
73
|
+
== Author
|
74
|
+
|
75
|
+
Szymon Przybył (github.com/apocalyptiq)
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module WeightedRandom
|
2
|
+
def compute_and_insert_cumulative_weight(collection)
|
3
|
+
weight_sum = 0
|
4
|
+
collection.collect do |item|
|
5
|
+
weight_sum += item[:weight]
|
6
|
+
item[:cumulative_weight] = weight_sum
|
7
|
+
item
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def create_with_cumulative_weight(collection)
|
12
|
+
self.create! self.compute_and_insert_cumulative_weight(collection)
|
13
|
+
end
|
14
|
+
|
15
|
+
def weighted_rand
|
16
|
+
self.where("cumulative_weight >= #{rand(self.maximum('cumulative_weight'))}").order('cumulative_weight').first
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'weighted_random/activerecord/base'
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "weighted_random/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "weighted_random"
|
7
|
+
s.version = WeightedRandom::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Szymon Przybył"]
|
10
|
+
s.email = ["apocalyptiq@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/apocalyptiq/weighted_random"
|
12
|
+
s.summary = %q{Weighted randomization extension for ActiveRecord}
|
13
|
+
s.description = %q{ActiveRecord extension for weighted randomization which supplies loading records with weight for randomize into database and weighted randomization of them}
|
14
|
+
|
15
|
+
s.rubyforge_project = "weighted_random"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weighted_random
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Szymon Przybył
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-04-11 00:00:00.000000000 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
description: ActiveRecord extension for weighted randomization which supplies loading
|
16
|
+
records with weight for randomize into database and weighted randomization of them
|
17
|
+
email:
|
18
|
+
- apocalyptiq@gmail.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- README.rdoc
|
26
|
+
- Rakefile
|
27
|
+
- lib/weighted_random.rb
|
28
|
+
- lib/weighted_random/activerecord/base.rb
|
29
|
+
- lib/weighted_random/version.rb
|
30
|
+
- weighted_random.gemspec
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: https://github.com/apocalyptiq/weighted_random
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project: weighted_random
|
52
|
+
rubygems_version: 1.6.2
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Weighted randomization extension for ActiveRecord
|
56
|
+
test_files: []
|