Skip to content

Commit 01a3a2c

Browse files
committed
get rid of Model.summarize and use Model#prediction
1 parent 7db19db commit 01a3a2c

File tree

3 files changed

+20
-44
lines changed

3 files changed

+20
-44
lines changed

examples/oracle.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
pred = model.prediction
1818
if pred
1919
puts model
20-
puts format("Predicted: %s %.2f\tGot: %s\t%s",
21-
pred[:best_key],
22-
pred[:best_pct] * 100,
20+
puts format("Predicted: %s Got: %s\t%s",
21+
pred,
2322
c,
24-
pred[:best_key] == c ? 'CORRECT ' : 'INCORRECT')
23+
c == pred ? 'CORRECT ' : 'INCORRECT')
2524

2625
end
2726
model.update(c)

lib/compsci/oracle.rb

+15-32
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,6 @@ def full?
3939
# adds a hash to track next-letter frequences,
4040
# and tracks its record of predictions
4141
class Model
42-
def self.summarize(hsh)
43-
best_key = nil
44-
best_val = 0
45-
best_pct = 0.0
46-
47-
total = hsh.values.sum
48-
pct = {}
49-
50-
hsh.each { |k, v|
51-
if v > best_val
52-
pct[k] = (v / total.to_f).round(4)
53-
best_key = k
54-
best_val = v
55-
best_pct = pct[k]
56-
end
57-
}
58-
59-
{ top: pct.sort_by { |k, v| -1 * v }.take(3).to_h,
60-
best_key: best_key,
61-
best_val: best_val,
62-
best_pct: best_pct, }
63-
end
64-
6542
attr_reader :ring, :freq, :pred
6643

6744
def initialize(limit = 5)
@@ -86,27 +63,33 @@ def percentage
8663
end
8764

8865
def prediction
89-
h = @freq[@ring.to_s] and Model.summarize(h)
66+
highest = -1
67+
best = nil
68+
@freq[@ring.to_s]&.each { |val, count|
69+
if count > highest
70+
best = val
71+
highest = count
72+
end
73+
}
74+
best
9075
end
9176

92-
def update(val)
77+
def update(char)
9378
# only make predictions once the ring is full
9479
if @ring.full?
9580
buf = @ring.to_s
9681

97-
# update @pred if we've seen this sequence before
98-
if (h = @freq[buf])
99-
pred = Model.summarize(h)
100-
@pred[(val == pred[:best_key]) ? :correct : :incorrect] += 1
101-
end
82+
# update @pred if we have a prediction
83+
pred = self.prediction
84+
@pred[(char == pred) ? :correct : :incorrect] += 1 if pred
10285

10386
# update @freq
10487
@freq[buf] ||= Hash.new(0)
105-
@freq[buf][val] += 1
88+
@freq[buf][char] += 1
10689
end
10790

10891
# update @ring
109-
@ring.update(val)
92+
@ring.update(char)
11093
self
11194
end
11295

test/oracle.rb

+2-8
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@
7878
expect(m.ring.to_s).must_equal '111'
7979
pred = m.prediction
8080
expect(pred).wont_be_nil
81-
expect(pred[:best_key]).must_equal '1'
82-
expect(pred[:best_val]).must_equal 1
83-
expect(pred[:best_pct]).must_equal 1.0
84-
expect(pred[:top]['1']).must_equal 1.0
81+
expect(pred).must_equal '1'
8582

8683
m.accept('2')
8784
expect(m.ring.to_s).must_equal '112'
@@ -91,10 +88,7 @@
9188
expect(m.ring.to_s).must_equal '112'
9289
pred = m.prediction
9390
expect(pred).wont_be_nil
94-
expect(pred[:best_key]).must_equal '3'
95-
expect(pred[:best_val]).must_equal 1
96-
expect(pred[:best_pct]).must_equal 1.0
97-
expect(pred[:top]['3']).must_equal 1.0
91+
expect(pred).must_equal '3'
9892
end
9993
end
10094
end

0 commit comments

Comments
 (0)