millirec 0.0.0-stage → 0.1.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 (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +156 -2
  3. package/dist/char-cnn/base-p25.bin +0 -0
  4. package/dist/char-cnn/base-p25.head.bin +0 -0
  5. package/dist/char-cnn/base.head.bin +0 -0
  6. package/dist/char-cnn/base.q4.bin +0 -0
  7. package/dist/char-cnn/cnn-web.js +8 -0
  8. package/dist/char-cnn/cnn-web.wasm +0 -0
  9. package/dist/char-cnn/core.d.ts +35 -0
  10. package/dist/char-cnn/core.js +536 -0
  11. package/dist/char-cnn/index.d.ts +9 -0
  12. package/dist/char-cnn/index.js +1009 -0
  13. package/dist/char-cnn/millirec-char-cnn-core.min.js +1 -0
  14. package/dist/char-cnn/millirec-char-cnn.min.js +1 -0
  15. package/dist/char-cnn/options/accurate.js +17 -0
  16. package/dist/char-cnn/options/accurate.json +15 -0
  17. package/dist/char-cnn/options/fast.js +17 -0
  18. package/dist/char-cnn/options/fast.json +15 -0
  19. package/dist/char-cnn/options/small.js +11 -0
  20. package/dist/char-cnn/options/small.json +9 -0
  21. package/dist/char-cnn/tiny.bin +0 -0
  22. package/dist/char-cnn/tiny.head.bin +0 -0
  23. package/dist/char-lite/index.d.ts +77 -0
  24. package/dist/char-lite/index.js +894 -0
  25. package/dist/char-lite/millirec-char-lite.min.js +1 -0
  26. package/dist/char-lite/millirec.wasm +0 -0
  27. package/dist/char-lite/sets/all.js +2 -0
  28. package/dist/char-lite/sets/all.json +1 -0
  29. package/dist/char-lite/sets/capitals.js +2 -0
  30. package/dist/char-lite/sets/capitals.json +1 -0
  31. package/dist/char-lite/sets/digits.js +2 -0
  32. package/dist/char-lite/sets/digits.json +1 -0
  33. package/dist/char-lite/sets/lowercase.js +2 -0
  34. package/dist/char-lite/sets/lowercase.json +1 -0
  35. package/dist/char-lite/sets/punctuation.js +2 -0
  36. package/dist/char-lite/sets/punctuation.json +1 -0
  37. package/dist/char-lite/sets.json +163 -0
  38. package/dist/char-lite/worker-stroke.js +11 -0
  39. package/dist/overlap/millirec-overlap.min.js +45 -0
  40. package/dist/overlap/millirec.wasm +0 -0
  41. package/dist/overlap/training.all.json +1 -0
  42. package/dist/overlap/training.capitals.json +1 -0
  43. package/dist/overlap/training.digits.json +1 -0
  44. package/dist/overlap/training.lowercase.json +1 -0
  45. package/dist/overlap/training.punctuation.json +1 -0
  46. package/dist/overlap/words.json +1 -0
  47. package/package.json +53 -4
@@ -0,0 +1 @@
1
+ "use strict";(()=>{var H={MRCNN1:8,MRCNN3:3,MRCNN4:4,MRCNN5:5,MRCNN6:6};function O(s){let n=new DataView(s.buffer?s.buffer:s,s.byteOffset||0,s.byteLength),l=new Uint8Array(n.buffer,n.byteOffset,n.byteLength),o=0;return{u32:()=>{let t=n.getUint32(o,!0);return o+=4,t},f32s:t=>{let r=new Float32Array(t);for(let f=0;f<t;f++)r[f]=n.getFloat32(o,!0),o+=4;return r},i8s:t=>{let r=new Int8Array(t);for(let f=0;f<t;f++)r[f]=l[o+f]<<24>>24;return o+=t,r},ibits:(t,r)=>{if(r===8){let i=new Int8Array(t);for(let e=0;e<t;e++)i[e]=l[o+e]<<24>>24;return o+=t,i}let f=new Int8Array(t),d=(1<<r)-1,u=1<<r-1;for(let i=0;i<t;i++){let e=i*r,c=o+(e>>3),a=e&7,w=(l[c]|l[c+1]<<8)>>a&d;f[i]=w&u?w-(1<<r):w}return o+=t*r+7>>3,f},str:t=>{let r=String.fromCharCode(...l.subarray(o,o+t));return o+=t,r},done:()=>o>=n.byteLength}}function T(s,n=8){let l=s.u32(),o=s.u32(),t=s.f32s(l),r=s.f32s(l),f=s.ibits(l*o,n),d=new Float32Array(l*o);for(let u=0;u<l;u++)for(let i=0;i<o;i++)d[u*o+i]=f[u*o+i]*t[u];return{out:l,in:o,w:d,bias:r}}function X(s){let n=O(s),l=n.str(6),o=H[l];if(!o)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(),f=n.u32(),d=n.u32(),u=[];for(let e=0;e<d;e++){let c=n.u32(),a=n.u32(),w=n.u32(),x=n.u32(),y=n.f32s(c),m=n.f32s(c),A=n.ibits(c*a*w*w,o),I=new Float32Array(A.length),M=a*w*w;for(let p=0;p<c;p++)for(let b=0;b<M;b++)I[p*M+b]=A[p*M+b]*y[p];u.push({cout:c,cin:a,k:w,pool:x,w:I,bias:m})}let i=T(n,o);return{size:r,dim:f,convs:u,lin:i,bits:o}}function Y(s){let n=new Uint8Array(s.buffer?s.buffer:s,s.byteOffset||0,s.byteLength),l=String.fromCharCode(...n.subarray(0,6)),o=H[l];if(!o)throw new Error("not an MRCNN model");if(o===8)return n;let t=O(s);t.str(6);let r=t.u32(),f=t.u32(),d=t.u32(),u=t.u32(),i=[],e=p=>{let b=new Uint8Array(4);return new DataView(b.buffer).setUint32(0,p,!0),b},c=p=>new Uint8Array(p.buffer,p.byteOffset,p.byteLength);i.push(new TextEncoder().encode("MRCNN1"),e(r),e(f),e(d),e(u));for(let p=0;p<u;p++){let b=t.u32(),h=t.u32(),g=t.u32(),_=t.u32(),k=t.f32s(b),N=t.f32s(b),F=t.ibits(b*h*g*g,o);i.push(e(b),e(h),e(g),e(_),c(k),c(N),new Uint8Array(F.buffer,F.byteOffset,F.length))}let a=t.u32(),w=t.u32(),x=t.f32s(a),y=t.f32s(a),m=t.ibits(a*w,o);i.push(e(a),e(w),c(x),c(y),new Uint8Array(m.buffer,m.byteOffset,m.length));let A=i.reduce((p,b)=>p+b.length,0),I=new Uint8Array(A),M=0;for(let p of i)I.set(p,M),M+=p.length;return I}function q(s){return T(O(s))}function nt(s,n,l,o){let{cout:t,cin:r,k:f,w:d,bias:u}=s,i=l*l,e=f-1>>1,c=r*f*f;for(let a=0;a<t;a++){let w=a*i,x=u[a],y=a*c;for(let m=0;m<l;m++)for(let A=0;A<l;A++){let I=x;for(let M=0;M<r;M++){let p=M*i,b=y+M*f*f;for(let h=0;h<f;h++){let g=m+h-e;if(g<0||g>=l)continue;let _=p+g*l,k=b+h*f;for(let N=0;N<f;N++){let F=A+N-e;F<0||F>=l||(I+=d[k+N]*n[_+F])}}}o[w+m*l+A]=I>0?I:0}}}function et(s,n,l,o){let t=n>>1;for(let r=0;r<l;r++){let f=r*n*n,d=r*t*t;for(let u=0;u<t;u++)for(let i=0;i<t;i++){let e=f+2*u*n+2*i,c=s[e],a=s[e+1],w=s[e+n],x=s[e+n+1];o[d+u*t+i]=Math.max(c,a,w,x)}}}function W(s,n){let l=s.size,o=Float32Array.from(n),t=l,r=1;for(let e of s.convs){let c=new Float32Array(e.cout*t*t);if(nt(e,o,t,c),o=c,r=e.cout,e.pool){let a=new Float32Array(r*(t>>1)*(t>>1));et(o,t,r,a),o=a,t>>=1}}let f=t*t,d=new Float32Array(r);for(let e=0;e<r;e++){let c=0;for(let a=0;a<f;a++)c+=o[e*f+a];d[e]=c/f}let{lin:u}=s,i=new Float32Array(u.out);for(let e=0;e<u.out;e++){let c=u.bias[e];for(let a=0;a<u.in;a++)c+=u.w[e*u.in+a]*d[a];i[e]=c}return i}function K(s){let n=0;for(let o=0;o<s.length;o++)n+=s[o]*s[o];n=1/(Math.sqrt(n)+1e-9);let l=new Float32Array(s.length);for(let o=0;o<s.length;o++)l[o]=s[o]*n;return l}function C(s,n){let l=new Float32Array(s.out);for(let o=0;o<s.out;o++){let t=s.bias[o];for(let r=0;r<s.in;r++){let f=n[r];f>0&&(t+=s.w[o*s.in+r]*f)}l[o]=t}return l}function B(s){let n=-1/0;for(let t of s)t>n&&(n=t);let l=new Float32Array(s.length),o=0;for(let t=0;t<s.length;t++)l[t]=Math.exp(s[t]-n),o+=l[t];for(let t=0;t<s.length;t++)l[t]/=o;return l}function Z(s,n,l=120,o=7,t=10){let r=1/0,f=1/0,d=-1/0,u=-1/0;for(let c of n)for(let a of c)a.x<r&&(r=a.x),a.x>d&&(d=a.x),a.y<f&&(f=a.y),a.y>u&&(u=a.y);let i=l/Math.max(d-r,u-f,1),e=l+2*t;s.fillStyle="#fff",s.fillRect(0,0,e,e),s.strokeStyle="#000",s.lineWidth=o,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-f)*i+t);for(let a=1;a<c.length;a++)s.lineTo((c[a].x-r)*i+t,(c[a].y-f)*i+t);s.stroke()}return e}function $(s,n,l){let o=new Float32Array(n*l);for(let t=0,r=0;t<o.length;t++,r+=4)o[t]=1-(s[r]*.299+s[r+1]*.587+s[r+2]*.114)/255;return o}function V(s,n,l,o=32,t=24){let r=1/0,f=-1,d=1/0,u=-1;for(let h=0;h<l;h++)for(let g=0;g<n;g++)s[h*n+g]>.15&&(g<r&&(r=g),g>f&&(f=g),h<d&&(d=h),h>u&&(u=h));let i=new Float32Array(o*o);if(f<0)return i;let e=f-r+1,c=u-d+1,a=new Float32Array(e*c);for(let h=0;h<c;h++)for(let g=0;g<e;g++)a[h*e+g]=s[(h+d)*n+(g+r)];let w=0;for(let h=0;h<a.length;h++)a[h]>w&&(w=a[h]);if(w>.05&&w<.999)for(let h=0;h<a.length;h++)a[h]=Math.min(1,a[h]/w);let x=t/Math.max(c,e),y=Math.max(1,Math.round(c*x)),m=Math.max(1,Math.round(e*x)),A=new Float32Array(m*y),I=e/m,M=c/y;for(let h=0;h<y;h++){let g=h*M,_=(h+1)*M,k=Math.floor(g),N=Math.min(c,Math.ceil(_));for(let F=0;F<m;F++){let z=F*I,j=(F+1)*I,Q=Math.floor(z),tt=Math.min(e,Math.ceil(j)),D=0,L=0;for(let S=k;S<N;S++){let R=Math.min(_,S+1)-Math.max(g,S);if(!(R<=0))for(let E=Q;E<tt;E++){let U=Math.min(j,E+1)-Math.max(z,E);U<=0||(D+=a[S*e+E]*U*R,L+=U*R)}}A[h*m+F]=L>0?D/L:0}}let p=o-y>>1,b=o-m>>1;for(let h=0;h<y;h++)for(let g=0;g<m;g++)i[(h+p)*o+g+b]=A[h*m+g];return i}function J(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 v=s=>Array.isArray(s)&&s.length&&!Array.isArray(s[0])?[s]:s;function ot(){let s=typeof document<"u"&&document.currentScript&&document.currentScript.src||"";return s?s.slice(0,s.lastIndexOf("/")+1):""}var st=ot(),G=s=>fetch(s).then(n=>{if(!n.ok)throw new Error(`${s}: ${n.status}`);return n.arrayBuffer()}),P=class{static async create(n={}){let o={...(typeof window<"u"?window:globalThis).MILLIREC_CNN||{},...n};if(!o.stages||!o.stages.length)throw new Error("MillirecCharCnn: no stages configured");let t=new this(o);return await t.ready,t}constructor(n){this.config=n,this.base=J(n.assetBase||st),this.classes=typeof n.classes=="string"?n.classes.split(""):n.classes,this.stages=[],this.off=typeof document<"u"?document.createElement("canvas"):null,this.off&&(this.off.width=this.off.height=140,this.octx=this.off.getContext("2d",{willReadFrequently:!0})),this.ready=this.load()}async load(){let{base:n,config:l}=this,o=null;l.kernel&&(o=(await import(n+l.kernel)).default);for(let t of l.stages){let[r,f]=await Promise.all([G(n+t.model),G(n+t.head)]),d=q(f),u={theta:t.theta==null?null:t.theta,head:d,headNorm:t.headNorm!==!1,size:0};if(o){let i=await o(),e=Y(new Uint8Array(r)),c=i._malloc(e.byteLength);i.HEAPU8.set(e,c);let a=i._cnnLoad(c,e.byteLength);if(i._free(c),!a)throw new Error(`kernel rejected ${t.model} (channel widths must be multiples of four)`);let w=i._cnnSize(),x=i._cnnDim();u.size=w,u.embed=y=>{let m=i._cnnInputPtr(1)>>2;i.HEAPF32.set(y,m),i._cnnRun(1);let A=i._cnnEmbedPtr(1)>>2;return i.HEAPF32.slice(A,A+x)},u.logits=y=>C(u.head,u.embed(y))}else{let i=X(r);u.size=i.size,u.embed=e=>{let c=W(i,e);return u.headNorm?K(c):c},u.logits=e=>C(u.head,u.embed(e))}this.stages.push(u)}}raster(n,l){if(this._planeFor!==n){let r=Z(this.octx,n);this._plane={ink:$(this.octx.getImageData(0,0,r,r).data,r,r),W:r},this._planeFor=n}let{ink:o,W:t}=this._plane;return V(o,t,t,l,Math.round(l*.75))}async recognize(n,{only:l=null}={}){if(n=v(n),!n||!n.length||(await this.ready,!this.stages.length))return null;this._planeFor=null;let o=l?new Set(l):null,t=null,r=0;for(let e=0;e<this.stages.length;e++){let c=this.stages[e];if((this.personal||o)&&e<this.stages.length-1)continue;if(t=c.logits(this.raster(n,c.size)),r=e,c.theta==null||e===this.stages.length-1)break;let a=-1/0,w=-1/0;for(let x of t)x>a?(w=a,a=x):x>w&&(w=x);if(a-w>=c.theta)break}let f=[...t.keys()];if(o&&(f=f.filter(e=>o.has(this.classes[e])),!f.length)){let e=[];return e.stage=r,e}let d=B(f.map(e=>t[e])),i=[...d.keys()].sort((e,c)=>d[c]-d[e]).slice(0,3).map(e=>({char:this.classes[f[e]],prob:d[e]}));return i.stage=r,i}async embedding(n){n=v(n),await this.ready,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:o=.5,prior:t=1}={}){await this.ready;let r=this.stages[this.stages.length-1],f=r.head0||(r.head0=r.head),d=[],u=[];for(let[w,x]of Object.entries(n||{})){let y=this.classes.indexOf(w);if(!(y<0))for(let m of x||[]){let A=v(m);!A||!A.length||(d.push(await this.embedding(A)),u.push(y))}}if(!d.length)return r.head=f,this.personal=null,null;let i={in:f.in,out:f.out,w:Float32Array.from(f.w),bias:Float32Array.from(f.bias)},e=d.length,c=new Float32Array(i.w.length),a=new Float32Array(i.out);for(let w=0;w<l;w++){c.fill(0),a.fill(0);for(let y=0;y<e;y++){let m=B(C(i,d[y])),A=d[y];for(let I=0;I<i.out;I++){let M=(m[I]-(I===u[y]?1:0))/e;if(!M)continue;a[I]+=M;let p=I*i.in;for(let b=0;b<i.in;b++){let h=A[b];h>0&&(c[p+b]+=M*h)}}}let x=t/e;for(let y=0;y<i.w.length;y++)i.w[y]-=o*(c[y]+x*(i.w[y]-f.w[y]));for(let y=0;y<i.out;y++)i.bias[y]-=o*(a[y]+x*(i.bias[y]-f.bias[y]))}return r.head=i,this.personal={n:e,classes:[...new Set(u)].map(w=>this.classes[w]).sort()},this.personal}destroy(){this.stages=[],this.personal=null,this._plane=null,this._planeFor=null}depersonalize(){let n=this.stages[this.stages.length-1];n.head0&&(n.head=n.head0),this.personal=null}};typeof window<"u"&&typeof __MILLIREC_ESM__>"u"&&(window.MillirecCharCnn=P);})();
@@ -0,0 +1 @@
1
+ "use strict";(()=>{var $={MRCNN1:8,MRCNN3:3,MRCNN4:4,MRCNN5:5,MRCNN6:6};function W(r){let o=new DataView(r.buffer?r.buffer:r,r.byteOffset||0,r.byteLength),l=new Uint8Array(o.buffer,o.byteOffset,o.byteLength),n=0;return{u32:()=>{let t=o.getUint32(n,!0);return n+=4,t},f32s:t=>{let i=new Float32Array(t);for(let a=0;a<t;a++)i[a]=o.getFloat32(n,!0),n+=4;return i},i8s:t=>{let i=new Int8Array(t);for(let a=0;a<t;a++)i[a]=l[n+a]<<24>>24;return n+=t,i},ibits:(t,i)=>{if(i===8){let f=new Int8Array(t);for(let e=0;e<t;e++)f[e]=l[n+e]<<24>>24;return n+=t,f}let a=new Int8Array(t),m=(1<<i)-1,c=1<<i-1;for(let f=0;f<t;f++){let e=f*i,s=n+(e>>3),u=e&7,x=(l[s]|l[s+1]<<8)>>u&m;a[f]=x&c?x-(1<<i):x}return n+=t*i+7>>3,a},str:t=>{let i=String.fromCharCode(...l.subarray(n,n+t));return n+=t,i},done:()=>n>=o.byteLength}}function J(r,o=8){let l=r.u32(),n=r.u32(),t=r.f32s(l),i=r.f32s(l),a=r.ibits(l*n,o),m=new Float32Array(l*n);for(let c=0;c<l;c++)for(let f=0;f<n;f++)m[c*n+f]=a[c*n+f]*t[c];return{out:l,in:n,w:m,bias:i}}function V(r){let o=W(r),l=o.str(6),n=$[l];if(!n)throw new Error("not an MRCNN1/3/4/5/6 model");let t=o.u32();if(t!==1)throw new Error("unknown model version "+t);let i=o.u32(),a=o.u32(),m=o.u32(),c=[];for(let e=0;e<m;e++){let s=o.u32(),u=o.u32(),x=o.u32(),d=o.u32(),g=o.f32s(s),y=o.f32s(s),w=o.ibits(s*u*x*x,n),v=new Float32Array(w.length),A=u*x*x;for(let I=0;I<s;I++)for(let b=0;b<A;b++)v[I*A+b]=w[I*A+b]*g[I];c.push({cout:s,cin:u,k:x,pool:d,w:v,bias:y})}let f=J(o,n);return{size:i,dim:a,convs:c,lin:f,bits:n}}function Q(r){let o=new Uint8Array(r.buffer?r.buffer:r,r.byteOffset||0,r.byteLength),l=String.fromCharCode(...o.subarray(0,6)),n=$[l];if(!n)throw new Error("not an MRCNN model");if(n===8)return o;let t=W(r);t.str(6);let i=t.u32(),a=t.u32(),m=t.u32(),c=t.u32(),f=[],e=I=>{let b=new Uint8Array(4);return new DataView(b.buffer).setUint32(0,I,!0),b},s=I=>new Uint8Array(I.buffer,I.byteOffset,I.byteLength);f.push(new TextEncoder().encode("MRCNN1"),e(i),e(a),e(m),e(c));for(let I=0;I<c;I++){let b=t.u32(),p=t.u32(),M=t.u32(),L=t.u32(),_=t.f32s(b),C=t.f32s(b),k=t.ibits(b*p*M*M,n);f.push(e(b),e(p),e(M),e(L),s(_),s(C),new Uint8Array(k.buffer,k.byteOffset,k.length))}let u=t.u32(),x=t.u32(),d=t.f32s(u),g=t.f32s(u),y=t.ibits(u*x,n);f.push(e(u),e(x),s(d),s(g),new Uint8Array(y.buffer,y.byteOffset,y.length));let w=f.reduce((I,b)=>I+b.length,0),v=new Uint8Array(w),A=0;for(let I of f)v.set(I,A),A+=I.length;return v}function z(r){return J(W(r))}function yt(r,o,l,n){let{cout:t,cin:i,k:a,w:m,bias:c}=r,f=l*l,e=a-1>>1,s=i*a*a;for(let u=0;u<t;u++){let x=u*f,d=c[u],g=u*s;for(let y=0;y<l;y++)for(let w=0;w<l;w++){let v=d;for(let A=0;A<i;A++){let I=A*f,b=g+A*a*a;for(let p=0;p<a;p++){let M=y+p-e;if(M<0||M>=l)continue;let L=I+M*l,_=b+p*a;for(let C=0;C<a;C++){let k=w+C-e;k<0||k>=l||(v+=m[_+C]*o[L+k])}}}n[x+y*l+w]=v>0?v:0}}}function dt(r,o,l,n){let t=o>>1;for(let i=0;i<l;i++){let a=i*o*o,m=i*t*t;for(let c=0;c<t;c++)for(let f=0;f<t;f++){let e=a+2*c*o+2*f,s=r[e],u=r[e+1],x=r[e+o],d=r[e+o+1];n[m+c*t+f]=Math.max(s,u,x,d)}}}function tt(r,o){let l=r.size,n=Float32Array.from(o),t=l,i=1;for(let e of r.convs){let s=new Float32Array(e.cout*t*t);if(yt(e,n,t,s),n=s,i=e.cout,e.pool){let u=new Float32Array(i*(t>>1)*(t>>1));dt(n,t,i,u),n=u,t>>=1}}let a=t*t,m=new Float32Array(i);for(let e=0;e<i;e++){let s=0;for(let u=0;u<a;u++)s+=n[e*a+u];m[e]=s/a}let{lin:c}=r,f=new Float32Array(c.out);for(let e=0;e<c.out;e++){let s=c.bias[e];for(let u=0;u<c.in;u++)s+=c.w[e*c.in+u]*m[u];f[e]=s}return f}function nt(r){let o=0;for(let n=0;n<r.length;n++)o+=r[n]*r[n];o=1/(Math.sqrt(o)+1e-9);let l=new Float32Array(r.length);for(let n=0;n<r.length;n++)l[n]=r[n]*o;return l}function O(r,o){let l=new Float32Array(r.out);for(let n=0;n<r.out;n++){let t=r.bias[n];for(let i=0;i<r.in;i++){let a=o[i];a>0&&(t+=r.w[n*r.in+i]*a)}l[n]=t}return l}function H(r){let o=-1/0;for(let t of r)t>o&&(o=t);let l=new Float32Array(r.length),n=0;for(let t=0;t<r.length;t++)l[t]=Math.exp(r[t]-o),n+=l[t];for(let t=0;t<r.length;t++)l[t]/=n;return l}function et(r,o,l=120,n=7,t=10){let i=1/0,a=1/0,m=-1/0,c=-1/0;for(let s of o)for(let u of s)u.x<i&&(i=u.x),u.x>m&&(m=u.x),u.y<a&&(a=u.y),u.y>c&&(c=u.y);let f=l/Math.max(m-i,c-a,1),e=l+2*t;r.fillStyle="#fff",r.fillRect(0,0,e,e),r.strokeStyle="#000",r.lineWidth=n,r.lineCap="round",r.lineJoin="round";for(let s of o)if(!(s.length<2)){r.beginPath(),r.moveTo((s[0].x-i)*f+t,(s[0].y-a)*f+t);for(let u=1;u<s.length;u++)r.lineTo((s[u].x-i)*f+t,(s[u].y-a)*f+t);r.stroke()}return e}function ot(r,o,l){let n=new Float32Array(o*l);for(let t=0,i=0;t<n.length;t++,i+=4)n[t]=1-(r[i]*.299+r[i+1]*.587+r[i+2]*.114)/255;return n}function rt(r,o,l,n=32,t=24){let i=1/0,a=-1,m=1/0,c=-1;for(let p=0;p<l;p++)for(let M=0;M<o;M++)r[p*o+M]>.15&&(M<i&&(i=M),M>a&&(a=M),p<m&&(m=p),p>c&&(c=p));let f=new Float32Array(n*n);if(a<0)return f;let e=a-i+1,s=c-m+1,u=new Float32Array(e*s);for(let p=0;p<s;p++)for(let M=0;M<e;M++)u[p*e+M]=r[(p+m)*o+(M+i)];let x=0;for(let p=0;p<u.length;p++)u[p]>x&&(x=u[p]);if(x>.05&&x<.999)for(let p=0;p<u.length;p++)u[p]=Math.min(1,u[p]/x);let d=t/Math.max(s,e),g=Math.max(1,Math.round(s*d)),y=Math.max(1,Math.round(e*d)),w=new Float32Array(y*g),v=e/y,A=s/g;for(let p=0;p<g;p++){let M=p*A,L=(p+1)*A,_=Math.floor(M),C=Math.min(s,Math.ceil(L));for(let k=0;k<y;k++){let X=k*v,D=(k+1)*v,P=Math.floor(X),Y=Math.min(e,Math.ceil(D)),U=0,T=0;for(let N=_;N<C;N++){let Z=Math.min(L,N+1)-Math.max(M,N);if(!(Z<=0))for(let h=P;h<Y;h++){let E=Math.min(D,h+1)-Math.max(X,h);E<=0||(U+=u[N*e+h]*E*Z,T+=E*Z)}}w[p*y+k]=T>0?U/T:0}}let I=n-g>>1,b=n-y>>1;for(let p=0;p<g;p++)for(let M=0;M<y;M++)f[(p+I)*n+M+b]=w[p*y+M];return f}function st(r){let o=r?String(r):"";return o?(o.endsWith("/")||(o+="/"),typeof location<"u"?new URL(o,location.href).href:o):typeof location<"u"?new URL("./",location.href).href:""}var j=r=>Array.isArray(r)&&r.length&&!Array.isArray(r[0])?[r]:r;function xt(){let r=typeof document<"u"&&document.currentScript&&document.currentScript.src||"";return r?r.slice(0,r.lastIndexOf("/")+1):""}var mt=xt(),it=r=>fetch(r).then(o=>{if(!o.ok)throw new Error(`${r}: ${o.status}`);return o.arrayBuffer()}),B=class{static async create(o={}){let n={...(typeof window<"u"?window:globalThis).MILLIREC_CNN||{},...o};if(!n.stages||!n.stages.length)throw new Error("MillirecCharCnn: no stages configured");let t=new this(n);return await t.ready,t}constructor(o){this.config=o,this.base=st(o.assetBase||mt),this.classes=typeof o.classes=="string"?o.classes.split(""):o.classes,this.stages=[],this.off=typeof document<"u"?document.createElement("canvas"):null,this.off&&(this.off.width=this.off.height=140,this.octx=this.off.getContext("2d",{willReadFrequently:!0})),this.ready=this.load()}async load(){let{base:o,config:l}=this,n=null;l.kernel&&(n=(await import(o+l.kernel)).default);for(let t of l.stages){let[i,a]=await Promise.all([it(o+t.model),it(o+t.head)]),m=z(a),c={theta:t.theta==null?null:t.theta,head:m,headNorm:t.headNorm!==!1,size:0};if(n){let f=await n(),e=Q(new Uint8Array(i)),s=f._malloc(e.byteLength);f.HEAPU8.set(e,s);let u=f._cnnLoad(s,e.byteLength);if(f._free(s),!u)throw new Error(`kernel rejected ${t.model} (channel widths must be multiples of four)`);let x=f._cnnSize(),d=f._cnnDim();c.size=x,c.embed=g=>{let y=f._cnnInputPtr(1)>>2;f.HEAPF32.set(g,y),f._cnnRun(1);let w=f._cnnEmbedPtr(1)>>2;return f.HEAPF32.slice(w,w+d)},c.logits=g=>O(c.head,c.embed(g))}else{let f=V(i);c.size=f.size,c.embed=e=>{let s=tt(f,e);return c.headNorm?nt(s):s},c.logits=e=>O(c.head,c.embed(e))}this.stages.push(c)}}raster(o,l){if(this._planeFor!==o){let i=et(this.octx,o);this._plane={ink:ot(this.octx.getImageData(0,0,i,i).data,i,i),W:i},this._planeFor=o}let{ink:n,W:t}=this._plane;return rt(n,t,t,l,Math.round(l*.75))}async recognize(o,{only:l=null}={}){if(o=j(o),!o||!o.length||(await this.ready,!this.stages.length))return null;this._planeFor=null;let n=l?new Set(l):null,t=null,i=0;for(let e=0;e<this.stages.length;e++){let s=this.stages[e];if((this.personal||n)&&e<this.stages.length-1)continue;if(t=s.logits(this.raster(o,s.size)),i=e,s.theta==null||e===this.stages.length-1)break;let u=-1/0,x=-1/0;for(let d of t)d>u?(x=u,u=d):d>x&&(x=d);if(u-x>=s.theta)break}let a=[...t.keys()];if(n&&(a=a.filter(e=>n.has(this.classes[e])),!a.length)){let e=[];return e.stage=i,e}let m=H(a.map(e=>t[e])),f=[...m.keys()].sort((e,s)=>m[s]-m[e]).slice(0,3).map(e=>({char:this.classes[a[e]],prob:m[e]}));return f.stage=i,f}async embedding(o){o=j(o),await this.ready,this._planeFor=null;let l=this.stages[this.stages.length-1];return l.embed(this.raster(o,l.size))}async personalize(o,{steps:l=250,lr:n=.5,prior:t=1}={}){await this.ready;let i=this.stages[this.stages.length-1],a=i.head0||(i.head0=i.head),m=[],c=[];for(let[x,d]of Object.entries(o||{})){let g=this.classes.indexOf(x);if(!(g<0))for(let y of d||[]){let w=j(y);!w||!w.length||(m.push(await this.embedding(w)),c.push(g))}}if(!m.length)return i.head=a,this.personal=null,null;let f={in:a.in,out:a.out,w:Float32Array.from(a.w),bias:Float32Array.from(a.bias)},e=m.length,s=new Float32Array(f.w.length),u=new Float32Array(f.out);for(let x=0;x<l;x++){s.fill(0),u.fill(0);for(let g=0;g<e;g++){let y=H(O(f,m[g])),w=m[g];for(let v=0;v<f.out;v++){let A=(y[v]-(v===c[g]?1:0))/e;if(!A)continue;u[v]+=A;let I=v*f.in;for(let b=0;b<f.in;b++){let p=w[b];p>0&&(s[I+b]+=A*p)}}}let d=t/e;for(let g=0;g<f.w.length;g++)f.w[g]-=n*(s[g]+d*(f.w[g]-a.w[g]));for(let g=0;g<f.out;g++)f.bias[g]-=n*(u[g]+d*(f.bias[g]-a.bias[g]))}return i.head=f,this.personal={n:e,classes:[...new Set(c)].map(x=>this.classes[x]).sort()},this.personal}destroy(){this.stages=[],this.personal=null,this._plane=null,this._planeFor=null}depersonalize(){let o=this.stages[this.stages.length-1];o.head0&&(o.head=o.head0),this.personal=null}};typeof window<"u"&&typeof __MILLIREC_ESM__>"u"&&(window.MillirecCharCnn=B);var ft={turns:5,retrace:.5,winding:3.5,cover:.6,minLength:40,minSpan:12,deadband:.22},G=2;function at(r){return{x0:r.x0-G,y0:r.y0-G,x1:r.x1+G,y1:r.y1+G,w:r.w+2*G,h:r.h+2*G}}function lt(r){let o=1/0,l=1/0,n=-1/0,t=-1/0;for(let i of r){let a=i.x!==void 0?i.x:i[0],m=i.y!==void 0?i.y:i[1];a<o&&(o=a),a>n&&(n=a),m<l&&(l=m),m>t&&(t=m)}return{x0:o,y0:l,x1:n,y1:t,w:n-o,h:t-l}}function ct(r,o){let l=1/0,n=-1/0;for(let c of r)c<l&&(l=c),c>n&&(n=c);let t=(n-l)*o;if(!(t>0))return 0;let i=0,a=r[0],m=0;for(let c of r)i>0?c>a?a=c:c<a-t&&(m++,i=-1,a=c):i<0?c<a?a=c:c>a+t&&(m++,i=1,a=c):c>a+t?(i=1,a=c):c<a-t&&(i=-1,a=c);return m}function gt(r){let o=0,l=0;for(let u of r)o+=u.x,l+=u.y;o/=r.length,l/=r.length;let n=0,t=0,i=0;for(let u of r){let x=u.x-o,d=u.y-l;n+=x*x,t+=x*d,i+=d*d}let a=n+i,m=n*i-t*t,c=a/2+Math.sqrt(Math.max(0,a*a/4-m)),f=t,e=c-n;Math.hypot(f,e)<1e-9&&(f=1,e=0);let s=Math.hypot(f,e);return{ux:f/s,uy:e/s}}var pt=.09,wt=4;function Mt(r,o){let l=Math.max(2,o*pt),n=l/2,t=[r[0]],i=0;for(let e=1;e<r.length;e++){let s=r[e].x-r[e-1].x,u=r[e].y-r[e-1].y,x=Math.hypot(s,u);if(!(x>0))continue;let d=(n-i)/x;for(;d<=1;)t.push({x:r[e-1].x+s*d,y:r[e-1].y+u*d}),d+=n/x;i=(i+x)%n}if(t.length<8)return 0;let a=Math.round(wt*l/n),m=e=>`${Math.floor(e.x/l)},${Math.floor(e.y/l)}`,c=new Map;t.forEach((e,s)=>{let u=m(e);c.has(u)||c.set(u,[]),c.get(u).push(s)});let f=0;for(let e=0;e<t.length;e++){let s=t[e],u=Math.floor(s.x/l),x=Math.floor(s.y/l),d=!1;for(let g=-1;g<=1&&!d;g++)for(let y=-1;y<=1&&!d;y++)for(let w of c.get(`${u+g},${x+y}`)||[])if(!(Math.abs(e-w)<=a)&&Math.hypot(t[w].x-s.x,t[w].y-s.y)<=l){d=!0;break}d&&f++}return f/t.length}function It(r,o=[],l={}){let n={...ft,...l},t=[];for(let y of r){let w={x:y.x!==void 0?y.x:y[0],y:y.y!==void 0?y.y:y[1]},v=t[t.length-1];(!v||Math.hypot(w.x-v.x,w.y-v.y)>.75)&&t.push(w)}if(t.length<4)return{turns:0,cross:0,retrace:0,winding:0,cover:0,length:0,span:0};let i=0;for(let y=1;y<t.length;y++)i+=Math.hypot(t[y].x-t[y-1].x,t[y].y-t[y-1].y);let a=lt(t),m=Math.hypot(a.w,a.h),{ux:c,uy:f}=gt(t),e=t.map(y=>y.x*c+y.y*f),s=t.map(y=>-y.x*f+y.y*c),u=ct(e,n.deadband),x=ct(s,n.deadband),d=0,g=[];for(let y of o)for(let w of y)g.push(w);if(g.length){let y=at(lt(g)),w=at(a),v=Math.max(0,Math.min(y.x1,w.x1)-Math.max(y.x0,w.x0)),A=Math.max(0,Math.min(y.y1,w.y1)-Math.max(y.y0,w.y0));d=v/Math.min(y.w,w.w)*(A/Math.min(y.h,w.h))}return{turns:u,cross:x,retrace:Mt(t,m),winding:m>0?i/m:0,cover:d,length:i,span:m}}function ut(r,o=[],l={}){let n={...ft,...l};if(!o||!o.length)return!1;let t=It(r,o,n);return t.length>=n.minLength&&t.span>=n.minSpan&&t.turns>=n.turns&&t.retrace>=n.retrace&&t.winding>=n.winding&&t.cover>=n.cover}var bt=12,q=()=>typeof performance<"u"&&performance.now?performance.now():Date.now();function ht({width:r=160,height:o=90,ink:l="#111827",onStrokeEnd:n,colorOf:t=null,idleOverwrite:i=0,scribbleClear:a=!1,scribble:m=null,onClear:c=null,staleAlpha:f=.3,onStale:e=null}){let s=document.createElement("canvas"),u=typeof devicePixelRatio<"u"&&devicePixelRatio||1;s.width=r*u,s.height=o*u,s.style.width=r+"px",s.style.height=o+"px",s.style.touchAction="none";let x=s.getContext("2d");x.scale(u,u);let d=[],g=[],y=null,w=!1,v=1,A=0,I=0,b=!1,p=null,M=!1;function L(h,E){if(!(h.length<2)){x.strokeStyle=E||l,x.beginPath(),x.moveTo(h[0].x,h[0].y);for(let S=1;S<h.length;S++)x.lineTo(h[S].x,h[S].y);x.stroke()}}function _(){x.clearRect(0,0,r,o),x.lineWidth=2.5,x.lineCap="round",x.lineJoin="round",x.globalAlpha=b?f:1;for(let h of d)L(h,t&&t(h));x.globalAlpha=1,L(g,null)}function C(h){b!==h&&(b=h,_(),e&&e(h))}function k(){clearTimeout(I),I=0;let h=(Number(i)||0)*1e3;if(!h||!d.length){C(!1);return}let E=h-(q()-A);if(E<=0){C(!0);return}C(!1),I=setTimeout(()=>C(!0),E)}function X(h){d.length&&(p=d,d=[],clearTimeout(I),I=0,C(!1),_(),c&&c(h))}function D(h,E){let S=d.filter(F=>!F.some(R=>Math.hypot(R.x-h,R.y-E)<=bt));return S.length===d.length?!1:(d=S,_(),!0)}let P=null,Y=h=>{let E=P||(P=s.getBoundingClientRect()),S=r/(s.clientWidth||r),F=o/(s.clientHeight||o);return{x:(h.clientX-E.left-s.clientLeft)*S,y:(h.clientY-E.top-s.clientTop)*F,p:h.pressure||.5}},U=!1,T=null,N=h=>h.preventDefault();s.addEventListener("touchstart",N,{passive:!1}),s.addEventListener("touchmove",N,{passive:!1}),s.addEventListener("contextmenu",N),s.style.userSelect=s.style.webkitUserSelect="none",s.style.webkitTouchCallout="none",s.addEventListener("pointerdown",h=>{if(h.button>0)return;if(y!==null&&y!==h.pointerId){if(T==="pen"&&h.pointerType!=="pen")return;Z({pointerId:y})}h.preventDefault(),y=h.pointerId,T=h.pointerType,P=s.getBoundingClientRect();try{s.setPointerCapture(h.pointerId)}catch{}let E=Y(h);if(w){U=D(E.x,E.y);return}b&&d.length&&(X("idle"),M=!0),g=[E],_()},{passive:!1}),s.addEventListener("pointermove",h=>{if(y!==h.pointerId)return;h.preventDefault();let E=h.getCoalescedEvents?h.getCoalescedEvents():null,S=E&&E.length?E:[h];if(w){for(let F of S){let R=Y(F);D(R.x,R.y)&&(U=!0)}return}for(let F of S)g.push(Y(F));_()},{passive:!1});let Z=(h,E=!1)=>{if(y===h.pointerId){try{s.releasePointerCapture(h.pointerId)}catch{}if(y=null,T=null,P=null,w){U&&(A=q(),k(),n&&n(d)),U=!1;return}if(g.length>0){let S=g;if(S.length===1){if(E){g=[],_(),M&&(M=!1,n&&n(d));return}let R=S[0];S=[{x:R.x-1,y:R.y,p:R.p},{x:R.x+1,y:R.y,p:R.p}]}let F=d.length?d:M?p:null;if(a&&F&&F.length&&ut(S,F,m||{})){g=[],A=q(),M=!1,d.length?X("scribble"):(_(),c&&c("scribble")),n&&n(d);return}S.id=v++,d=[...d,S],g=[],M=!1,A=q(),k(),n&&n(d)}_()}};return s.addEventListener("pointerup",h=>Z(h)),s.addEventListener("pointercancel",h=>Z(h,!0)),{el:s,getStrokes:()=>d,setInk(h){l=h,_()},setErasing(h){w=!!h,s.style.cursor=w?"cell":"crosshair"},isErasing:()=>w,repaint:_,isStale:()=>b,undoClear(){return!p||!p.length?!1:(d=d.length?[...p,...d]:p,p=null,A=q(),k(),_(),n&&n(d),!0)},configure(h={}){"idleOverwrite"in h&&(i=Number(h.idleOverwrite)||0),"scribbleClear"in h&&(a=!!h.scribbleClear),"scribble"in h&&(m=h.scribble),"staleAlpha"in h&&(f=Number(h.staleAlpha)),k()},clear(){d=[],g=[],y=null,T=null,P=null,clearTimeout(I),I=0,C(!1),M=!1,_(),n&&n(d)}}}var K=class extends B{attach(o,{width:l=240,height:n=240,ink:t="#1e293b",idleOverwrite:i=2,scribbleClear:a=!0,scribble:m=null,staleAlpha:c,onStale:f}={}){let e=ht({width:l,height:n,ink:t,idleOverwrite:i,scribbleClear:a,scribble:m,onStale:f,...c===void 0?{}:{staleAlpha:c},onClear:s=>o.dispatchEvent(new CustomEvent("millirec:clear",{detail:{reason:s,undo:()=>e.undoClear()}})),onStrokeEnd:async s=>{if(!s.length){o.dispatchEvent(new CustomEvent("millirec:result",{detail:{results:[],strokes:[]}}));return}let u=await this.recognize(s);o.dispatchEvent(new CustomEvent("millirec:result",{detail:{results:u,strokes:s}}))}});return o.appendChild(e.el),{clear:e.clear,undoClear:e.undoClear,isStale:e.isStale,configure:e.configure,setInk:e.setInk,repaint:e.repaint,getStrokes:e.getStrokes,canvas:e.el}}};typeof window<"u"&&typeof __MILLIREC_ESM__>"u"&&(window.MillirecCharCnn=K);})();
@@ -0,0 +1,17 @@
1
+ // accurate: the most accurate, still fast and small
2
+ // 98.49% top-1, 0.24 ms mean via the kernel (worst case 0.9 ms), 20% escalated, 37 KB gzipped with the pad
3
+ window.MILLIREC_CNN = {
4
+ "classes": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
5
+ "stages": [
6
+ {
7
+ "model": "tiny.bin",
8
+ "head": "tiny.head.bin",
9
+ "theta": 2.032
10
+ },
11
+ {
12
+ "model": "base.q4.bin",
13
+ "head": "base.head.bin"
14
+ }
15
+ ],
16
+ "kernel": "cnn-web.js"
17
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "classes": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
3
+ "stages": [
4
+ {
5
+ "model": "tiny.bin",
6
+ "head": "tiny.head.bin",
7
+ "theta": 2.032
8
+ },
9
+ {
10
+ "model": "base.q4.bin",
11
+ "head": "base.head.bin"
12
+ }
13
+ ],
14
+ "kernel": "cnn-web.js"
15
+ }
@@ -0,0 +1,17 @@
1
+ // fast: most accurate under 0.10 ms per character
2
+ // 97.77% top-1 on EMNIST uppercase (7,800 test images), 0.095 ms mean via the kernel, 5% escalated
3
+ window.MILLIREC_CNN = {
4
+ "classes": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
5
+ "stages": [
6
+ {
7
+ "model": "tiny.bin",
8
+ "head": "tiny.head.bin",
9
+ "theta": 0.89
10
+ },
11
+ {
12
+ "model": "base-p25.bin",
13
+ "head": "base-p25.head.bin"
14
+ }
15
+ ],
16
+ "kernel": "cnn-web.js"
17
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "classes": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
3
+ "stages": [
4
+ {
5
+ "model": "tiny.bin",
6
+ "head": "tiny.head.bin",
7
+ "theta": 0.89
8
+ },
9
+ {
10
+ "model": "base-p25.bin",
11
+ "head": "base-p25.head.bin"
12
+ }
13
+ ],
14
+ "kernel": "cnn-web.js"
15
+ }
@@ -0,0 +1,11 @@
1
+ // small: most accurate under 10 KB gzipped
2
+ // 96.36% top-1, 0.89 ms in plain JS (0.06 ms with the kernel, which adds 16 KB), 8.0 KB gzipped with the pad-less runtime
3
+ window.MILLIREC_CNN = {
4
+ "classes": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
5
+ "stages": [
6
+ {
7
+ "model": "tiny.bin",
8
+ "head": "tiny.head.bin"
9
+ }
10
+ ]
11
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "classes": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
3
+ "stages": [
4
+ {
5
+ "model": "tiny.bin",
6
+ "head": "tiny.head.bin"
7
+ }
8
+ ]
9
+ }
Binary file
Binary file
@@ -0,0 +1,77 @@
1
+ export interface Point { x: number; y: number }
2
+ /** One stroke is a polyline; a drawing is one or more strokes. */
3
+ export type Stroke = Point[]
4
+ export type Strokes = Stroke[]
5
+ /** A delta-encoded sample: first value absolute, then differences. */
6
+ export interface Sample { x: number[]; y: number[] }
7
+ export interface Reading { char: string; prob: number }
8
+
9
+ /** v2 training data (core/format/schema.js). v1 `{ class: Sample[] }` is also accepted. */
10
+ export interface Training {
11
+ version: 2
12
+ mode?: 'stroke' | 'image' | 'both'
13
+ pointsPerShape?: number
14
+ classes: Record<string, { samples: Sample[]; geometry?: unknown }>
15
+ params?: Record<string, number>
16
+ ext?: Record<string, unknown>
17
+ }
18
+
19
+ export interface CreateOptions {
20
+ training: Training | Record<string, Sample[]>
21
+ /** Folder holding worker-stroke.js and millirec.wasm. Default: next to this module. Absolute,
22
+ * root-relative or page-relative; the trailing slash is optional. It decides what code is loaded:
23
+ * never fill it from a URL parameter or user input. */
24
+ assetBase?: string
25
+ params?: Record<string, number>
26
+ workerStroke?: string
27
+ }
28
+
29
+ export interface AttachOptions {
30
+ width?: number
31
+ height?: number
32
+ ink?: string
33
+ /** Seconds of pen-up after which the next stroke starts a new character (0 = off). */
34
+ idleOverwrite?: number
35
+ scribbleClear?: boolean
36
+ /** Overrides for the scribble-out detector's bars (turns, retrace, winding, cover). */
37
+ scribble?: Record<string, number> | null
38
+ /** Opacity of ink once the character has settled (default 0.3). */
39
+ staleAlpha?: number
40
+ onStale?: (stale: boolean) => void
41
+ }
42
+
43
+ export interface Pad {
44
+ clear(): void
45
+ /** Put back ink the pad dropped by itself; false when there was none. */
46
+ undoClear(): boolean
47
+ isStale(): boolean
48
+ getStrokes(): Strokes
49
+ /** Change idleOverwrite, scribbleClear, scribble or staleAlpha on a mounted pad. */
50
+ configure(opts: Pick<AttachOptions, 'idleOverwrite' | 'scribbleClear' | 'scribble' | 'staleAlpha'>): void
51
+ setInk(color: string): void
52
+ repaint(): void
53
+ canvas: HTMLCanvasElement
54
+ }
55
+
56
+ export declare const SETS: readonly ['all', 'capitals', 'digits', 'lowercase', 'punctuation']
57
+ export type SetName = (typeof SETS)[number]
58
+
59
+ /** Fetch a shipped training set. */
60
+ export declare function loadSet(name: SetName, opts?: { assetBase?: string }): Promise<Training>
61
+
62
+ export declare class MillirecCharLite {
63
+ static create(opts: CreateOptions): Promise<MillirecCharLite>
64
+ /** Strokes → one sample. Pass the set's pointsPerShape (shipped sets: 25). */
65
+ static strokesToSample(strokes: Strokes | Stroke, pointsPerShape?: number): Sample | null
66
+ /** A copy of `training` with your own drawings added per class (new classes allowed). */
67
+ static withSamples(training: Training | Record<string, Sample[]>, extra: Record<string, Array<Strokes | Stroke | Sample>>, opts?: { replace?: boolean }): Training
68
+ static createDrawCanvas(opts?: object): unknown
69
+ readonly charList: string[]
70
+ readonly ready: Promise<void>
71
+ recognize(strokes: Strokes | Stroke): Promise<Reading[] | null>
72
+ /** Raw per-template costs: costs[i] holds one cost per training sample of classes[i] (lower is closer). */
73
+ rawScores(strokes: Strokes | Stroke): Promise<{ classes: string[]; costs: number[][] } | null>
74
+ use(plugin: { rerank?: (out: unknown, ctx: unknown) => unknown }): this
75
+ attach(el: HTMLElement, opts?: AttachOptions): Pad
76
+ destroy(): void
77
+ }