kujibiki 0.1.4 → 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
  SHA1:
3
- metadata.gz: cc1b80e4107b12827cb68fab29191475df72cff4
4
- data.tar.gz: 79d96d0bfed5e0894855001e540c9895bed350fc
3
+ metadata.gz: 5a00aa2d312cdb6a276739da7fd703594f1d6d77
4
+ data.tar.gz: ec1f679ddd807cb3d49af9e03dd1d9d80a7c2a16
5
5
  SHA512:
6
- metadata.gz: 3c38987eecf30a7c776a4d47a3b77daf67e342afd0be4a3e9142578a00f26ae4e39b6d9f65600e7df6b39860eef182640dfddedc2286ba8956058d6d570e45da
7
- data.tar.gz: fa28225731ed7113e53d9f12fca6ef6f52df4435ec97159259aadaf2cf82796376fd42e3a5cab1cef443a9b2b9f52f6a0a9493079829206cb3a8880148b6d80d
6
+ metadata.gz: d33baa2cce6c158200ca79930aaf358c879fe0ef68390f565ebb945479dd95c174031db0f81a5274bf8df37979f4867bf1bab2b5fa3abf3b918dca96164ebc43
7
+ data.tar.gz: 9981869ef4d582dc539828bd8be38c8ef285ef3221d7772b584dcd12370b94ce6d338562e084a553c2c85f2cb0c4220e24388fd24bc60a54ffc389b129282990
@@ -1,43 +1,52 @@
1
1
  class Array
2
2
  alias_method :_sample, :sample
3
3
  def sample(n=nil, random: nil, weight: nil)
4
- unless weight
5
- if n
6
- if random
7
- return _sample(n, random: random)
8
- else
9
- return _sample(n)
10
- end
11
- else
12
- if random
13
- return _sample(random: random)
14
- else
15
- return _sample
16
- end
17
- end
18
- end
4
+ return call_original_sample(n, random: random) if (weight.nil? || self.size <= 1)
19
5
 
20
6
  unless weight.select {|w| w > 0}.size >= self.size
21
7
  raise ArgumentError, "weight size must be larger than or equal to sample number(#{self.size})"
22
8
  end
23
9
 
24
- sum = weight.inject(0.0, :+)
25
- normalized_hash = self.each_with_object({}).with_index do |(e, h), i|
26
- h[e] = weight[i] / sum
27
- end
10
+ begin
11
+ sum = weight.inject(0.0, :+)
12
+ normalized_hash = self.each_with_object({}).with_index do |(e, h), i|
13
+ h[e] = weight[i] / sum
14
+ end
15
+
16
+ pick_max = 1.0
17
+ sampled = n.times.map do
18
+ pick = random ? random.rand(0..pick_max) : rand(0..pick_max)
28
19
 
29
- pick_max = 1.0
30
- sampled = n.times.map do
31
- pick = random ? random.rand(0..pick_max) : rand(0..pick_max)
20
+ e, weight = normalized_hash.find do |e, weight|
21
+ (pick <= weight) || ((pick -= weight) && false)
22
+ end
32
23
 
33
- e, weight = normalized_hash.find do |e, weight|
34
- (pick <= weight) || ((pick -= weight) && false)
24
+ pick_max -= weight
25
+ normalized_hash[e] = 0 and e
35
26
  end
36
27
 
37
- pick_max -= weight
38
- normalized_hash[e] = 0 and e
28
+ sampled.size > 1 ? sampled : sampled.first
29
+ rescue => e
30
+ warn([e.class, e.message, e.backtrace.join("\n")].join(' '))
31
+ return call_original_sample(n, random: random)
39
32
  end
33
+ end
40
34
 
41
- sampled.size > 1 ? sampled : sampled.first
35
+ private
36
+
37
+ def call_original_sample(n=nil, random: nil)
38
+ if n
39
+ if random
40
+ _sample(n, random: random)
41
+ else
42
+ _sample(n)
43
+ end
44
+ else
45
+ if random
46
+ _sample(random: random)
47
+ else
48
+ _sample
49
+ end
50
+ end
42
51
  end
43
52
  end
@@ -1,3 +1,3 @@
1
1
  module Kujibiki
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kujibiki
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naoki Shimizu