File tree 3 files changed +20
-44
lines changed
3 files changed +20
-44
lines changed Original file line number Diff line number Diff line change 17
17
pred = model . prediction
18
18
if pred
19
19
puts model
20
- puts format ( "Predicted: %s %.2f\t Got: %s\t %s" ,
21
- pred [ :best_key ] ,
22
- pred [ :best_pct ] * 100 ,
20
+ puts format ( "Predicted: %s Got: %s\t %s" ,
21
+ pred ,
23
22
c ,
24
- pred [ :best_key ] == c ? 'CORRECT ' : 'INCORRECT' )
23
+ c == pred ? 'CORRECT ' : 'INCORRECT' )
25
24
26
25
end
27
26
model . update ( c )
Original file line number Diff line number Diff line change @@ -39,29 +39,6 @@ def full?
39
39
# adds a hash to track next-letter frequences,
40
40
# and tracks its record of predictions
41
41
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
-
65
42
attr_reader :ring , :freq , :pred
66
43
67
44
def initialize ( limit = 5 )
@@ -86,27 +63,33 @@ def percentage
86
63
end
87
64
88
65
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
90
75
end
91
76
92
- def update ( val )
77
+ def update ( char )
93
78
# only make predictions once the ring is full
94
79
if @ring . full?
95
80
buf = @ring . to_s
96
81
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
102
85
103
86
# update @freq
104
87
@freq [ buf ] ||= Hash . new ( 0 )
105
- @freq [ buf ] [ val ] += 1
88
+ @freq [ buf ] [ char ] += 1
106
89
end
107
90
108
91
# update @ring
109
- @ring . update ( val )
92
+ @ring . update ( char )
110
93
self
111
94
end
112
95
Original file line number Diff line number Diff line change 78
78
expect ( m . ring . to_s ) . must_equal '111'
79
79
pred = m . prediction
80
80
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'
85
82
86
83
m . accept ( '2' )
87
84
expect ( m . ring . to_s ) . must_equal '112'
91
88
expect ( m . ring . to_s ) . must_equal '112'
92
89
pred = m . prediction
93
90
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'
98
92
end
99
93
end
100
94
end
You can’t perform that action at this time.
0 commit comments