probability_to_friendly_string 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/probability_to_friendly_string.rb +40 -10
- data/test/friendly_probability_tests.rb +25 -3
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 579e97359c71ab54409353dd83c154dc09e4c357b1069f1455c04125602f39b8
|
4
|
+
data.tar.gz: f290613fadcea9859d6e27432b780f6b91445ebd0fbadde34e5866121cf1d396
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d68f336766d8adfc11a3252d7448cfdb3b12088e33093485a4100a45175718d9ddcb54a7da98fc4dccdc70cdabbdfd35871772ba11fcaf9caa40bf683662c7b6
|
7
|
+
data.tar.gz: 84c70e153f65e5044605c2ed29b1dc58421c5cdff3a17305c9b79bd47e61bfed29deea21945ad105cb174f45535a0b208e351b1d9bfe54f51f5cb5d26ccc5cfb
|
@@ -2,12 +2,31 @@ module ProbabilityToFriendlyString
|
|
2
2
|
# Represents probability represented in a friendly manner.
|
3
3
|
class FriendlyProbability
|
4
4
|
include Comparable
|
5
|
-
attr_reader :numerator, :denominator, :friendlyString
|
5
|
+
attr_reader :numerator, :denominator, :friendlyDescription, :friendlyString
|
6
6
|
|
7
7
|
@@fractionsData = nil
|
8
|
-
|
8
|
+
@@friendlyDescriptionValues = [0.005, 0.02, 0.08, 0.15, 0.2, 0.45, 0.55, 0.7, 0.8, 0.85, 0.9, 0.95, 0.995]
|
9
|
+
@@friendlyDescriptionStrings = [
|
10
|
+
"Hard to imagine",
|
11
|
+
"Barely possible",
|
12
|
+
"Still possible",
|
13
|
+
"Some chance",
|
14
|
+
"Could happen",
|
15
|
+
"Perhaps",
|
16
|
+
"Flip a coin",
|
17
|
+
"Likelier than not",
|
18
|
+
"Good chance",
|
19
|
+
"Probably",
|
20
|
+
"Quite likely",
|
21
|
+
"Pretty likely",
|
22
|
+
"Very likely",
|
23
|
+
"Almost certainly",
|
24
|
+
]
|
25
|
+
|
26
|
+
def initialize(numerator, denominator, friendlyDescription, friendlyString = nil)
|
9
27
|
@numerator = numerator
|
10
28
|
@denominator = denominator
|
29
|
+
@friendlyDescription = friendlyDescription
|
11
30
|
if friendlyString
|
12
31
|
@friendlyString = friendlyString
|
13
32
|
else
|
@@ -45,14 +64,20 @@ module ProbabilityToFriendlyString
|
|
45
64
|
if f < 0 or f > 1
|
46
65
|
raise RangeError, "f is less than 0 or greater than 1"
|
47
66
|
end
|
67
|
+
# index of the least element > f
|
68
|
+
index = @@friendlyDescriptionValues.bsearch_index {|x| x > f}
|
69
|
+
if index.nil?
|
70
|
+
index = @@friendlyDescriptionStrings.length - 1
|
71
|
+
end
|
72
|
+
friendlyDescription = @@friendlyDescriptionStrings[index]
|
48
73
|
if f == 0
|
49
|
-
return FriendlyProbability.new 0, 1
|
74
|
+
return FriendlyProbability.new 0, 1, friendlyDescription
|
50
75
|
elsif f == 1
|
51
|
-
return FriendlyProbability.new 1, 1
|
76
|
+
return FriendlyProbability.new 1, 1, friendlyDescription
|
52
77
|
elsif f > 0.99
|
53
|
-
return FriendlyProbability.new 99, 100, ">99 in 100"
|
78
|
+
return FriendlyProbability.new 99, 100, friendlyDescription, ">99 in 100"
|
54
79
|
elsif f < 0.01
|
55
|
-
return FriendlyProbability.new 1, 100, "<1 in 100"
|
80
|
+
return FriendlyProbability.new 1, 100, friendlyDescription, "<1 in 100"
|
56
81
|
end
|
57
82
|
|
58
83
|
FriendlyProbability._createFractionsData
|
@@ -64,14 +89,14 @@ module ProbabilityToFriendlyString
|
|
64
89
|
left = @@fractionsData.length - 1
|
65
90
|
end
|
66
91
|
if (left == (@@fractionsData.length - 1) or (left >= 0 and f - @@fractionsData[left][0] < @@fractionsData[right][0] - f))
|
67
|
-
return FriendlyProbability.new @@fractionsData[left][1], @@fractionsData[left][2]
|
92
|
+
return FriendlyProbability.new @@fractionsData[left][1], @@fractionsData[left][2], friendlyDescription
|
68
93
|
else
|
69
|
-
return FriendlyProbability.new @@fractionsData[right][1], @@fractionsData[right][2]
|
94
|
+
return FriendlyProbability.new @@fractionsData[right][1], @@fractionsData[right][2], friendlyDescription
|
70
95
|
end
|
71
96
|
end
|
72
97
|
|
73
98
|
def to_s
|
74
|
-
return "#{@numerator}/#{@denominator} (text: \"#{@friendlyString}\")"
|
99
|
+
return "#{@numerator}/#{@denominator} (text: \"#{@friendlyString}\", description: \"#{@friendlyDescription}\")"
|
75
100
|
end
|
76
101
|
|
77
102
|
def <=>(another_friendly_string)
|
@@ -85,7 +110,12 @@ module ProbabilityToFriendlyString
|
|
85
110
|
elsif self.denominator > another_friendly_string.denominator
|
86
111
|
1
|
87
112
|
end
|
88
|
-
if self.
|
113
|
+
if self.friendlyDescription < another_friendly_string.friendlyDescription
|
114
|
+
-1
|
115
|
+
elsif self.friendlyDescription > another_friendly_string.friendlyDescription
|
116
|
+
1
|
117
|
+
end
|
118
|
+
if self.friendlyString < another_friendly_string.friendlyString
|
89
119
|
-1
|
90
120
|
elsif self.friendlyString > another_friendly_string.friendlyString
|
91
121
|
1
|
@@ -19,14 +19,36 @@ class FriendlyProbabilityTests < Test::Unit::TestCase
|
|
19
19
|
parts = line.split(',')
|
20
20
|
case parts.length
|
21
21
|
when 3
|
22
|
-
expected = ProbabilityToFriendlyString::FriendlyProbability.new parts[1].to_i, parts[2].to_i
|
22
|
+
expected = ProbabilityToFriendlyString::FriendlyProbability.new parts[1].to_i, parts[2].to_i, nil
|
23
23
|
when 4
|
24
|
-
expected = ProbabilityToFriendlyString::FriendlyProbability.new parts[1].to_i, parts[2].to_i, parts[3]
|
24
|
+
expected = ProbabilityToFriendlyString::FriendlyProbability.new parts[1].to_i, parts[2].to_i, nil, parts[3]
|
25
25
|
else
|
26
26
|
flunk "Line badly formatted: #{line} (line #{lineNumber})"
|
27
27
|
end
|
28
28
|
actual = ProbabilityToFriendlyString::FriendlyProbability.fromProbability(parts[0].to_f)
|
29
|
-
assert_equal expected, actual, "
|
29
|
+
assert_equal expected.numerator, actual.numerator, "Numerator, called on #{parts[0]} (line #{lineNumber})"
|
30
|
+
assert_equal expected.denominator, actual.denominator, "Denominator, called on #{parts[0]} (line #{lineNumber})"
|
31
|
+
assert_equal expected.friendlyString, actual.friendlyString, "Denominator, called on #{parts[0]} (line #{lineNumber})"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
def test_all_cases_friendly_probability
|
35
|
+
directory = File.absolute_path(File.dirname(__FILE__))
|
36
|
+
path = File.join(directory, 'testCases.friendlyDescription.txt')
|
37
|
+
while !File.exists?(path)
|
38
|
+
directory = File.dirname(directory)
|
39
|
+
path = File.join(directory, 'testCases.friendlyDescription.txt')
|
40
|
+
end
|
41
|
+
lineNumber = 0
|
42
|
+
File.open(path).each do |fullLine|
|
43
|
+
lineNumber += 1
|
44
|
+
line = fullLine.strip
|
45
|
+
if (line.start_with?('#') or line.empty?)
|
46
|
+
next
|
47
|
+
end
|
48
|
+
parts = line.split(',')
|
49
|
+
expected = parts[1]
|
50
|
+
actual = ProbabilityToFriendlyString::FriendlyProbability.fromProbability(parts[0].to_f)
|
51
|
+
assert_equal expected, actual.friendlyDescription, "FriendlyDescription, called on #{parts[0]} (line #{lineNumber})"
|
30
52
|
end
|
31
53
|
end
|
32
54
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: probability_to_friendly_string
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Greg Stoll
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Make probabilities more intuitive by converting them to odds (i.e. 72.3%
|
14
14
|
becomes "5 in 7")
|
@@ -39,7 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
39
39
|
version: '0'
|
40
40
|
requirements: []
|
41
41
|
rubyforge_project:
|
42
|
-
rubygems_version: 2.
|
42
|
+
rubygems_version: 2.7.6
|
43
43
|
signing_key:
|
44
44
|
specification_version: 4
|
45
45
|
summary: Make probabilities more intuitive by converting them to odds
|