millirec 0.1.2 → 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 (76) hide show
  1. package/CHANGELOG.md +65 -0
  2. package/README.md +171 -58
  3. package/dist/char-cnn/cnn-web.wasm +0 -0
  4. package/dist/char-cnn/core.d.ts +3 -1
  5. package/dist/char-cnn/core.js +145 -99
  6. package/dist/char-cnn/index.d.ts +1 -1
  7. package/dist/char-cnn/index.js +198 -136
  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 +2 -2
  12. package/dist/char-cnn/options/accurate.json +1 -1
  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-tiny.head.bin +0 -0
  31. package/dist/char-cnn/options/digits-fast/digits-base-p25.bin +0 -0
  32. package/dist/char-cnn/options/digits-fast/digits-base-p25.head.bin +0 -0
  33. package/dist/char-cnn/options/digits-fast/digits-tiny.bin +0 -0
  34. package/dist/char-cnn/options/digits-fast/digits-tiny.head.bin +0 -0
  35. package/dist/char-cnn/options/digits-fast.js +17 -0
  36. package/dist/char-cnn/options/digits-fast.json +15 -0
  37. package/dist/char-cnn/options/digits-small/digits-tiny-qat4.head.bin +0 -0
  38. package/dist/char-cnn/options/digits-small/digits-tiny-qat4.q4.bin +0 -0
  39. package/dist/char-cnn/options/digits-small.js +11 -0
  40. package/dist/char-cnn/options/digits-small.json +9 -0
  41. package/dist/char-cnn/options/digits.js +2 -2
  42. package/dist/char-cnn/options/digits.json +1 -1
  43. package/dist/char-cnn/options/fast/base-p25.head.bin +0 -0
  44. package/dist/char-cnn/options/fast/tiny.head.bin +0 -0
  45. package/dist/char-cnn/options/fast.js +2 -2
  46. package/dist/char-cnn/options/fast.json +1 -1
  47. package/dist/char-cnn/options/lowercase/lowercase-tiny.head.bin +0 -0
  48. package/dist/char-cnn/options/lowercase-fast/lowercase-base-p25.bin +0 -0
  49. package/dist/char-cnn/options/lowercase-fast/lowercase-base-p25.head.bin +0 -0
  50. package/dist/char-cnn/options/lowercase-fast/lowercase-tiny.bin +0 -0
  51. package/dist/char-cnn/options/lowercase-fast/lowercase-tiny.head.bin +0 -0
  52. package/dist/char-cnn/options/lowercase-fast.js +17 -0
  53. package/dist/char-cnn/options/lowercase-fast.json +15 -0
  54. package/dist/char-cnn/options/lowercase-small/lowercase-tiny-qat4.head.bin +0 -0
  55. package/dist/char-cnn/options/lowercase-small/lowercase-tiny-qat4.q4.bin +0 -0
  56. package/dist/char-cnn/options/lowercase-small.js +11 -0
  57. package/dist/char-cnn/options/lowercase-small.json +9 -0
  58. package/dist/char-cnn/options/lowercase.js +2 -2
  59. package/dist/char-cnn/options/lowercase.json +1 -1
  60. package/dist/char-cnn/options/small/tiny.head.bin +0 -0
  61. package/dist/char-cnn/options/small/tiny.q4.bin +0 -0
  62. package/dist/char-cnn/options/small.js +2 -2
  63. package/dist/char-cnn/options/small.json +1 -1
  64. package/dist/char-lite/core.d.ts +1 -0
  65. package/dist/char-lite/core.js +628 -0
  66. package/dist/char-lite/index.d.ts +28 -8
  67. package/dist/char-lite/index.js +845 -717
  68. package/dist/char-lite/millirec-char-lite.min.js +1 -1
  69. package/dist/char-lite/sets.json +17 -17
  70. package/dist/index.d.ts +35 -0
  71. package/dist/index.js +88 -0
  72. package/dist/overlap/millirec-overlap.min.js +7 -7
  73. package/dist/pad/index.d.ts +8 -0
  74. package/dist/pad/index.js +482 -0
  75. package/package.json +21 -7
  76. package/dist/char-cnn/options/small/tiny.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;
240
- }
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;
307
+ return { ink, W, iMinX, iMaxX, iMinY, iMaxY };
245
308
  }
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,6 @@ 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;
339
386
  var TEACH_SHARPNESS = 18;
