-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestLetters.js
More file actions
115 lines (99 loc) · 2.04 KB
/
testLetters.js
File metadata and controls
115 lines (99 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// get wilson
var wilson = require('./wilson.js')();
/**
* Helper functions for running tests and outputting results
*/
function test(name, inputs, targets, report) {
console.log('');
console.log('************************************************************');
console.log('Running "' + name + '" test...');
console.log(' Learning...');
console.log(' Inputs: ' + JSON.stringify(inputs.slice(0, 5)) + '...');
console.log(' Targets: ' + JSON.stringify(targets.slice(0, 5)) + '...');
wilson.classify(inputs, targets, report);
}
function predict(input, expected) {
var p = wilson.predict(input);
console.log('Predicting...');
console.log(' Input: ' + JSON.stringify(input.slice(0, 5)) + '...');
console.log(' Expected: ' + expected);
console.log(' Output: ' + p.best.label + ' (' + ((p.best.score * 100).toFixed(2)) + '% confidence)');
}
/**
* Letters.
*
* - Imagine these # and . represent black and white pixels.
*/
var a = character(
'.#####.' +
'#.....#' +
'#.....#' +
'#######' +
'#.....#' +
'#.....#' +
'#.....#'
);
var b = character(
'######.' +
'#.....#' +
'#.....#' +
'######.' +
'#.....#' +
'#.....#' +
'######.'
);
var c = character(
'#######' +
'#......' +
'#......' +
'#......' +
'#......' +
'#......' +
'#######'
);
/**
* Learn the letters A through C.
*/
test('OCR: Classification', [
a,
b,
c
], [
'a',
'b',
'c'
], true);
/**
* Predict the letter C, even with a pixel off.
*/
predict(character(
'.######' +
'#......' +
'#......' +
'#.#....' +
'#......' +
'##.....' +
'######.'
), 'c');
/**
* Turn the # into 1s and . into 0s.
*/
function character(string) {
return string
.trim()
.split('')
.map(integer);
function integer(symbol) {
if ('#' === symbol) return 1;
if ('.' === symbol) return 0;
}
}
/**
* Map letter to a number.
*/
function map(letter) {
if (letter === 'a') return [ 0.1 ];
if (letter === 'b') return [ 0.3 ];
if (letter === 'c') return [ 0.5 ];
return 0;
}