weightedpicker 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,6 +1,9 @@
1
1
  = weightedpicker changelog
2
2
 
3
3
  == Master (for 0.1.1)
4
+ Add bin/weight to deal with weight yaml.
5
+ Add WeightedPicker::names.
6
+ Add WeightedPicker::dump_histgram.
4
7
 
5
8
  == Version 0.1.0
6
9
  Use integer alternative to float.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/bin/weight ADDED
@@ -0,0 +1,32 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require "yaml"
5
+ require "pp"
6
+ require "optparse"
7
+
8
+ require "rubygems"
9
+ require "weightedpicker"
10
+
11
+ WIDTH = 50
12
+
13
+ ## option analysis
14
+ OPTIONS = {}
15
+ op = OptionParser.new
16
+ op.on("-l", "--lighten", "Lighten all"){ OPTIONS[:lighten] = true}
17
+ op.on("-w", "--weigh" , "Weigh all" ){ OPTIONS[:weigh] = true}
18
+ op.parse!(ARGV)
19
+
20
+ data_file = ARGV[0]
21
+ wp = WeightedPicker.load_file data_file
22
+
23
+ if OPTIONS[:lighten]
24
+ wp.names.each { |name| wp.lighten name }
25
+ io = File.open(data_file, "w") { |io| wp.dump io }
26
+ elsif OPTIONS[:weigh]
27
+ wp.names.each { |name| wp.weigh name }
28
+ io = File.open(data_file, "w") { |io| wp.dump io }
29
+ end
30
+
31
+ wp.dump_histgram($stdout)
32
+
@@ -99,6 +99,8 @@ class WeightedPicker
99
99
  INI_WEIGHT = 2** 8
100
100
  MIN_WEIGHT = 2** 0
101
101
 
102
+ HISTGRAM_WIDTH = 50
103
+
102
104
  class InvalidFilenameError < Exception; end
103
105
  class NoEntryError < Exception; end
104
106
  class NotExistKeyError < Exception; end
@@ -128,10 +130,30 @@ class WeightedPicker
128
130
  YAML.dump(@tree.names_weights, io)
129
131
  end
130
132
 
133
+ def dump_histgram(io)
134
+ encounters = {}
135
+ names_weights.each do |key, weight|
136
+ #val_log2 = (Math::log(weight)/Math::log(2.0)).to_i
137
+ power = Math::log2(weight).ceil
138
+ encounters[power] ||= 0
139
+ encounters[power] += 1
140
+ end
141
+ max = encounters.values.max
142
+ 0.upto(16) do |power|
143
+ num = encounters[power] || 0
144
+ stars = "*" * (HISTGRAM_WIDTH.to_f * num / max).ceil
145
+ io.printf("%6d(%4d)|#{stars}\n", 2**power, num)
146
+ end
147
+ end
148
+
131
149
  def names_weights
132
150
  @tree.names_weights
133
151
  end
134
152
 
153
+ def names
154
+ names_weights.keys
155
+ end
156
+
135
157
  # 乱数を利用して優先度で重み付けして要素を選び、要素を返す。
136
158
  # num is only for test. User should not use this argument.
137
159
  def pick
@@ -55,22 +55,6 @@ class WeightedPicker::Tree
55
55
  current_index = 2 * current_index + choise
56
56
  end
57
57
  return @names[current_index]
58
-
59
-
60
- #sums = []
61
- #keys = []
62
- #sum = 0
63
- #@weights.each do |key, weight|
64
- # keys << key
65
- # sum += weight
66
- # sums << sum
67
- #end
68
-
69
- #num ||= rand(sum)
70
- ## find index of first excess a number
71
- #sums.each_with_index do |item, index|
72
- # return keys[index] if num < item
73
- #end
74
58
  end
75
59
 
76
60
  def weigh(item)
@@ -188,5 +188,51 @@ describe "Weightedpicker" do
188
188
 
189
189
  end
190
190
 
191
+ describe "Weightedpicker::names" do
192
+ it "should return an array of names." do
193
+ @wp01.names.should == [ "A", "B"]
194
+ end
195
+
196
+ end
197
+
198
+ describe "Weightedpicker::dump_histgram" do
199
+ it "should output histgram to io." do
200
+ input = {}
201
+ 4.times do |power|
202
+ num = 10**power
203
+ num.times do |i|
204
+ input["#{power}_#{i}"] = num
205
+ end
206
+ end
207
+ #pp input
208
+ wp20 = WeightedPicker.new(input)
209
+ #pp wp20
210
+ io = StringIO.new
211
+ wp20.dump_histgram(io)
212
+ io.rewind
213
+ io.read.should == <<HERE
214
+ 1( 1)|*
215
+ 2( 0)|
216
+ 4( 0)|
217
+ 8( 0)|
218
+ 16( 10)|*
219
+ 32( 0)|
220
+ 64( 0)|
221
+ 128( 100)|*****
222
+ 256( 0)|
223
+ 512( 0)|
224
+ 1024(1000)|**************************************************
225
+ 2048( 0)|
226
+ 4096( 0)|
227
+ 8192( 0)|
228
+ 16384( 0)|
229
+ 32768( 0)|
230
+ 65536( 0)|
231
+ HERE
232
+
233
+ end
234
+
235
+ end
236
+
191
237
  end