340
387
  var unitOf = (e) => {
341
388
  let n = 0;
@@ -363,8 +410,6 @@ var MillirecCharCnnCore = class {
363
410
  this.base = assetBaseOf(config.assetBase || ASSET_BASE);
364
411
  this.classes = typeof config.classes === "string" ? config.classes.split("") : config.classes;
365
412
  this.stages = [];
366
- this.off = makeCanvas(140);
367
- this.octx = this.off ? this.off.getContext("2d", { willReadFrequently: true }) : null;
368
413
  this.ready = this.load();
369
414
  }
370
415
  async load() {
@@ -420,18 +465,19 @@ var MillirecCharCnnCore = class {
420
465
  this.stages.push(stage);
421
466
  }
422
467
  }
468
+ /** Which engine this is (createRecognizer() picks one). */
469
+ get engine() {
470
+ return "char-cnn";
471
+ }
423
472
  /** drawn strokes ([[{x,y},...],...]) -> the model input raster at `size`.
424
- * The canvas draw + readback is the dear part (~1 ms) and is shared by every
425
- * 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. */
426
475
  raster(strokes, size) {
427
- if (!this.octx) throw new Error("millirec: char-cnn needs a canvas to draw strokes on (a document, or OffscreenCanvas in a worker)");
428
476
  if (this._planeFor !== strokes) {
429
- const W2 = drawStrokes(this.octx, strokes);
430
- this._plane = { ink: inkPlane(this.octx.getImageData(0, 0, W2, W2).data, W2, W2), W: W2 };
477
+ this._plane = strokePlane(strokes);
431
478
  this._planeFor = strokes;
432
479
  }
433
- const { ink, W } = this._plane;
434
- return normalizeInk(ink, W, W, size, Math.round(size * 0.75));
480
+ return normalizePlane(this._plane, size, Math.round(size * 0.75));
435
481
  }
436
482
  /**
437
483
  * -> [{char, prob} x3] (best first) with `.stage` = index of the model that decided, or null.
@@ -441,7 +487,7 @@ var MillirecCharCnnCore = class {
441
487
  */
442
488
  async recognize(strokes, { only = null } = {}) {
443
489
  strokes = asStrokes(strokes);
444
- if (!strokes || !strokes.length) return null;
490
+ if (!strokes || !strokes.length || !finitePoints(strokes)) return null;
445
491
  await this.ready;
446
492
  if (this.destroyed || !this.stages.length) return null;
447
493
  this._planeFor = null;
@@ -537,6 +583,8 @@ var MillirecCharCnnCore = class {
537
583
  /** The last stage's (unit) embedding of drawn strokes — what the head reads. */
538
584
  async embedding(strokes) {
539
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");
540
588
  await this.ready;
541
589
  if (this.destroyed || !this.stages.length) throw new Error(DESTROYED);
542
590
  this._planeFor = null;
@@ -834,6 +882,18 @@ function createDrawCanvas({
834
882
  for (let i = 1; i < st.length; i++) ctx.lineTo(st[i].x, st[i].y);
835
883
  ctx.stroke();
836
884
  }
885
+ function paintFrom(st, from) {
886
+ if (st.length < 2 || from >= st.length - 1) return;
887
+ ctx.lineWidth = 2.5;
888
+ ctx.lineCap = "round";
889
+ ctx.lineJoin = "round";
890
+ ctx.globalAlpha = 1;
891
+ ctx.strokeStyle = ink;
892
+ ctx.beginPath();
893
+ ctx.moveTo(st[Math.max(0, from)].x, st[Math.max(0, from)].y);
894
+ for (let i = Math.max(1, from + 1); i < st.length; i++) ctx.lineTo(st[i].x, st[i].y);
895
+ ctx.stroke();
896
+ }
837
897
  function render() {
838
898
  ctx.clearRect(0, 0, width, height);
839
899
  ctx.lineWidth = 2.5;
@@ -899,8 +959,8 @@ function createDrawCanvas({
899
959
  canvas.style.webkitTouchCallout = "none";
900
960
  canvas.addEventListener("pointerdown", (e) => {
901
961
  if (e.button > 0) return;
902
- if (activeId !== null && activeId !== e.pointerId) {
903
- if (activeType === "pen" && e.pointerType !== "pen") return;
962
+ if (activeId !== null) {
963
+ if (activeType === "pen" && e.pointerType !== "pen" && e.pointerId !== activeId) return;
904
964
  finish({ pointerId: activeId });
905
965
  }
906
966
  e.preventDefault();
@@ -935,8 +995,9 @@ function createDrawCanvas({
935
995
  }
936
996
  return;
937
997
  }
998
+ const from = current.length - 1;
938
999
  for (const ev of events) current.push(getPos(ev));
939
- render();
1000
+ paintFrom(current, from);
940
1001
  }, { passive: false });
941
1002
  const finish = (e, cancelled = false) => {
942
1003
  if (activeId !== e.pointerId) return;
@@ -994,8 +1055,14 @@ function createDrawCanvas({
994
1055
  }
995
1056
  render();
996
1057
  };
997
- canvas.addEventListener("pointerup", (e) => finish(e));
998
- canvas.addEventListener("pointercancel", (e) => finish(e, true));
1058
+ canvas.addEventListener("pointerup", (e) => {
1059
+ if (e.pointerId === activeId) e.preventDefault();
1060
+ finish(e);
1061
+ }, { passive: false });
1062
+ canvas.addEventListener("pointercancel", (e) => finish(e, true), { passive: false });
1063
+ canvas.addEventListener("lostpointercapture", (e) => {
1064
+ if (!canvas.hasPointerCapture || !canvas.hasPointerCapture(e.pointerId)) finish(e, true);
1065
+ });
999
1066
  return {
1000
1067
  el: canvas,
1001
1068
  getStrokes: () => strokes,
@@ -1051,40 +1118,37 @@ function createDrawCanvas({
1051
1118
  };
1052
1119
  }
1053
1120
 
1121
+ // ../../library/src/pad.js
1122
+ function attachPad(el, recognizer, { width = 240, height = 240, ink = "#1e293b", idleOverwrite = 2, scribbleClear = true, scribble = null, staleAlpha, onStale, recognizeOptions } = {}) {
1123
+ if (!recognizer || typeof recognizer.recognize !== "function") throw new TypeError("millirec: attachPad(el, recognizer) needs a recognizer with recognize(strokes)");
1124
+ const pad = createDrawCanvas({
1125
+ width,
1126
+ height,
1127
+ ink,
1128
+ idleOverwrite,
1129
+ scribbleClear,
1130
+ scribble,
1131
+ onStale,
1132
+ ...staleAlpha === void 0 ? {} : { staleAlpha },
1133
+ onClear: (reason) => el.dispatchEvent(new CustomEvent("millirec:clear", { detail: { reason, undo: () => pad.undoClear() } })),
1134
+ onStrokeEnd: async (strokes) => {
1135
+ if (!strokes.length) {
1136
+ el.dispatchEvent(new CustomEvent("millirec:result", { detail: { results: [], strokes: [] } }));
1137
+ return;
1138
+ }
1139
+ const results = await (recognizeOptions ? recognizer.recognize(strokes, recognizeOptions) : recognizer.recognize(strokes));
1140
+ el.dispatchEvent(new CustomEvent("millirec:result", { detail: { results, strokes } }));
1141
+ }
1142
+ });
1143
+ el.appendChild(pad.el);
1144
+ return { clear: pad.clear, undoClear: pad.undoClear, isStale: pad.isStale, configure: pad.configure, setInk: pad.setInk, repaint: pad.repaint, getStrokes: pad.getStrokes, canvas: pad.el };
1145
+ }
1146
+
1054
1147
  // ../../library/src/char-cnn.js
1055
1148
  var MillirecCharCnn = class extends MillirecCharCnnCore {
1056
- /**
1057
- * Mount a drawing pad in `el`; fires 'millirec:result' ({results, strokes})
1058
- * after each stroke, and 'millirec:clear' ({reason, undo}) when the pad
1059
- * drops its own ink.
1060
- *
1061
- * The pad reads ONE character, so by default writing in it after a pause
1062
- * starts a fresh one (`idleOverwrite`, seconds — 0 turns it off) and
1063
- * scribbling over it throws it away (`scribbleClear`). Both are measured:
1064
- * see widget/canvas.js and scripts/verify-scribble.mjs.
1065
- */
1066
- attach(el, { width = 240, height = 240, ink = "#1e293b", idleOverwrite = 2, scribbleClear = true, scribble = null, staleAlpha, onStale } = {}) {
1067
- const pad = createDrawCanvas({
1068
- width,
1069
- height,
1070
- ink,
1071
- idleOverwrite,
1072
- scribbleClear,
1073
- scribble,
1074
- onStale,
1075
- ...staleAlpha === void 0 ? {} : { staleAlpha },
1076
- onClear: (reason) => el.dispatchEvent(new CustomEvent("millirec:clear", { detail: { reason, undo: () => pad.undoClear() } })),
1077
- onStrokeEnd: async (strokes) => {
1078
- if (!strokes.length) {
1079
- el.dispatchEvent(new CustomEvent("millirec:result", { detail: { results: [], strokes: [] } }));
1080
- return;
1081
- }
1082
- const results = await this.recognize(strokes);
1083
- el.dispatchEvent(new CustomEvent("millirec:result", { detail: { results, strokes } }));
1084
- }
1085
- });
1086
- el.appendChild(pad.el);
1087
- return { clear: pad.clear, undoClear: pad.undoClear, isStale: pad.isStale, configure: pad.configure, setInk: pad.setInk, repaint: pad.repaint, getStrokes: pad.getStrokes, canvas: pad.el };
1149
+ /** Mount a drawing pad in `el` (library/src/pad.js): 'millirec:result' after each stroke, 'millirec:clear' when it drops its ink. */
1150
+ attach(el, opts) {
1151
+ return attachPad(el, this, opts);
1088
1152
  }
1089
1153
  };
1090
1154
  if (typeof window !== "undefined" && false) window.MillirecCharCnn = MillirecCharCnn;
@@ -1101,7 +1165,7 @@ function nodeFileUrl(url2, spec) {
1101
1165
  }
1102
1166
 
1103
1167
  // src/options.js
1104
- var OPTIONS = ["accurate", "fast", "small", "digits", "lowercase"];
1168
+ var OPTIONS = ["accurate", "fast", "small", "digits", "digits-fast", "digits-small", "lowercase", "lowercase-fast", "lowercase-small", "all", "all-fast", "all-small"];
1105
1169
  async function loadOption(name, { assetBase = HERE } = {}) {
1106
1170
  if (!OPTIONS.includes(name)) throw new Error(`millirec: no CNN option "${name}" (one of ${OPTIONS.join(", ")})`);
1107
1171
  const base = assetBaseOf(assetBase);
@@ -1112,9 +1176,7 @@ async function loadOption(name, { assetBase = HERE } = {}) {
1112
1176
  async function withOption(opts) {
1113
1177
  if (typeof opts === "string") opts = { option: opts };
1114
1178
  opts = opts || {};
1115
- if (typeof document === "undefined" && typeof OffscreenCanvas === "undefined") {
1116
- 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");
1117
- }
1179
+ if (typeof fetch === "undefined") throw new Error("millirec: char-cnn needs fetch to load its model files");
1118
1180
  const assetBase = assetBaseOf(opts.assetBase || HERE);
1119
1181
  const preset = opts.stages ? {} : await loadOption(opts.option || "accurate", { assetBase });
1120
1182
  return { ...preset, ...opts, assetBase };
@@ -1122,7 +1184,7 @@ async function withOption(opts) {
1122
1184
 
1123
1185
  // src/char-cnn.js
1124
1186
  var MillirecCharCnn2 = class extends MillirecCharCnn {
1125
- /** opts: option ('accurate' | 'fast' | 'small', default 'accurate') or your own
1187
+ /** opts: option (one of OPTIONS: 'accurate', 'fast', 'small', 'digits', 'lowercase', 'all', each set also -fast and -small; default 'accurate') or your own
1126
1188
  * { classes, stages, kernel }, plus assetBase (default: this module's folder).
1127
1189
  * A string is the option's name: create('fast') = create({ option: 'fast' }). */
1128
1190
  static async create(opts = {}) {
@@ -1 +1 @@
1
- "use strict";(()=>{var D={MRCNN1:8,MRCNN3:3,MRCNN4:4,MRCNN5:5,MRCNN6:6};function z(s){let n=new DataView(s.buffer?s.buffer:s,s.byteOffset||0,s.byteLength),l=new Uint8Array(n.buffer,n.byteOffset,n.byteLength),e=0;return{u32:()=>{let t=n.getUint32(e,!0);return e+=4,t},f32s:t=>{let r=new Float32Array(t);for(let a=0;a<t;a++)r[a]=n.getFloat32(e,!0),e+=4;return r},i8s:t=>{let r=new Int8Array(t);for(let a=0;a<t;a++)r[a]=l[e+a]<<24>>24;return e+=t,r},ibits:(t,r)=>{if(r===8){let i=new Int8Array(t);for(let o=0;o<t;o++)i[o]=l[e+o]<<24>>24;return e+=t,i}let a=new Int8Array(t),u=(1<<r)-1,h=1<<r-1;for(let i=0;i<t;i++){let o=i*r,c=e+(o>>3),f=o&7,d=(l[c]|l[c+1]<<8)>>f&u;a[i]=d&h?d-(1<<r):d}return e+=t*r+7>>3,a},str:t=>{let r=String.fromCharCode(...l.subarray(e,e+t));return e+=t,r},done:()=>e>=n.byteLength}}function q(s,n=8){let l=s.u32(),e=s.u32(),t=s.f32s(l),r=s.f32s(l),a=s.ibits(l*e,n),u=new Float32Array(l*e);for(let h=0;h<l;h++)for(let i=0;i<e;i++)u[h*e+i]=a[h*e+i]*t[h];return{out:l,in:e,w:u,bias:r}}function W(s){let n=z(s),l=n.str(6),e=D[l];if(!e)throw new Error("not an MRCNN1/3/4/5/6 model");let t=n.u32();if(t!==1)throw new Error("unknown model version "+t);let r=n.u32(),a=n.u32(),u=n.u32(),h=[];for(let o=0;o<u;o++){let c=n.u32(),f=n.u32(),d=n.u32(),m=n.u32(),g=n.f32s(c),b=n.f32s(c),A=n.ibits(c*f*d*d,e),M=new Float32Array(A.length),I=f*d*d;for(let p=0;p<c;p++)for(let x=0;x<I;x++)M[p*I+x]=A[p*I+x]*g[p];h.push({cout:c,cin:f,k:d,pool:m,w:M,bias:b})}let i=q(n,e);return{size:r,dim:a,convs:h,lin:i,bits:e}}function Y(s){let n=new Uint8Array(s.buffer?s.buffer:s,s.byteOffset||0,s.byteLength),l=String.fromCharCode(...n.subarray(0,6)),e=D[l];if(!e)throw new Error("not an MRCNN model");if(e===8)return n;let t=z(s);t.str(6);let r=t.u32(),a=t.u32(),u=t.u32(),h=t.u32(),i=[],o=p=>{let x=new Uint8Array(4);return new DataView(x.buffer).setUint32(0,p,!0),x},c=p=>new Uint8Array(p.buffer,p.byteOffset,p.byteLength);i.push(new TextEncoder().encode("MRCNN1"),o(r),o(a),o(u),o(h));for(let p=0;p<h;p++){let x=t.u32(),y=t.u32(),w=t.u32(),k=t.u32(),N=t.f32s(x),S=t.f32s(x),F=t.ibits(x*y*w*w,e);i.push(o(x),o(y),o(w),o(k),c(N),c(S),new Uint8Array(F.buffer,F.byteOffset,F.length))}let f=t.u32(),d=t.u32(),m=t.f32s(f),g=t.f32s(f),b=t.ibits(f*d,e);i.push(o(f),o(d),c(m),c(g),new Uint8Array(b.buffer,b.byteOffset,b.length));let A=i.reduce((p,x)=>p+x.length,0),M=new Uint8Array(A),I=0;for(let p of i)M.set(p,I),I+=p.length;return M}function $(s){return q(z(s))}function st(s,n,l,e){let{cout:t,cin:r,k:a,w:u,bias:h}=s,i=l*l,o=a-1>>1,c=r*a*a;for(let f=0;f<t;f++){let d=f*i,m=h[f],g=f*c;for(let b=0;b<l;b++)for(let A=0;A<l;A++){let M=m;for(let I=0;I<r;I++){let p=I*i,x=g+I*a*a;for(let y=0;y<a;y++){let w=b+y-o;if(w<0||w>=l)continue;let k=p+w*l,N=x+y*a;for(let S=0;S<a;S++){let F=A+S-o;F<0||F>=l||(M+=u[N+S]*n[k+F])}}}e[d+b*l+A]=M>0?M:0}}}function rt(s,n,l,e){let t=n>>1;for(let r=0;r<l;r++){let a=r*n*n,u=r*t*t;for(let h=0;h<t;h++)for(let i=0;i<t;i++){let o=a+2*h*n+2*i,c=s[o],f=s[o+1],d=s[o+n],m=s[o+n+1];e[u+h*t+i]=Math.max(c,f,d,m)}}}function X(s,n){let l=s.size,e=Float32Array.from(n),t=l,r=1;for(let o of s.convs){let c=new Float32Array(o.cout*t*t);if(st(o,e,t,c),e=c,r=o.cout,o.pool){let f=new Float32Array(r*(t>>1)*(t>>1));rt(e,t,r,f),e=f,t>>=1}}let a=t*t,u=new Float32Array(r);for(let o=0;o<r;o++){let c=0;for(let f=0;f<a;f++)c+=e[o*a+f];u[o]=c/a}let{lin:h}=s,i=new Float32Array(h.out);for(let o=0;o<h.out;o++){let c=h.bias[o];for(let f=0;f<h.in;f++)c+=h.w[o*h.in+f]*u[f];i[o]=c}return i}function K(s){let n=0;for(let e=0;e<s.length;e++)n+=s[e]*s[e];n=1/(Math.sqrt(n)+1e-9);let l=new Float32Array(s.length);for(let e=0;e<s.length;e++)l[e]=s[e]*n;return l}function _(s,n){let l=new Float32Array(s.out);for(let e=0;e<s.out;e++){let t=s.bias[e];for(let r=0;r<s.in;r++){let a=n[r];a>0&&(t+=s.w[e*s.in+r]*a)}l[e]=t}return l}function L(s){let n=-1/0;for(let t of s)t>n&&(n=t);let l=new Float32Array(s.length),e=0;for(let t=0;t<s.length;t++)l[t]=Math.exp(s[t]-n),e+=l[t];for(let t=0;t<s.length;t++)l[t]/=e;return l}function Z(s,n,l=120,e=7,t=10){let r=1/0,a=1/0,u=-1/0,h=-1/0;for(let c of n)for(let f of c)f.x<r&&(r=f.x),f.x>u&&(u=f.x),f.y<a&&(a=f.y),f.y>h&&(h=f.y);let i=l/Math.max(u-r,h-a,1),o=l+2*t;s.fillStyle="#fff",s.fillRect(0,0,o,o),s.strokeStyle="#000",s.lineWidth=e,s.lineCap="round",s.lineJoin="round";for(let c of n)if(!(c.length<2)){s.beginPath(),s.moveTo((c[0].x-r)*i+t,(c[0].y-a)*i+t);for(let f=1;f<c.length;f++)s.lineTo((c[f].x-r)*i+t,(c[f].y-a)*i+t);s.stroke()}return o}function V(s,n,l){let e=new Float32Array(n*l);for(let t=0,r=0;t<e.length;t++,r+=4)e[t]=1-(s[r]*.299+s[r+1]*.587+s[r+2]*.114)/255;return e}function J(s,n,l,e=32,t=24){let r=1/0,a=-1,u=1/0,h=-1;for(let y=0;y<l;y++)for(let w=0;w<n;w++)s[y*n+w]>.15&&(w<r&&(r=w),w>a&&(a=w),y<u&&(u=y),y>h&&(h=y));let i=new Float32Array(e*e);if(a<0)return i;let o=a-r+1,c=h-u+1,f=new Float32Array(o*c);for(let y=0;y<c;y++)for(let w=0;w<o;w++)f[y*o+w]=s[(y+u)*n+(w+r)];let d=0;for(let y=0;y<f.length;y++)f[y]>d&&(d=f[y]);if(d>.05&&d<.999)for(let y=0;y<f.length;y++)f[y]=Math.min(1,f[y]/d);let m=t/Math.max(c,o),g=Math.max(1,Math.round(c*m)),b=Math.max(1,Math.round(o*m)),A=new Float32Array(b*g),M=o/b,I=c/g;for(let y=0;y<g;y++){let w=y*I,k=(y+1)*I,N=Math.floor(w),S=Math.min(c,Math.ceil(k));for(let F=0;F<b;F++){let B=F*M,H=(F+1)*M,nt=Math.floor(B),ot=Math.min(o,Math.ceil(H)),P=0,O=0;for(let v=N;v<S;v++){let R=Math.min(k,v+1)-Math.max(w,v);if(!(R<=0))for(let E=nt;E<ot;E++){let U=Math.min(H,E+1)-Math.max(B,E);U<=0||(P+=f[v*o+E]*U*R,O+=U*R)}}A[y*b+F]=O>0?P/O:0}}let p=e-g>>1,x=e-b>>1;for(let y=0;y<g;y++)for(let w=0;w<b;w++)i[(y+p)*e+w+x]=A[y*b+w];return i}function G(s){let n=s?String(s):"";return n?(n.endsWith("/")||(n+="/"),typeof location<"u"?new URL(n,location.href).href:n):typeof location<"u"?new URL("./",location.href).href:""}var C=s=>Array.isArray(s)&&s.length&&!Array.isArray(s[0])?[s]:s,T=(s,n,l,e)=>new Error(`millirec: ${s} is not ${n} \u2014 the package files are not served at ${l}; copy node_modules/millirec/dist/${e}/ and pass assetBase (see README "Serving the files")`);async function Q(s,{as:n="json",what:l,base:e,recognizer:t}){let r=o=>{let c=T(s,l,e,t);return o&&(c.message+=` (${o})`),c},a;try{a=await fetch(s)}catch(o){throw r(o&&o.message)}let u=a.headers&&a.headers.get("content-type")||"";if(!a.ok)throw r(`HTTP ${a.status}`);if(n==="json")try{return await a.json()}catch(o){throw r(/html/i.test(u)?"an HTML page":o&&o.message)}let h=await a.arrayBuffer(),i=String.fromCharCode(...new Uint8Array(h,0,Math.min(h.byteLength,16))).trimStart().toLowerCase();if(i.startsWith("<!doctype")||i.startsWith("<html"))throw r("an HTML page");return h}function it(){let s=typeof document<"u"&&document.currentScript&&document.currentScript.src||"";return s?s.slice(0,s.lastIndexOf("/")+1):""}var at=it(),tt=(s,n)=>Q(s,{as:"buf",what:"a char-cnn model file",base:n,recognizer:"char-cnn"}),lt="millirec: recognizer destroyed",ct=s=>typeof document<"u"&&document.createElement?Object.assign(document.createElement("canvas"),{width:s,height:s}):typeof OffscreenCanvas<"u"?new OffscreenCanvas(s,s):null,ft=18,et=s=>{let n=0;for(let e=0;e<s.length;e++)n+=s[e]*s[e];n=Math.sqrt(n)||1;let l=new Float32Array(s.length);for(let e=0;e<s.length;e++)l[e]=s[e]/n;return l},j=class{static async create(n={}){let e={...(typeof window<"u"?window:globalThis).MILLIREC_CNN||{},...n};if(!e.stages||!e.stages.length)throw new Error("MillirecCharCnn: no stages configured");if(typeof fetch>"u"||typeof document>"u"&&typeof OffscreenCanvas>"u")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");let t=new this(e);return await t.ready,e.examples&&await t.teach(e.examples),t}constructor(n){this.config=n,this.base=G(n.assetBase||at),this.classes=typeof n.classes=="string"?n.classes.split(""):n.classes,this.stages=[],this.off=ct(140),this.octx=this.off?this.off.getContext("2d",{willReadFrequently:!0}):null,this.ready=this.load()}async load(){let{base:n,config:l}=this,e=null;if(l.kernel){let t=n+l.kernel;try{e=(await import(t)).default}catch(r){let a=T(t,"the char-cnn kernel",n,"char-cnn");throw a.message+=` (${r&&r.message})`,a}}for(let t of l.stages){let[r,a]=await Promise.all([tt(n+t.model,n),tt(n+t.head,n)]);if(this.destroyed)return;let u=$(a),h={theta:t.theta==null?null:t.theta,head:u,headNorm:t.headNorm!==!1,size:0};if(e){let i=await e();if(this.destroyed)return;let o=Y(new Uint8Array(r)),c=i._malloc(o.byteLength);i.HEAPU8.set(o,c);let f=i._cnnLoad(c,o.byteLength);if(i._free(c),!f)throw new Error(`kernel rejected ${t.model} (channel widths must be multiples of four)`);let d=i._cnnSize(),m=i._cnnDim();h.size=d,h.embed=g=>{let b=i._cnnInputPtr(1)>>2;i.HEAPF32.set(g,b),i._cnnRun(1);let A=i._cnnEmbedPtr(1)>>2;return i.HEAPF32.slice(A,A+m)},h.logits=g=>_(h.head,h.embed(g))}else{let i=W(r);h.size=i.size,h.embed=o=>{let c=X(i,o);return h.headNorm?K(c):c},h.logits=o=>_(h.head,h.embed(o))}this.stages.push(h)}}raster(n,l){if(!this.octx)throw new Error("millirec: char-cnn needs a canvas to draw strokes on (a document, or OffscreenCanvas in a worker)");if(this._planeFor!==n){let r=Z(this.octx,n);this._plane={ink:V(this.octx.getImageData(0,0,r,r).data,r,r),W:r},this._planeFor=n}let{ink:e,W:t}=this._plane;return J(e,t,t,l,Math.round(l*.75))}async recognize(n,{only:l=null}={}){if(n=C(n),!n||!n.length||(await this.ready,this.destroyed||!this.stages.length))return null;this._planeFor=null;let e=l?new Set(l):null;if(this.taught)return this.nearest(n,e);let t=null,r=0;for(let o=0;o<this.stages.length;o++){let c=this.stages[o];if((this.personal||e)&&o<this.stages.length-1)continue;if(t=c.logits(this.raster(n,c.size)),r=o,c.theta==null||o===this.stages.length-1)break;let f=-1/0,d=-1/0;for(let m of t)m>f?(d=f,f=m):m>d&&(d=m);if(f-d>=c.theta)break}let a=[...t.keys()];if(e&&(a=a.filter(o=>e.has(this.classes[o])),!a.length)){let o=[];return o.stage=r,o}let u=L(a.map(o=>t[o])),i=[...u.keys()].sort((o,c)=>u[c]-u[o]).slice(0,3).map(o=>({char:this.classes[a[o]],prob:u[o]}));return i.stage=r,i}nearest(n,l){let e=this.stages.length-1,t=this.stages[e],r=et(t.embed(this.raster(n,t.size))),a=[...this.taught.keys()].filter(h=>!l||l.has(h)),u=[];if(a.length){let h=a.map(o=>{let c=-2;for(let f of this.taught.get(o)){let d=0;for(let m=0;m<r.length;m++)d+=r[m]*f[m];d>c&&(c=d)}return c}),i=L(h.map(o=>o*ft));for(let o of[...i.keys()].sort((c,f)=>i[f]-i[c]).slice(0,3))u.push({char:a[o],prob:i[o]})}return u.stage=e,u}async teach(n,{add:l=!1}={}){if(await this.ready,this.destroyed||!this.stages.length)return null;let e=l&&this.taught?new Map([...this.taught].map(([t,r])=>[t,r.slice()])):new Map;for(let[t,r]of Object.entries(n||{}))for(let a of r||[]){let u=C(a);if(!u||!u.length)continue;let h=et(await this.embedding(u));if(this.destroyed)return null;e.has(t)||e.set(t,[]),e.get(t).push(h)}return e.size?(this.taught=e,{classes:[...e.keys()],n:[...e.values()].reduce((t,r)=>t+r.length,0)}):(l||(this.taught=null),null)}forget(){this.taught=null}async embedding(n){if(n=C(n),await this.ready,this.destroyed||!this.stages.length)throw new Error(lt);this._planeFor=null;let l=this.stages[this.stages.length-1];return l.embed(this.raster(n,l.size))}async personalize(n,{steps:l=250,lr:e=.5,prior:t=1}={}){if(await this.ready,this.destroyed||!this.stages.length)return null;let r=this.stages[this.stages.length-1],a=r.head0||(r.head0=r.head),u=[],h=[];for(let[d,m]of Object.entries(n||{})){let g=this.classes.indexOf(d);if(!(g<0))for(let b of m||[]){let A=C(b);!A||!A.length||(u.push(await this.embedding(A)),h.push(g))}}if(this.destroyed)return null;if(!u.length)return r.head=a,this.personal=null,null;let i={in:a.in,out:a.out,w:Float32Array.from(a.w),bias:Float32Array.from(a.bias)},o=u.length,c=new Float32Array(i.w.length),f=new Float32Array(i.out);for(let d=0;d<l;d++){c.fill(0),f.fill(0);for(let g=0;g<o;g++){let b=L(_(i,u[g])),A=u[g];for(let M=0;M<i.out;M++){let I=(b[M]-(M===h[g]?1:0))/o;if(!I)continue;f[M]+=I;let p=M*i.in;for(let x=0;x<i.in;x++){let y=A[x];y>0&&(c[p+x]+=I*y)}}}let m=t/o;for(let g=0;g<i.w.length;g++)i.w[g]-=e*(c[g]+m*(i.w[g]-a.w[g]));for(let g=0;g<i.out;g++)i.bias[g]-=e*(f[g]+m*(i.bias[g]-a.bias[g]))}return r.head=i,this.personal={n:o,classes:[...new Set(h)].map(d=>this.classes[d]).sort()},this.personal}destroy(){this.destroyed=!0,this.stages=[],this.personal=null,this.taught=null,this._plane=null,this._planeFor=null}depersonalize(){let n=this.stages[this.stages.length-1];n&&n.head0&&(n.head=n.head0),this.personal=null}};typeof window<"u"&&typeof __MILLIREC_ESM__>"u"&&(window.MillirecCharCnn=j);})();
1
+ "use strict";(()=>{var K={MRCNN1:8,MRCNN3:3,MRCNN4:4,MRCNN5:5,MRCNN6:6};function $(s){let t=new DataView(s.buffer?s.buffer:s,s.byteOffset||0,s.byteLength),i=new Uint8Array(t.buffer,t.byteOffset,t.byteLength),e=0;return{u32:()=>{let n=t.getUint32(e,!0);return e+=4,n},f32s:n=>{let l=new Float32Array(n);for(let a=0;a<n;a++)l[a]=t.getFloat32(e,!0),e+=4;return l},i8s:n=>{let l=new Int8Array(n);for(let a=0;a<n;a++)l[a]=i[e+a]<<24>>24;return e+=n,l},ibits:(n,l)=>{if(l===8){let o=new Int8Array(n);for(let r=0;r<n;r++)o[r]=i[e+r]<<24>>24;return e+=n,o}let a=new Int8Array(n),y=(1<<l)-1,f=1<<l-1;for(let o=0;o<n;o++){let r=o*l,h=e+(r>>3),u=r&7,g=(i[h]|i[h+1]<<8)>>u&y;a[o]=g&f?g-(1<<l):g}return e+=n*l+7>>3,a},str:n=>{let l=String.fromCharCode(...i.subarray(e,e+n));return e+=n,l},done:()=>e>=t.byteLength}}function Q(s,t=8){let i=s.u32(),e=s.u32(),n=s.f32s(i),l=s.f32s(i),a=s.ibits(i*e,t),y=new Float32Array(i*e);for(let f=0;f<i;f++)for(let o=0;o<e;o++)y[f*e+o]=a[f*e+o]*n[f];return{out:i,in:e,w:y,bias:l}}function Z(s){let t=$(s),i=t.str(6),e=K[i];if(!e)throw new Error("not an MRCNN1/3/4/5/6 model");let n=t.u32();if(n!==1)throw new Error("unknown model version "+n);let l=t.u32(),a=t.u32(),y=t.u32(),f=[];for(let r=0;r<y;r++){let h=t.u32(),u=t.u32(),g=t.u32(),M=t.u32(),c=t.f32s(h),w=t.f32s(h),m=t.ibits(h*u*g*g,e),b=new Float32Array(m.length),v=u*g*g;for(let A=0;A<h;A++)for(let d=0;d<v;d++)b[A*v+d]=m[A*v+d]*c[A];f.push({cout:h,cin:u,k:g,pool:M,w:b,bias:w})}let o=Q(t,e);return{size:l,dim:a,convs:f,lin:o,bits:e}}function tt(s){let t=new Uint8Array(s.buffer?s.buffer:s,s.byteOffset||0,s.byteLength),i=String.fromCharCode(...t.subarray(0,6)),e=K[i];if(!e)throw new Error("not an MRCNN model");if(e===8)return t;let n=$(s);n.str(6);let l=n.u32(),a=n.u32(),y=n.u32(),f=n.u32(),o=[],r=A=>{let d=new Uint8Array(4);return new DataView(d.buffer).setUint32(0,A,!0),d},h=A=>new Uint8Array(A.buffer,A.byteOffset,A.byteLength);o.push(new TextEncoder().encode("MRCNN1"),r(l),r(a),r(y),r(f));for(let A=0;A<f;A++){let d=n.u32(),x=n.u32(),I=n.u32(),C=n.u32(),N=n.f32s(d),L=n.f32s(d),F=n.ibits(d*x*I*I,e);o.push(r(d),r(x),r(I),r(C),h(N),h(L),new Uint8Array(F.buffer,F.byteOffset,F.length))}let u=n.u32(),g=n.u32(),M=n.f32s(u),c=n.f32s(u),w=n.ibits(u*g,e);o.push(r(u),r(g),h(M),h(c),new Uint8Array(w.buffer,w.byteOffset,w.length));let m=o.reduce((A,d)=>A+d.length,0),b=new Uint8Array(m),v=0;for(let A of o)b.set(A,v),v+=A.length;return b}function et(s){return Q($(s))}function ht(s,t,i,e){let{cout:n,cin:l,w:a,bias:y}=s,f=i*i,o=i-1;for(let r=0;r<n;r++){let h=r*f,u=y[r],g=r*l*9;e.fill(u,h,h+f);for(let c=0;c<l;c++){let w=c*f,m=g+c*9,b=a[m],v=a[m+1],A=a[m+2],d=a[m+3],x=a[m+4],I=a[m+5],C=a[m+6],N=a[m+7],L=a[m+8];for(let F=1;F<o;F++){let _=w+(F-1)*i,S=w+F*i,p=w+(F+1)*i,R=h+F*i;e[R]+=v*t[_]+A*t[_+1]+x*t[S]+I*t[S+1]+N*t[p]+L*t[p+1];for(let E=1;E<o;E++)e[R+E]+=b*t[_+E-1]+v*t[_+E]+A*t[_+E+1]+d*t[S+E-1]+x*t[S+E]+I*t[S+E+1]+C*t[p+E-1]+N*t[p+E]+L*t[p+E+1];e[R+o]+=b*t[_+o-1]+v*t[_+o]+d*t[S+o-1]+x*t[S+o]+C*t[p+o-1]+N*t[p+o]}{let F=w,_=w+i,S=h;e[S]+=x*t[F]+I*t[F+1]+N*t[_]+L*t[_+1];for(let p=1;p<o;p++)e[S+p]+=d*t[F+p-1]+x*t[F+p]+I*t[F+p+1]+C*t[_+p-1]+N*t[_+p]+L*t[_+p+1];e[S+o]+=d*t[F+o-1]+x*t[F+o]+C*t[_+o-1]+N*t[_+o]}{let F=w+(o-1)*i,_=w+o*i,S=h+o*i;e[S]+=v*t[F]+A*t[F+1]+x*t[_]+I*t[_+1];for(let p=1;p<o;p++)e[S+p]+=b*t[F+p-1]+v*t[F+p]+A*t[F+p+1]+d*t[_+p-1]+x*t[_+p]+I*t[_+p+1];e[S+o]+=b*t[F+o-1]+v*t[F+o]+d*t[_+o-1]+x*t[_+o]}}let M=h+f;for(let c=h;c<M;c++)e[c]<0&&(e[c]=0)}}function ut(s,t,i,e){let n=t>>1;for(let l=0;l<i;l++){let a=l*t*t,y=l*n*n;for(let f=0;f<n;f++){let o=a+(f<<1)*t,r=o+t,h=y+f*n;for(let u=0;u<n;u++){let g=o+(u<<1),M=r+(u<<1),c=s[g],w=s[g+1],m=s[M],b=s[M+1];w>c&&(c=w),m>c&&(c=m),b>c&&(c=b),e[h+u]=c}}}}function nt(s,t){let i=s.size;if(!s._bufA){let c=i*i,w=i;for(let m of s.convs)m.cout*w*w>c&&(c=m.cout*w*w),m.pool&&(w>>=1);s._bufA=new Float32Array(c),s._bufB=new Float32Array(c),s._gap=new Float32Array(s.lin.in)}let e=s._bufA,n=s._bufB,l=i,a=1;e.set(t);for(let c of s.convs){ht(c,e,l,n);let w=e;e=n,n=w,a=c.cout,c.pool&&(ut(e,l,a,n),w=e,e=n,n=w,l>>=1)}let y=l*l,f=1/y,o=s._gap;for(let c=0;c<a;c++){let w=0,m=c*y;for(let b=0;b<y;b++)w+=e[m+b];o[c]=w*f}let{lin:r}=s,h=r.w,u=r.bias,g=r.in,M=new Float32Array(r.out);for(let c=0;c<r.out;c++){let w=u[c],m=c*g;for(let b=0;b<g;b++)w+=h[m+b]*o[b];M[c]=w}return M}function ot(s){let t=0;for(let e=0;e<s.length;e++)t+=s[e]*s[e];t=1/(Math.sqrt(t)+1e-9);let i=new Float32Array(s.length);for(let e=0;e<s.length;e++)i[e]=s[e]*t;return i}function j(s,t){let i=new Float32Array(s.out);for(let e=0;e<s.out;e++){let n=s.bias[e];for(let l=0;l<s.in;l++){let a=t[l];a>0&&(n+=s.w[e*s.in+l]*a)}i[e]=n}return i}function H(s){let t=-1/0;for(let n of s)n>t&&(t=n);let i=new Float32Array(s.length),e=0;for(let n=0;n<s.length;n++)i[n]=Math.exp(s[n]-t),e+=i[n];for(let n=0;n<s.length;n++)i[n]/=e;return i}var z=null;function st(s,t=120,i=7,e=10){let n=1/0,l=1/0,a=-1/0,y=-1/0;for(let d of s)for(let x of d)x.x<n&&(n=x.x),x.x>a&&(a=x.x),x.y<l&&(l=x.y),x.y>y&&(y=x.y);let f=t+2*e,o=z&&z.length===f*f?z:z=new Float32Array(f*f);if(o.fill(0),!(n<=a))return{ink:o,W:f,iMinX:0,iMaxX:-1,iMinY:0,iMaxY:-1};let r=t/Math.max(a-n,y-l,1),h=i*.5,u=h+.5,g=h-.5,M=u*u,c=g*g,w=f,m=-1,b=f,v=-1,A=(d,x,I,C)=>{let N=Math.max(0,Math.floor(Math.min(d,I)-u)),L=Math.min(f-1,Math.ceil(Math.max(d,I)+u)),F=Math.max(0,Math.floor(Math.min(x,C)-u)),_=Math.min(f-1,Math.ceil(Math.max(x,C)+u)),S=I-d,p=C-x,R=S*S+p*p,E=R>1e-6?1/R:0;for(let k=F;k<=_;k++){let P=k+.5-x,B=k*f;for(let O=N;O<=L;O++){let D=o[B+O];if(D>=1)continue;let V=O+.5-d,T=(V*S+P*p)*E;T<0?T=0:T>1&&(T=1);let J=V-T*S,G=P-T*p,Y=J*J+G*G;if(Y>=M)continue;let q=Y<=c?1:u-Math.sqrt(Y);q>D&&(o[B+O]=q,q>.15&&(O<w&&(w=O),O>m&&(m=O),k<b&&(b=k),k>v&&(v=k)))}}};for(let d of s){if(!d.length)continue;let x=(d[0].x-n)*r+e,I=(d[0].y-l)*r+e;if(d.length===1){A(x,I,x,I);continue}for(let C=1;C<d.length;C++){let N=(d[C].x-n)*r+e,L=(d[C].y-l)*r+e;A(x,I,N,L),x=N,I=L}}return{ink:o,W:f,iMinX:w,iMaxX:m,iMinY:b,iMaxY:v}}function rt({ink:s,W:t,iMinX:i,iMaxX:e,iMinY:n,iMaxY:l},a=32,y=Math.round(a*.75)){let f=new Float32Array(a*a);if(e<0)return f;let o=e-i+1,r=l-n+1,h=y/Math.max(r,o),u=Math.max(1,Math.round(r*h)),g=Math.max(1,Math.round(o*h)),M=o/g,c=r/u,w=a-u>>1,m=a-g>>1;for(let b=0;b<u;b++){let v=b*c,A=(b+1)*c,d=Math.floor(v),x=Math.min(r,Math.ceil(A)),I=(b+w)*a+m;for(let C=0;C<g;C++){let N=C*M,L=(C+1)*M,F=Math.floor(N),_=Math.min(o,Math.ceil(L)),S=0,p=0;for(let R=d;R<x;R++){let E=Math.min(A,R+1)-Math.max(v,R);if(E<=0)continue;let k=(R+n)*t+i;for(let P=F;P<_;P++){let B=Math.min(L,P+1)-Math.max(N,P);if(B<=0)continue;let O=B*E;S+=s[k+P]*O,p+=O}}f[I+C]=p>0?S/p:0}}return f}function it(s){let t=s?String(s):"";return t?(t.endsWith("/")||(t+="/"),typeof location<"u"?new URL(t,location.href).href:t):typeof location<"u"?new URL("./",location.href).href:""}var U=s=>Array.isArray(s)&&s.length&&!Array.isArray(s[0])?[s]:s,W=(s,t,i,e)=>new Error(`millirec: ${s} is not ${t} \u2014 the package files are not served at ${i}; copy node_modules/millirec/dist/${e}/ and pass assetBase (see README "Serving the files")`);async function at(s,{as:t="json",what:i,base:e,recognizer:n}){let l=r=>{let h=W(s,i,e,n);return r&&(h.message+=` (${r})`),h},a;try{a=await fetch(s)}catch(r){throw l(r&&r.message)}let y=a.headers&&a.headers.get("content-type")||"";if(!a.ok)throw l(`HTTP ${a.status}`);if(t==="json")try{return await a.json()}catch(r){throw l(/html/i.test(y)?"an HTML page":r&&r.message)}let f=await a.arrayBuffer(),o=String.fromCharCode(...new Uint8Array(f,0,Math.min(f.byteLength,16))).trimStart().toLowerCase();if(o.startsWith("<!doctype")||o.startsWith("<html"))throw l("an HTML page");return f}var lt=s=>s.every(t=>Array.isArray(t)&&t.every(i=>i&&Number.isFinite(i.x)&&Number.isFinite(i.y)));function yt(){let s=typeof document<"u"&&document.currentScript&&document.currentScript.src||"";return s?s.slice(0,s.lastIndexOf("/")+1):""}var gt=yt(),ct=(s,t)=>at(s,{as:"buf",what:"a char-cnn model file",base:t,recognizer:"char-cnn"}),wt="millirec: recognizer destroyed",dt=18,ft=s=>{let t=0;for(let e=0;e<s.length;e++)t+=s[e]*s[e];t=Math.sqrt(t)||1;let i=new Float32Array(s.length);for(let e=0;e<s.length;e++)i[e]=s[e]/t;return i},X=class{static async create(t={}){let e={...(typeof window<"u"?window:globalThis).MILLIREC_CNN||{},...t};if(!e.stages||!e.stages.length)throw new Error("MillirecCharCnn: no stages configured");if(typeof fetch>"u"||typeof document>"u"&&typeof OffscreenCanvas>"u")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");let n=new this(e);return await n.ready,e.examples&&await n.teach(e.examples),n}constructor(t){this.config=t,this.base=it(t.assetBase||gt),this.classes=typeof t.classes=="string"?t.classes.split(""):t.classes,this.stages=[],this.ready=this.load()}async load(){let{base:t,config:i}=this,e=null;if(i.kernel){let n=t+i.kernel;try{e=(await import(n)).default}catch(l){let a=W(n,"the char-cnn kernel",t,"char-cnn");throw a.message+=` (${l&&l.message})`,a}}for(let n of i.stages){let[l,a]=await Promise.all([ct(t+n.model,t),ct(t+n.head,t)]);if(this.destroyed)return;let y=et(a),f={theta:n.theta==null?null:n.theta,head:y,headNorm:n.headNorm!==!1,size:0};if(e){let o=await e();if(this.destroyed)return;let r=tt(new Uint8Array(l)),h=o._malloc(r.byteLength);o.HEAPU8.set(r,h);let u=o._cnnLoad(h,r.byteLength);if(o._free(h),!u)throw new Error(`kernel rejected ${n.model} (channel widths must be multiples of four)`);let g=o._cnnSize(),M=o._cnnDim();f.size=g,f.embed=c=>{let w=o._cnnInputPtr(1)>>2;o.HEAPF32.set(c,w),o._cnnRun(1);let m=o._cnnEmbedPtr(1)>>2;return o.HEAPF32.slice(m,m+M)},f.logits=c=>j(f.head,f.embed(c))}else{let o=Z(l);f.size=o.size,f.embed=r=>{let h=nt(o,r);return f.headNorm?ot(h):h},f.logits=r=>j(f.head,f.embed(r))}this.stages.push(f)}}get engine(){return"char-cnn"}raster(t,i){return this._planeFor!==t&&(this._plane=st(t),this._planeFor=t),rt(this._plane,i,Math.round(i*.75))}async recognize(t,{only:i=null}={}){if(t=U(t),!t||!t.length||!lt(t)||(await this.ready,this.destroyed||!this.stages.length))return null;this._planeFor=null;let e=i?new Set(i):null;if(this.taught)return this.nearest(t,e);let n=null,l=0;for(let r=0;r<this.stages.length;r++){let h=this.stages[r];if((this.personal||e)&&r<this.stages.length-1)continue;if(n=h.logits(this.raster(t,h.size)),l=r,h.theta==null||r===this.stages.length-1)break;let u=-1/0,g=-1/0;for(let M of n)M>u?(g=u,u=M):M>g&&(g=M);if(u-g>=h.theta)break}let a=[...n.keys()];if(e&&(a=a.filter(r=>e.has(this.classes[r])),!a.length)){let r=[];return r.stage=l,r}let y=H(a.map(r=>n[r])),o=[...y.keys()].sort((r,h)=>y[h]-y[r]).slice(0,3).map(r=>({char:this.classes[a[r]],prob:y[r]}));return o.stage=l,o}nearest(t,i){let e=this.stages.length-1,n=this.stages[e],l=ft(n.embed(this.raster(t,n.size))),a=[...this.taught.keys()].filter(f=>!i||i.has(f)),y=[];if(a.length){let f=a.map(r=>{let h=-2;for(let u of this.taught.get(r)){let g=0;for(let M=0;M<l.length;M++)g+=l[M]*u[M];g>h&&(h=g)}return h}),o=H(f.map(r=>r*dt));for(let r of[...o.keys()].sort((h,u)=>o[u]-o[h]).slice(0,3))y.push({char:a[r],prob:o[r]})}return y.stage=e,y}async teach(t,{add:i=!1}={}){if(await this.ready,this.destroyed||!this.stages.length)return null;let e=i&&this.taught?new Map([...this.taught].map(([n,l])=>[n,l.slice()])):new Map;for(let[n,l]of Object.entries(t||{}))for(let a of l||[]){let y=U(a);if(!y||!y.length)continue;let f=ft(await this.embedding(y));if(this.destroyed)return null;e.has(n)||e.set(n,[]),e.get(n).push(f)}return e.size?(this.taught=e,{classes:[...e.keys()],n:[...e.values()].reduce((n,l)=>n+l.length,0)}):(i||(this.taught=null),null)}forget(){this.taught=null}async embedding(t){if(t=U(t),!t||!t.length)throw new TypeError("MillirecCharCnn: embedding() needs at least one stroke");if(!lt(t))throw new TypeError("MillirecCharCnn: embedding() got a point that is not a finite number");if(await this.ready,this.destroyed||!this.stages.length)throw new Error(wt);this._planeFor=null;let i=this.stages[this.stages.length-1];return i.embed(this.raster(t,i.size))}async personalize(t,{steps:i=250,lr:e=.5,prior:n=1}={}){if(await this.ready,this.destroyed||!this.stages.length)return null;let l=this.stages[this.stages.length-1],a=l.head0||(l.head0=l.head),y=[],f=[];for(let[g,M]of Object.entries(t||{})){let c=this.classes.indexOf(g);if(!(c<0))for(let w of M||[]){let m=U(w);!m||!m.length||(y.push(await this.embedding(m)),f.push(c))}}if(this.destroyed)return null;if(!y.length)return l.head=a,this.personal=null,null;let o={in:a.in,out:a.out,w:Float32Array.from(a.w),bias:Float32Array.from(a.bias)},r=y.length,h=new Float32Array(o.w.length),u=new Float32Array(o.out);for(let g=0;g<i;g++){h.fill(0),u.fill(0);for(let c=0;c<r;c++){let w=H(j(o,y[c])),m=y[c];for(let b=0;b<o.out;b++){let v=(w[b]-(b===f[c]?1:0))/r;if(!v)continue;u[b]+=v;let A=b*o.in;for(let d=0;d<o.in;d++){let x=m[d];x>0&&(h[A+d]+=v*x)}}}let M=n/r;for(let c=0;c<o.w.length;c++)o.w[c]-=e*(h[c]+M*(o.w[c]-a.w[c]));for(let c=0;c<o.out;c++)o.bias[c]-=e*(u[c]+M*(o.bias[c]-a.bias[c]))}return l.head=o,this.personal={n:r,classes:[...new Set(f)].map(g=>this.classes[g]).sort()},this.personal}destroy(){this.destroyed=!0,this.stages=[],this.personal=null,this.taught=null,this._plane=null,this._planeFor=null}depersonalize(){let t=this.stages[this.stages.length-1];t&&t.head0&&(t.head=t.head0),this.personal=null}};typeof window<"u"&&typeof __MILLIREC_ESM__>"u"&&(window.MillirecCharCnn=X);})();