hunch 0.2.0 → 0.3.0
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.
- checksums.yaml +4 -4
- data/README.md +11 -8
- data/lib/hunch/backends/stub.rb +2 -2
- data/lib/hunch/configuration.rb +2 -2
- data/lib/hunch/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 385258ecd737c836bd9b677e023cbc5ffc653770ee046aba43aeeac3225c8cc2
|
|
4
|
+
data.tar.gz: 335bd7011b904736715d5490ca56645ce9a012faf7aff0fd485929d8ec5e5cf3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 05473ca9fbca95957fecd251d2dc53ee239f5e12b4b579846dbccf20c92f78a36ee0d4697b5a410ef89ddf406829e85bcead5aa0361307e32a3eb97984f9fd98
|
|
7
|
+
data.tar.gz: 9e50657e98f6e290fe96f08e161416ead241aae674dad5a9862c38eaac60232fc3fb314f60ae0cbee342f3d51b71fa2e5d8a0ce7cfb2eaaafa9411cb3fb08ca0
|
data/README.md
CHANGED
|
@@ -7,7 +7,7 @@ three moves for judgment calls. Every question is a conditional probability —
|
|
|
7
7
|
_how likely is this, given that_ — and the API reads that way:
|
|
8
8
|
|
|
9
9
|
```ruby
|
|
10
|
-
if Hunch.
|
|
10
|
+
if Hunch.almost_certainly?("this order is fraudulent", given: order.attributes)
|
|
11
11
|
order.hold!
|
|
12
12
|
end
|
|
13
13
|
```
|
|
@@ -40,15 +40,18 @@ Hunch.chance("written by a real human, not spam", given: bio) # => 0.87
|
|
|
40
40
|
Named levels collapse it into predicates:
|
|
41
41
|
|
|
42
42
|
```ruby
|
|
43
|
-
Hunch.
|
|
44
|
-
Hunch.likely?("fraudulent", given: order)
|
|
45
|
-
Hunch.
|
|
46
|
-
Hunch.
|
|
43
|
+
Hunch.possibly?("fraudulent", given: order) # chance >= 0.25
|
|
44
|
+
Hunch.likely?("fraudulent", given: order) # chance >= 0.5
|
|
45
|
+
Hunch.probably?("fraudulent", given: order) # chance >= 0.75
|
|
46
|
+
Hunch.almost_certainly?("fraudulent", given: order) # chance >= 0.93
|
|
47
47
|
|
|
48
48
|
Hunch.configure { |c| c.levels[:paranoid] = 0.99 }
|
|
49
49
|
Hunch.paranoid?("fraudulent", given: order) # chance >= 0.99
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
+
Configure built-in thresholds with the matching level names, for example
|
|
53
|
+
`Hunch.configure { |c| c.levels[:probably] = 0.8 }`.
|
|
54
|
+
|
|
52
55
|
**`pick`** answers _which_:
|
|
53
56
|
|
|
54
57
|
```ruby
|
|
@@ -77,13 +80,13 @@ Several questions about one piece of state cost one API call:
|
|
|
77
80
|
|
|
78
81
|
```ruby
|
|
79
82
|
result = Hunch.decide(given: mail.raw_source) do |q|
|
|
80
|
-
q.
|
|
83
|
+
q.probably? :urgent, "does this convey urgency?"
|
|
81
84
|
q.pick :team, billing: "payments", technical: "bugs", sales: "pricing"
|
|
82
85
|
q.rate :mood, :calm, :frustrated, :livid
|
|
83
86
|
end
|
|
84
87
|
|
|
85
88
|
result.urgent # => 0.92
|
|
86
|
-
result.urgent? # => true,
|
|
89
|
+
result.urgent? # => true, at or above :probably
|
|
87
90
|
result.team # => :technical
|
|
88
91
|
result.team_probabilities # => { billing: 0.08, technical: 0.85, sales: 0.07 }
|
|
89
92
|
result.mood.level # => :livid
|
|
@@ -117,7 +120,7 @@ class Signup < ApplicationRecord
|
|
|
117
120
|
|
|
118
121
|
def bio_reads_like_a_human
|
|
119
122
|
return if bio.blank?
|
|
120
|
-
return if Hunch.
|
|
123
|
+
return if Hunch.probably?("a genuine human bio, not spam or keyword stuffing", given: bio)
|
|
121
124
|
|
|
122
125
|
errors.add(:bio, "reads like spam")
|
|
123
126
|
rescue Hunch::APIError
|
data/lib/hunch/backends/stub.rb
CHANGED
|
@@ -39,7 +39,7 @@ module Hunch
|
|
|
39
39
|
when true then 1.0
|
|
40
40
|
when false then 0.0
|
|
41
41
|
when Numeric then value.to_f
|
|
42
|
-
else raise ArgumentError, "
|
|
42
|
+
else raise ArgumentError, "chance stub must be true, false, or a probability"
|
|
43
43
|
end
|
|
44
44
|
{ "type" => "noul", "noul" => probability }
|
|
45
45
|
end
|
|
@@ -49,7 +49,7 @@ module Hunch
|
|
|
49
49
|
case value
|
|
50
50
|
when Symbol then question.options.keys.to_h { |option| [option.to_s, option == value ? 1.0 : 0.0] }
|
|
51
51
|
when Hash then value.transform_keys(&:to_s).transform_values(&:to_f)
|
|
52
|
-
else raise ArgumentError, "
|
|
52
|
+
else raise ArgumentError, "pick stub must be a symbol or a probabilities hash"
|
|
53
53
|
end
|
|
54
54
|
winner = probabilities.max_by { |_, p| p }.first
|
|
55
55
|
{ "type" => "choice", "choice" => winner, "probabilities" => probabilities, "confidence" => probabilities[winner] }
|
data/lib/hunch/configuration.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
module Hunch
|
|
2
2
|
class Configuration
|
|
3
|
-
LEVELS = {
|
|
3
|
+
LEVELS = { possibly: 0.25, likely: 0.5, probably: 0.75, almost_certainly: 0.93 }.freeze
|
|
4
4
|
|
|
5
5
|
attr_accessor :api_key, :model, :url, :timeout, :open_timeout, :max_retries, :levels
|
|
6
6
|
attr_writer :backend
|
|
@@ -24,7 +24,7 @@ module Hunch
|
|
|
24
24
|
raise ConfigurationError, "unknown level #{value.inspect}; known levels: #{levels.keys.join(", ")}"
|
|
25
25
|
end
|
|
26
26
|
else
|
|
27
|
-
raise ArgumentError, "
|
|
27
|
+
raise ArgumentError, "level must be a number or a level name"
|
|
28
28
|
end
|
|
29
29
|
end
|
|
30
30
|
|
data/lib/hunch/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: hunch
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Carl Dawson
|
|
@@ -9,7 +9,7 @@ bindir: bin
|
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
12
|
-
description: '
|
|
12
|
+
description: 'chance, pick, and rate: three primitives that let Ruby branch on judgment
|
|
13
13
|
calls, answered by TypeSafe''s Jev System One model or any backend you plug in.'
|
|
14
14
|
email:
|
|
15
15
|
- carldawson@hey.com
|