millirec 0.1.1 → 0.2.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.
Files changed (89) hide show
  1. package/CHANGELOG.md +101 -0
  2. package/README.md +286 -51
  3. package/dist/char-cnn/cnn-web.wasm +0 -0
  4. package/dist/char-cnn/core.d.ts +15 -1
  5. package/dist/char-cnn/core.js +214 -100
  6. package/dist/char-cnn/index.d.ts +1 -1
  7. package/dist/char-cnn/index.js +267 -137
  8. package/dist/char-cnn/millirec-char-cnn-core.min.js +1 -1
  9. package/dist/char-cnn/millirec-char-cnn.min.js +1 -1
  10. package/dist/char-cnn/options/accurate/tiny.head.bin +0 -0
  11. package/dist/char-cnn/options/accurate.js +6 -6
  12. package/dist/char-cnn/options/accurate.json +5 -5
  13. package/dist/char-cnn/options/all/all-base.head.bin +0 -0
  14. package/dist/char-cnn/options/all/all-base.q4.bin +0 -0
  15. package/dist/char-cnn/options/all/all-tiny.bin +0 -0
  16. package/dist/char-cnn/options/all/all-tiny.head.bin +0 -0
  17. package/dist/char-cnn/options/all-fast/all-base-p25.bin +0 -0
  18. package/dist/char-cnn/options/all-fast/all-base-p25.head.bin +0 -0
  19. package/dist/char-cnn/options/all-fast/all-tiny.bin +0 -0
  20. package/dist/char-cnn/options/all-fast/all-tiny.head.bin +0 -0
  21. package/dist/char-cnn/options/all-fast.js +17 -0
  22. package/dist/char-cnn/options/all-fast.json +15 -0
  23. package/dist/char-cnn/options/all-small/all-tiny-qat4.head.bin +0 -0
  24. package/dist/char-cnn/options/all-small/all-tiny-qat4.q4.bin +0 -0
  25. package/dist/char-cnn/options/all-small.js +11 -0
  26. package/dist/char-cnn/options/all-small.json +9 -0
  27. package/dist/char-cnn/options/all.js +17 -0
  28. package/dist/char-cnn/options/all.json +15 -0
  29. package/dist/char-cnn/options/digits/digits-base.head.bin +0 -0
  30. package/dist/char-cnn/options/digits/digits-base.q4.bin +0 -0
  31. package/dist/char-cnn/options/digits/digits-tiny.bin +0 -0
  32. package/dist/char-cnn/options/digits/digits-tiny.head.bin +0 -0
  33. package/dist/char-cnn/options/digits-fast/digits-base-p25.bin +0 -0
  34. package/dist/char-cnn/options/digits-fast/digits-base-p25.head.bin +0 -0
  35. package/dist/char-cnn/options/digits-fast/digits-tiny.bin +0 -0
  36. package/dist/char-cnn/options/digits-fast/digits-tiny.head.bin +0 -0
  37. package/dist/char-cnn/options/digits-fast.js +17 -0
  38. package/dist/char-cnn/options/digits-fast.json +15 -0
  39. package/dist/char-cnn/options/digits-small/digits-tiny-qat4.head.bin +0 -0
  40. package/dist/char-cnn/options/digits-small/digits-tiny-qat4.q4.bin +0 -0
  41. package/dist/char-cnn/options/digits-small.js +11 -0
  42. package/dist/char-cnn/options/digits-small.json +9 -0
  43. package/dist/char-cnn/options/digits.js +17 -0
  44. package/dist/char-cnn/options/digits.json +15 -0
  45. package/dist/char-cnn/options/fast/base-p25.head.bin +0 -0
  46. package/dist/char-cnn/options/fast/tiny.bin +0 -0
  47. package/dist/char-cnn/options/fast/tiny.head.bin +0 -0
  48. package/dist/char-cnn/options/fast.js +6 -6
  49. package/dist/char-cnn/options/fast.json +5 -5
  50. package/dist/char-cnn/options/lowercase/lowercase-base.head.bin +0 -0
  51. package/dist/char-cnn/options/lowercase/lowercase-base.q4.bin +0 -0
  52. package/dist/char-cnn/options/lowercase/lowercase-tiny.bin +0 -0
  53. package/dist/char-cnn/options/lowercase/lowercase-tiny.head.bin +0 -0
  54. package/dist/char-cnn/options/lowercase-fast/lowercase-base-p25.bin +0 -0
  55. package/dist/char-cnn/options/lowercase-fast/lowercase-base-p25.head.bin +0 -0
  56. package/dist/char-cnn/options/lowercase-fast/lowercase-tiny.bin +0 -0
  57. package/dist/char-cnn/options/lowercase-fast/lowercase-tiny.head.bin +0 -0
  58. package/dist/char-cnn/options/lowercase-fast.js +17 -0
  59. package/dist/char-cnn/options/lowercase-fast.json +15 -0
  60. package/dist/char-cnn/options/lowercase-small/lowercase-tiny-qat4.head.bin +0 -0
  61. package/dist/char-cnn/options/lowercase-small/lowercase-tiny-qat4.q4.bin +0 -0
  62. package/dist/char-cnn/options/lowercase-small.js +11 -0
  63. package/dist/char-cnn/options/lowercase-small.json +9 -0
  64. package/dist/char-cnn/options/lowercase.js +17 -0
  65. package/dist/char-cnn/options/lowercase.json +15 -0
  66. package/dist/char-cnn/options/small/tiny.head.bin +0 -0
  67. package/dist/char-cnn/options/small/tiny.q4.bin +0 -0
  68. package/dist/char-cnn/options/small.js +3 -3
  69. package/dist/char-cnn/options/small.json +2 -2
  70. package/dist/char-lite/core.d.ts +1 -0
  71. package/dist/char-lite/core.js +628 -0
  72. package/dist/char-lite/index.d.ts +28 -8
  73. package/dist/char-lite/index.js +459 -297
  74. package/dist/char-lite/millirec-char-lite.min.js +1 -1
  75. package/dist/char-lite/millirec.wasm +0 -0
  76. package/dist/char-lite/sets.json +25 -29
  77. package/dist/index.d.ts +35 -0
  78. package/dist/index.js +88 -0
  79. package/dist/overlap/millirec-overlap.min.js +7 -7
  80. package/dist/overlap/millirec.wasm +0 -0
  81. package/dist/pad/index.d.ts +8 -0
  82. package/dist/pad/index.js +482 -0
  83. package/package.json +30 -8
  84. package/dist/char-cnn/base-p25.head.bin +0 -0
  85. package/dist/char-cnn/tiny.head.bin +0 -0
  86. /package/dist/char-cnn/{base.head.bin → options/accurate/base.head.bin} +0 -0
  87. /package/dist/char-cnn/{base.q4.bin → options/accurate/base.q4.bin} +0 -0
  88. /package/dist/char-cnn/{tiny.bin → options/accurate/tiny.bin} +0 -0
  89. /package/dist/char-cnn/{base-p25.bin → options/fast/base-p25.bin} +0 -0