192
238
 
@@ -5,13 +5,14 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "weightedpicker"
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["ippei94da"]
12
- s.date = "2013-04-10"
12
+ s.date = "2013-04-18"
13
13
  s.description = "This library enables to pick out items at the rate of their weight.\n Weight data is storaged as a YAML file.\n You can use this library for music player, wallpaper changer, language training.\n "
14
14
  s.email = "ippei94da@gmail.com"
15
+ s.executables = ["weight"]
15
16
  s.extra_rdoc_files = [
16
17
  "LICENSE.txt",
17
18
  "README.rdoc"
@@ -25,6 +26,7 @@ Gem::Specification.new do |s|
25
26
  "README.rdoc",
26
27
  "Rakefile",
27
28
  "VERSION",
29
+ "bin/weight",
28
30
  "lib/weightedpicker.rb",
29
31
  "lib/weightedpicker/tree.rb",
30
32
  "spec/a-1b256.yaml",
@@ -42,7 +44,7 @@ Gem::Specification.new do |s|
42
44
  s.homepage = "http://github.com/ippei94da/weightedpicker"
43
45
  s.licenses = ["MIT"]
44
46
  s.require_paths = ["lib"]
45
- s.rubygems_version = "1.8.23"
47
+ s.rubygems_version = "1.8.11"
46
48
  s.summary = "Picking one item from list at the rate of its weight."
47
49
 
48
50
  if s.respond_to? :specification_version then
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weightedpicker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-10 00:00:00.000000000 Z
12
+ date: 2013-04-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &71283260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: 2.13.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ~>
28
- - !ruby/object:Gem::Version
29
- version: 2.13.0
24
+ version_requirements: *71283260
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: rdoc
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &71282480 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ~>
@@ -37,15 +32,10 @@ dependencies:
37
32
  version: '3.12'
38
33
  type: :development
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ~>
44
- - !ruby/object:Gem::Version
45
- version: '3.12'
35
+ version_requirements: *71282480
46
36
  - !ruby/object:Gem::Dependency
47
37
  name: bundler
48
- requirement: !ruby/object:Gem::Requirement
38
+ requirement: &71281810 !ruby/object:Gem::Requirement
49
39
  none: false
50
40
  requirements:
51
41
  - - ~>
@@ -53,15 +43,10 @@ dependencies:
53
43
  version: 1.3.4
54
44
  type: :development
55
45
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: 1.3.4
46
+ version_requirements: *71281810
62
47
  - !ruby/object:Gem::Dependency
63
48
  name: jeweler
64
- requirement: !ruby/object:Gem::Requirement
49
+ requirement: &71281240 !ruby/object:Gem::Requirement
65
50
  none: false
66
51
  requirements:
67
52
  - - ~>
@@ -69,15 +54,10 @@ dependencies:
69
54
  version: 1.8.3
70
55
  type: :development
71
56
  prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ~>
76
- - !ruby/object:Gem::Version
77
- version: 1.8.3
57
+ version_requirements: *71281240
78
58
  - !ruby/object:Gem::Dependency
79
59
  name: simplecov
80
- requirement: !ruby/object:Gem::Requirement
60
+ requirement: &71280910 !ruby/object:Gem::Requirement
81
61
  none: false
82
62
  requirements:
83
63
  - - ! '>='
@@ -85,17 +65,13 @@ dependencies:
85
65
  version: '0'
86
66
  type: :development
87
67
  prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ! '>='
92
- - !ruby/object:Gem::Version
93
- version: '0'
68
+ version_requirements: *71280910
94
69
  description: ! "This library enables to pick out items at the rate of their weight.\n
95
70
  \ Weight data is storaged as a YAML file.\n You can use this library for music
96
71
  player, wallpaper changer, language training.\n "
97
72
  email: ippei94da@gmail.com
98
- executables: []
73
+ executables:
74
+ - weight
99
75
  extensions: []
100
76
  extra_rdoc_files:
101
77
  - LICENSE.txt
@@ -109,6 +85,7 @@ files:
109
85
  - README.rdoc
110
86
  - Rakefile
111
87
  - VERSION
88
+ - bin/weight
112
89
  - lib/weightedpicker.rb
113
90
  - lib/weightedpicker/tree.rb
114
91
  - spec/a-1b256.yaml
@@ -137,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
114
  version: '0'
138
115
  segments:
139
116
  - 0
140
- hash: -243347693
117
+ hash: -1071442157
141
118
  required_rubygems_version: !ruby/object:Gem::Requirement
142
119
  none: false
143
120
  requirements:
@@ -146,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
123
  version: '0'
147
124
  requirements: []
148
125
  rubyforge_project:
149
- rubygems_version: 1.8.23
126
+ rubygems_version: 1.8.11
150
127
  signing_key:
151
128
  specification_version: 3
152
129
  summary: Picking one item from list at the rate of its weight.