@@ -114,69 +114,102 @@ function parseHead(buf) {
114
114
  return readLinear(reader(buf));
115
115
  }
116
116
  function conv3(c, inp, res, out) {
117
- const { cout, cin, k, w, bias } = c;
118
- const plane = res * res, half = k - 1 >> 1, per = cin * k * k;
117
+ const { cout, cin, w, bias } = c;
118
+ const plane = res * res, rm1 = res - 1;
119
119
  for (let co = 0; co < cout; co++) {
120
- const ob = co * plane, b = bias[co], wb = co * per;
121
- for (let y = 0; y < res; y++) {
122
- for (let x = 0; x < res; x++) {
123
- let s = b;
124
- for (let ci = 0; ci < cin; ci++) {
125
- const ib = ci * plane, wc = wb + ci * k * k;
126
- for (let ky = 0; ky < k; ky++) {
127
- const yy = y + ky - half;
128
- if (yy < 0 || yy >= res) continue;
129
- const row = ib + yy * res, wr = wc + ky * k;
130
- for (let kx = 0; kx < k; kx++) {
131
- const xx = x + kx - half;
132
- if (xx < 0 || xx >= res) continue;
133
- s += w[wr + kx] * inp[row + xx];
134
- }
135
- }
120
+ const ob = co * plane, b = bias[co], wb = co * cin * 9;
121
+ out.fill(b, ob, ob + plane);
122
+ for (let ci = 0; ci < cin; ci++) {
123
+ const ib = ci * plane, wc = wb + ci * 9;
124
+ const w0 = w[wc], w1 = w[wc + 1], w2 = w[wc + 2];
125
+ const w3 = w[wc + 3], w4 = w[wc + 4], w5 = w[wc + 5];
126
+ const w6 = w[wc + 6], w7 = w[wc + 7], w8 = w[wc + 8];
127
+ for (let y = 1; y < rm1; y++) {
128
+ const r0 = ib + (y - 1) * res, r1 = ib + y * res, r2 = ib + (y + 1) * res, orow = ob + y * res;
129
+ out[orow] += w1 * inp[r0] + w2 * inp[r0 + 1] + w4 * inp[r1] + w5 * inp[r1 + 1] + w7 * inp[r2] + w8 * inp[r2 + 1];
130
+ for (let x = 1; x < rm1; x++) {
131
+ out[orow + x] += w0 * inp[r0 + x - 1] + w1 * inp[r0 + x] + w2 * inp[r0 + x + 1] + w3 * inp[r1 + x - 1] + w4 * inp[r1 + x] + w5 * inp[r1 + x + 1] + w6 * inp[r2 + x - 1] + w7 * inp[r2 + x] + w8 * inp[r2 + x + 1];
132
+ }
133
+ out[orow + rm1] += w0 * inp[r0 + rm1 - 1] + w1 * inp[r0 + rm1] + w3 * inp[r1 + rm1 - 1] + w4 * inp[r1 + rm1] + w6 * inp[r2 + rm1 - 1] + w7 * inp[r2 + rm1];
134
+ }
135
+ {
136
+ const r1 = ib, r2 = ib + res, orow = ob;
137
+ out[orow] += w4 * inp[r1] + w5 * inp[r1 + 1] + w7 * inp[r2] + w8 * inp[r2 + 1];
138
+ for (let x = 1; x < rm1; x++) {
139
+ out[orow + x] += w3 * inp[r1 + x - 1] + w4 * inp[r1 + x] + w5 * inp[r1 + x + 1] + w6 * inp[r2 + x - 1] + w7 * inp[r2 + x] + w8 * inp[r2 + x + 1];
136
140
  }
137
- out[ob + y * res + x] = s > 0 ? s : 0;
141
+ out[orow + rm1] += w3 * inp[r1 + rm1 - 1] + w4 * inp[r1 + rm1] + w6 * inp[r2 + rm1 - 1] + w7 * inp[r2 + rm1];
142
+ }
143
+ {
144
+ const r0 = ib + (rm1 - 1) * res, r1 = ib + rm1 * res, orow = ob + rm1 * res;
145
+ out[orow] += w1 * inp[r0] + w2 * inp[r0 + 1] + w4 * inp[r1] + w5 * inp[r1 + 1];
146
+ for (let x = 1; x < rm1; x++) {
147
+ out[orow + x] += w0 * inp[r0 + x - 1] + w1 * inp[r0 + x] + w2 * inp[r0 + x + 1] + w3 * inp[r1 + x - 1] + w4 * inp[r1 + x] + w5 * inp[r1 + x + 1];
148
+ }
149
+ out[orow + rm1] += w0 * inp[r0 + rm1 - 1] + w1 * inp[r0 + rm1] + w3 * inp[r1 + rm1 - 1] + w4 * inp[r1 + rm1];
138
150
  }
139
151
  }
152
+ const end = ob + plane;
153
+ for (let i = ob; i < end; i++) if (out[i] < 0) out[i] = 0;
140
154
  }
141
155
  }
142
156
  function pool2(inp, res, ch, out) {
143
157
  const r2 = res >> 1;
144
158
  for (let c = 0; c < ch; c++) {
145
159
  const ib = c * res * res, ob = c * r2 * r2;
146
- for (let y = 0; y < r2; y++) for (let x = 0; x < r2; x++) {
147
- const i0 = ib + 2 * y * res + 2 * x;
148
- const a = inp[i0], b = inp[i0 + 1], d = inp[i0 + res], e = inp[i0 + res + 1];
149
- out[ob + y * r2 + x] = Math.max(a, b, d, e);
160
+ for (let y = 0; y < r2; y++) {
161
+ const r0 = ib + (y << 1) * res, r1 = r0 + res, orow = ob + y * r2;
162
+ for (let x = 0; x < r2; x++) {
163
+ const i0 = r0 + (x << 1), i1 = r1 + (x << 1);
164
+ let m = inp[i0], b = inp[i0 + 1], d = inp[i1], e = inp[i1 + 1];
165
+ if (b > m) m = b;
166
+ if (d > m) m = d;
167
+ if (e > m) m = e;
168
+ out[orow + x] = m;
169
+ }
150
170
  }
151
171
  }
152
172
  }
153
173
  function embed(model, img) {
154
174
  const S = model.size;
155
- let cur = Float32Array.from(img), res = S, ch = 1;
175
+ if (!model._bufA) {
176
+ let maxLen = S * S, r = S;
177
+ for (const c of model.convs) {
178
+ if (c.cout * r * r > maxLen) maxLen = c.cout * r * r;
179
+ if (c.pool) r >>= 1;
180
+ }
181
+ model._bufA = new Float32Array(maxLen);
182
+ model._bufB = new Float32Array(maxLen);
183
+ model._gap = new Float32Array(model.lin.in);
184
+ }
185
+ let cur = model._bufA, nxt = model._bufB, res = S, ch = 1;
186
+ cur.set(img);
156
187
  for (const c of model.convs) {
157
- const out = new Float32Array(c.cout * res * res);
158
- conv3(c, cur, res, out);
159
- cur = out;
188
+ conv3(c, cur, res, nxt);
189
+ let t = cur;
190
+ cur = nxt;
191
+ nxt = t;
160
192
  ch = c.cout;
161
193
  if (c.pool) {
162
- const p = new Float32Array(ch * (res >> 1) * (res >> 1));
163
- pool2(cur, res, ch, p);
164
- cur = p;
194
+ pool2(cur, res, ch, nxt);
195
+ t = cur;
196
+ cur = nxt;
197
+ nxt = t;
165
198
  res >>= 1;
166
199
  }
167
200
  }
168
- const plane = res * res;
169
- const gap = new Float32Array(ch);
201
+ const plane = res * res, invPlane = 1 / plane;
202
+ const gap = model._gap;
170
203
  for (let c = 0; c < ch; c++) {
171
- let s = 0;
172
- for (let j = 0; j < plane; j++) s += cur[c * plane + j];
173
- gap[c] = s / plane;
204
+ let s = 0, off = c * plane;
205
+ for (let j = 0; j < plane; j++) s += cur[off + j];
206
+ gap[c] = s * invPlane;
174
207
  }
175
- const { lin } = model;
208
+ const { lin } = model, lw = lin.w, lb = lin.bias, linIn = lin.in;
176
209
  const e = new Float32Array(lin.out);
177
210
  for (let o = 0; o < lin.out; o++) {
178
- let s = lin.bias[o];
179
- for (let j = 0; j < lin.in; j++) s += lin.w[o * lin.in + j] * gap[j];
211
+ let s = lb[o], off = o * linIn;
212
+ for (let j = 0; j < linIn; j++) s += lw[off + j] * gap[j];
180
213
  e[o] = s;
181
214
  }
182
215
  return e;
@@ -213,7 +246,8 @@ function softmax(z) {
213
246
  for (let i = 0; i < z.length; i++) out[i] /= s;
214
247
  return out;
215
248
  }
216
- function drawStrokes(ctx, strokes, box = 120, lw = 7, pad = 10) {
249
+ var _planeBuf = null;
250
+ function strokePlane(strokes, box = 120, lw = 7, pad = 10) {
217
251
  let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
218
252
  for (const s of strokes) for (const p of s) {
219
253
  if (p.x < minX) minX = p.x;
@@ -221,73 +255,86 @@ function drawStrokes(ctx, strokes, box = 120, lw = 7, pad = 10) {
221
255
  if (p.y < minY) minY = p.y;
222
256
  if (p.y > maxY) maxY = p.y;
223
257
  }
224
- const sc = box / Math.max(maxX - minX, maxY - minY, 1);
225
258
  const W = box + 2 * pad;
226
- ctx.fillStyle = "#fff";
227
- ctx.fillRect(0, 0, W, W);
228
- ctx.strokeStyle = "#000";
229
- ctx.lineWidth = lw;
230
- ctx.lineCap = "round";
231
- ctx.lineJoin = "round";
259
+ const ink = _planeBuf && _planeBuf.length === W * W ? _planeBuf : _planeBuf = new Float32Array(W * W);
260
+ ink.fill(0);
261
+ if (!(minX <= maxX)) return { ink, W, iMinX: 0, iMaxX: -1, iMinY: 0, iMaxY: -1 };
262
+ const sc = box / Math.max(maxX - minX, maxY - minY, 1);
263
+ const r = lw * 0.5, rOut = r + 0.5, rIn = r - 0.5, rOut2 = rOut * rOut, rIn2 = rIn * rIn;
264
+ let iMinX = W, iMaxX = -1, iMinY = W, iMaxY = -1;
265
+ const drawSeg = (ax, ay, bx, by) => {
266
+ const x0 = Math.max(0, Math.floor(Math.min(ax, bx) - rOut)), x1 = Math.min(W - 1, Math.ceil(Math.max(ax, bx) + rOut));
267
+ const y0 = Math.max(0, Math.floor(Math.min(ay, by) - rOut)), y1 = Math.min(W - 1, Math.ceil(Math.max(ay, by) + rOut));
268
+ const vx = bx - ax, vy = by - ay, l2 = vx * vx + vy * vy, invL2 = l2 > 1e-6 ? 1 / l2 : 0;
269
+ for (let y = y0; y <= y1; y++) {
270
+ const py = y + 0.5 - ay, row = y * W;
271
+ for (let x = x0; x <= x1; x++) {
272
+ const cur = ink[row + x];
273
+ if (cur >= 1) continue;
274
+ const px = x + 0.5 - ax;
275
+ let t = (px * vx + py * vy) * invL2;
276
+ if (t < 0) t = 0;
277
+ else if (t > 1) t = 1;
278
+ const dx = px - t * vx, dy = py - t * vy, d2 = dx * dx + dy * dy;
279
+ if (d2 >= rOut2) continue;
280
+ const v = d2 <= rIn2 ? 1 : rOut - Math.sqrt(d2);
281
+ if (v > cur) {
282
+ ink[row + x] = v;
283
+ if (v > 0.15) {
284
+ if (x < iMinX) iMinX = x;
285
+ if (x > iMaxX) iMaxX = x;
286
+ if (y < iMinY) iMinY = y;
287
+ if (y > iMaxY) iMaxY = y;
288
+ }
289
+ }
290
+ }
291
+ }
292
+ };
232
293
  for (const s of strokes) {
233
- if (s.length < 2) continue;
234
- ctx.beginPath();
235
- ctx.moveTo((s[0].x - minX) * sc + pad, (s[0].y - minY) * sc + pad);
236
- for (let i = 1; i < s.length; i++) ctx.lineTo((s[i].x - minX) * sc + pad, (s[i].y - minY) * sc + pad);
237
- ctx.stroke();
294
+ if (!s.length) continue;
295
+ let ax = (s[0].x - minX) * sc + pad, ay = (s[0].y - minY) * sc + pad;
296
+ if (s.length === 1) {
297
+ drawSeg(ax, ay, ax, ay);
298
+ continue;
299
+ }
300
+ for (let i = 1; i < s.length; i++) {
301
+ const bx = (s[i].x - minX) * sc + pad, by = (s[i].y - minY) * sc + pad;
302
+ drawSeg(ax, ay, bx, by);
303
+ ax = bx;
304
+ ay = by;
305
+ }
238
306
  }
239
- return W;
307
+ return { ink, W, iMinX, iMaxX, iMinY, iMaxY };
240
308
  }
241
- function inkPlane(data, W, H) {
242
- const ink = new Float32Array(W * H);
243
- for (let i = 0, p = 0; i < ink.length; i++, p += 4) ink[i] = 1 - (data[p] * 0.299 + data[p + 1] * 0.587 + data[p + 2] * 0.114) / 255;
244
- return ink;
245
- }
246
-
247
- // ../../server/public/wordgpu/cnnprep.mjs
248
- var SIZE = 32;
249
- var INK = 24;
250
- function normalizeInk(ink, w, h, size = SIZE, inkBox = INK) {
251
- let minX = Infinity, maxX = -1, minY = Infinity, maxY = -1;
252
- for (let y = 0; y < h; y++) for (let x = 0; x < w; x++)
253
- if (ink[y * w + x] > 0.15) {
254
- if (x < minX) minX = x;
255
- if (x > maxX) maxX = x;
256
- if (y < minY) minY = y;
257
- if (y > maxY) maxY = y;
258
- }
309
+ function normalizePlane({ ink, W, iMinX, iMaxX, iMinY, iMaxY }, size = 32, inkBox = Math.round(size * 0.75)) {
259
310
  const out = new Float32Array(size * size);
260
- if (maxX < 0) return out;
261
- const cw = maxX - minX + 1, ch = maxY - minY + 1;
262
- const crop = new Float32Array(cw * ch);
263
- for (let y = 0; y < ch; y++) for (let x = 0; x < cw; x++) crop[y * cw + x] = ink[(y + minY) * w + (x + minX)];
264
- let peak = 0;
265
- for (let i = 0; i < crop.length; i++) if (crop[i] > peak) peak = crop[i];
266
- if (peak > 0.05 && peak < 0.999) for (let i = 0; i < crop.length; i++) crop[i] = Math.min(1, crop[i] / peak);
311
+ if (iMaxX < 0) return out;
312
+ const cw = iMaxX - iMinX + 1, ch = iMaxY - iMinY + 1;
267
313
  const s = inkBox / Math.max(ch, cw);
268
314
  const nh = Math.max(1, Math.round(ch * s)), nw = Math.max(1, Math.round(cw * s));
269
- const r = new Float32Array(nw * nh);
270
315
  const fx = cw / nw, fy = ch / nh;
316
+ const oy = size - nh >> 1, ox = size - nw >> 1;
271
317
  for (let y = 0; y < nh; y++) {
272
318
  const y0 = y * fy, y1 = (y + 1) * fy, iy0 = Math.floor(y0), iy1 = Math.min(ch, Math.ceil(y1));
319
+ const outRow = (y + oy) * size + ox;
273
320
  for (let x = 0; x < nw; x++) {
274
321
  const x0 = x * fx, x1 = (x + 1) * fx, ix0 = Math.floor(x0), ix1 = Math.min(cw, Math.ceil(x1));
275
322
  let ss = 0, wsum = 0;
276
323
  for (let yy = iy0; yy < iy1; yy++) {
277
324
  const wy = Math.min(y1, yy + 1) - Math.max(y0, yy);
278
325
  if (wy <= 0) continue;
326
+ const srcRow = (yy + iMinY) * W + iMinX;
279
327
  for (let xx = ix0; xx < ix1; xx++) {
280
328
  const wx = Math.min(x1, xx + 1) - Math.max(x0, xx);
281
329
  if (wx <= 0) continue;
282
- ss += crop[yy * cw + xx] * wx * wy;
283
- wsum += wx * wy;
330
+ const wxy = wx * wy;
331
+ ss += ink[srcRow + xx] * wxy;
332
+ wsum += wxy;
284
333
  }
285
334
  }
286
- r[y * nw + x] = wsum > 0 ? ss / wsum : 0;
335
+ out[outRow + x] = wsum > 0 ? ss / wsum : 0;
287
336
  }
288
337
  }
289
- const oy = size - nh >> 1, ox = size - nw >> 1;
290
- for (let y = 0; y < nh; y++) for (let x = 0; x < nw; x++) out[(y + oy) * size + x + ox] = r[y * nw + x];
291
338
  return out;
292
339
  }
293
340
 
@@ -328,6 +375,7 @@ async function fetchAsset(url2, { as = "json", what, base, recognizer }) {
328
375
  }
329
376
 
330
377
  // ../../library/src/char-cnn-core.js
378
+ var finitePoints = (strokes) => strokes.every((st) => Array.isArray(st) && st.every((p) => p && Number.isFinite(p.x) && Number.isFinite(p.y)));
331
379
  function scriptBase() {
332
380
  const src = typeof document !== "undefined" && document.currentScript && document.currentScript.src || "";
333
381
  return src ? src.slice(0, src.lastIndexOf("/") + 1) : "";
@@ -335,7 +383,15 @@ function scriptBase() {
335
383
  var ASSET_BASE = scriptBase();
336
384
  var fetchBuf = (url2, base) => fetchAsset(url2, { as: "buf", what: "a char-cnn model file", base, recognizer: "char-cnn" });
337
385
  var DESTROYED = "millirec: recognizer destroyed";
338
- var makeCanvas = (n) => typeof document !== "undefined" && document.createElement ? Object.assign(document.createElement("canvas"), { width: n, height: n }) : typeof OffscreenCanvas !== "undefined" ? new OffscreenCanvas(n, n) : null;
386
+ var TEACH_SHARPNESS = 18;
387
+ var unitOf = (e) => {
388
+ let n = 0;
389
+ for (let i = 0; i < e.length; i++) n += e[i] * e[i];
390
+ n = Math.sqrt(n) || 1;
391
+ const o = new Float32Array(e.length);
392
+ for (let i = 0; i < e.length; i++) o[i] = e[i] / n;
393
+ return o;
394
+ };
339
395
  var MillirecCharCnnCore = class {
340
396
  static async create(opts = {}) {
341
397
  const g = typeof window !== "undefined" ? window : globalThis;
@@ -346,6 +402,7 @@ var MillirecCharCnnCore = class {
346
402
  }
347
403
  const inst = new this(config);
348
404
  await inst.ready;
405
+ if (config.examples) await inst.teach(config.examples);
349
406
  return inst;
350
407
  }
351
408
  constructor(config) {
@@ -353,8 +410,6 @@ var MillirecCharCnnCore = class {
353
410
  this.base = assetBaseOf(config.assetBase || ASSET_BASE);
354
411
  this.classes = typeof config.classes === "string" ? config.classes.split("") : config.classes;
355
412
  this.stages = [];
356
- this.off = makeCanvas(140);
357
- this.octx = this.off ? this.off.getContext("2d", { willReadFrequently: true }) : null;
358
413
  this.ready = this.load();
359
414
  }
360
415
  async load() {
@@ -410,18 +465,19 @@ var MillirecCharCnnCore = class {
410
465
  this.stages.push(stage);
411
466
  }
412
467
  }
468
+ /** Which engine this is (createRecognizer() picks one). */
469
+ get engine() {
470
+ return "char-cnn";
471
+ }
413
472
  /** drawn strokes ([[{x,y},...],...]) -> the model input raster at `size`.
414
- * The canvas draw + readback is the dear part (~1 ms) and is shared by every
415
- * stage of one recognize() call; only the resample differs per size. */
473
+ * The 140x140 stroke plane is computed once per recognize() call and shared
474
+ * by every stage; only the resample differs per size. */
416
475
  raster(strokes, size) {
417
- if (!this.octx) throw new Error("millirec: char-cnn needs a canvas to draw strokes on (a document, or OffscreenCanvas in a worker)");
418
476
  if (this._planeFor !== strokes) {
419
- const W2 = drawStrokes(this.octx, strokes);
420
- this._plane = { ink: inkPlane(this.octx.getImageData(0, 0, W2, W2).data, W2, W2), W: W2 };
477
+ this._plane = strokePlane(strokes);
421
478
  this._planeFor = strokes;
422
479
  }
423
- const { ink, W } = this._plane;
424
- return normalizeInk(ink, W, W, size, Math.round(size * 0.75));
480
+ return normalizePlane(this._plane, size, Math.round(size * 0.75));
425
481
  }
426
482
  /**
427
483
  * -> [{char, prob} x3] (best first) with `.stage` = index of the model that decided, or null.
@@ -431,11 +487,12 @@ var MillirecCharCnnCore = class {
431
487
  */
432
488
  async recognize(strokes, { only = null } = {}) {
433
489
  strokes = asStrokes(strokes);
434
- if (!strokes || !strokes.length) return null;
490
+ if (!strokes || !strokes.length || !finitePoints(strokes)) return null;
435
491
  await this.ready;
436
492
  if (this.destroyed || !this.stages.length) return null;
437
493
  this._planeFor = null;
438
494
  const keep = only ? new Set(only) : null;
495
+ if (this.taught) return this.nearest(strokes, keep);
439
496
  let z = null, used = 0;
440
497
  for (let s = 0; s < this.stages.length; s++) {
441
498
  const st = this.stages[s];
@@ -467,9 +524,67 @@ var MillirecCharCnnCore = class {
467
524
  out.stage = used;
468
525
  return out;
469
526
  }
527
+ /** recognize() once teach() has run: the drawing against every taught example, in the last stage's feature space. */
528
+ nearest(strokes, keep) {
529
+ const last = this.stages.length - 1, st = this.stages[last];
530
+ const q = unitOf(st.embed(this.raster(strokes, st.size)));
531
+ const names = [...this.taught.keys()].filter((c) => !keep || keep.has(c));
532
+ const out = [];
533
+ if (names.length) {
534
+ const sim = names.map((c) => {
535
+ let b = -2;
536
+ for (const e of this.taught.get(c)) {
537
+ let d = 0;
538
+ for (let i = 0; i < q.length; i++) d += q[i] * e[i];
539
+ if (d > b) b = d;
540
+ }
541
+ return b;
542
+ });
543
+ const p = softmax(sim.map((v) => v * TEACH_SHARPNESS));
544
+ for (const i of [...p.keys()].sort((a, b) => p[b] - p[a]).slice(0, 3)) out.push({ char: names[i], prob: p[i] });
545
+ }
546
+ out.stage = last;
547
+ return out;
548
+ }
549
+ /**
550
+ * TEACH: read symbols of your own from a few drawings of each, with no training. `examples` is
551
+ * `{ name: [drawing, …] }` — names are any strings (a letter, 'star', an emoji), a drawing is strokes as for
552
+ * recognize(). Each drawing is kept as what the last stage's network makes of it; from then on recognize() answers
553
+ * AMONG THE TAUGHT NAMES ONLY, by the nearest example. Three drawings of each is a good start, five is better;
554
+ * draw the variants people really use. `{ add: true }` keeps what was taught before (new drawings of a known name
555
+ * are added to it). Returns { classes, n } or null (nothing usable). forget() goes back to the shipped classes.
556
+ * Works best on an option whose last stage is the larger network (`accurate`, the default, or `fast`).
557
+ */
558
+ async teach(examples, { add = false } = {}) {
559
+ await this.ready;
560
+ if (this.destroyed || !this.stages.length) return null;
561
+ const taught = add && this.taught ? new Map([...this.taught].map(([c, list]) => [c, list.slice()])) : /* @__PURE__ */ new Map();
562
+ for (const [name, list] of Object.entries(examples || {})) {
563
+ for (const drawing of list || []) {
564
+ const strokes = asStrokes(drawing);
565
+ if (!strokes || !strokes.length) continue;
566
+ const e = unitOf(await this.embedding(strokes));
567
+ if (this.destroyed) return null;
568
+ if (!taught.has(name)) taught.set(name, []);
569
+ taught.get(name).push(e);
570
+ }
571
+ }
572
+ if (!taught.size) {
573
+ if (!add) this.taught = null;
574
+ return null;
575
+ }
576
+ this.taught = taught;
577
+ return { classes: [...taught.keys()], n: [...taught.values()].reduce((a, l) => a + l.length, 0) };
578
+ }
579
+ /** Drop everything teach() learned: recognize() reads the shipped classes again. */
580
+ forget() {
581
+ this.taught = null;
582
+ }
470
583
  /** The last stage's (unit) embedding of drawn strokes — what the head reads. */
471
584
  async embedding(strokes) {
472
585
  strokes = asStrokes(strokes);
586
+ if (!strokes || !strokes.length) throw new TypeError("MillirecCharCnn: embedding() needs at least one stroke");
587
+ if (!finitePoints(strokes)) throw new TypeError("MillirecCharCnn: embedding() got a point that is not a finite number");
473
588
  await this.ready;
474
589
  if (this.destroyed || !this.stages.length) throw new Error(DESTROYED);
475
590
  this._planeFor = null;
@@ -479,7 +594,7 @@ var MillirecCharCnnCore = class {
479
594
  /**
480
595
  * PERSONALIZE: refit the last stage's head (a softmax over the frozen embedding) on the player's own samples
481
596
  * (`{ 'A': [strokes…], … }`), anchored to the shipped head — the anchor's pull is prior/n, so a few samples barely move
482
- * a letter and many take it over (Rob, Sep 24 2026: "gradually add and then replace with the player's own"). Plain
597
+ * a letter and many take it over. Plain
483
598
  * gradient descent on the mean cross-entropy: a few hundred steps over a few hundred samples is ~100 ms. While a
484
599
  * personal head is in place the cascade always runs to the last stage (the tiny head is the shipped one).
485
600
  * Returns { n, classes } or null (nothing usable). `depersonalize()` restores the shipped head.
@@ -537,6 +652,7 @@ var MillirecCharCnnCore = class {
537
652
  this.destroyed = true;
538
653
  this.stages = [];
539
654
  this.personal = null;
655
+ this.taught = null;
540
656
  this._plane = null;
541
657
  this._planeFor = null;
542
658
  }
@@ -560,7 +676,7 @@ function nodeFileUrl(url2, spec) {
560
676
  }
561
677
 
562
678
  // src/options.js
563
- var OPTIONS = ["accurate", "fast", "small"];
679
+ var OPTIONS = ["accurate", "fast", "small", "digits", "digits-fast", "digits-small", "lowercase", "lowercase-fast", "lowercase-small", "all", "all-fast", "all-small"];
564
680
  async function loadOption(name, { assetBase = HERE } = {}) {
565
681
  if (!OPTIONS.includes(name)) throw new Error(`millirec: no CNN option "${name}" (one of ${OPTIONS.join(", ")})`);
566
682
  const base = assetBaseOf(assetBase);
@@ -571,9 +687,7 @@ async function loadOption(name, { assetBase = HERE } = {}) {
571
687
  async function withOption(opts) {
572
688
  if (typeof opts === "string") opts = { option: opts };
573
689
  opts = opts || {};
574
- if (typeof document === "undefined" && typeof OffscreenCanvas === "undefined") {
575
- throw new Error("millirec: char-cnn needs a browser (fetch, and a canvas or OffscreenCanvas); importing is SSR-safe but call create() on the client");
576
- }
690
+ if (typeof fetch === "undefined") throw new Error("millirec: char-cnn needs fetch to load its model files");
577
691
  const assetBase = assetBaseOf(opts.assetBase || HERE);
578
692
  const preset = opts.stages ? {} : await loadOption(opts.option || "accurate", { assetBase });
579
693
  return { ...preset, ...opts, assetBase };
@@ -581,7 +695,7 @@ async function withOption(opts) {
581
695
 
582
696
  // src/char-cnn-core.js
583
697
  var MillirecCharCnnCore2 = class extends MillirecCharCnnCore {
584
- /** opts: option ('accurate' | 'fast' | 'small', default 'accurate') or your own
698
+ /** opts: option (one of OPTIONS: 'accurate', 'fast', 'small', 'digits', 'lowercase', 'all', each set also -fast and -small; default 'accurate') or your own
585
699
  * { classes, stages, kernel }, plus assetBase (default: this module's folder).
586
700
  * A string is the option's name: create('fast') = create({ option: 'fast' }). */
587
701
  static async create(opts = {}) {
@@ -1,7 +1,7 @@
1
1
  import type { AttachOptions, Pad } from '../char-lite/index.js'
2
2
  import { MillirecCharCnnCore, type CreateOptions, type OptionName } from './core.js'
3
3
 
4
- export type { ResultDetail, ClearDetail } from '../char-lite/index.js'
4
+ export type { ResultDetail, ClearDetail, Point, Stroke, Strokes, Reading } from '../char-lite/index.js'
5
5
  export { OPTIONS, loadOption, type OptionName, type CnnConfig, type Stage, type CreateOptions } from './core.js'
6
6
 
7
7
  export declare class MillirecCharCnn extends MillirecCharCnnCore {