@stdlib/esm
Version:
ES module distribution for Stdlib, a standard library for JavaScript and Node.js.
3 lines • 5.28 kB
JavaScript
// This file is a part of stdlib. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0
import t from"./../assert/is-nonnegative-integer.js";import i from"./../assert/is-positive-integer.js";import e from"./../assert/is-string-array.js";import r from"./../utils/define-read-only-property.js";import s from"./../assert/contains.js";import n from"./tokenize.js";import a from"./../array/int32.js";import h from"./../array/float64.js";import o from"./../utils/define-nonenumerable-read-only-property.js";import u from"./../assert/is-positive-number.js";import m from"./../assert/is-plain-object.js";import p from"./../assert/has-own-property.js";import f from"./../random/base/randu.js";import g from"./../math/base/special/floor.js";var l=a,w=h,d=o;function v(){var t,i,e,r,s,n,a;for(1===arguments.length?e=arguments[0]:2===arguments.length&&(e=arguments[0],t=arguments[1]),i=e.length,s=1,a=0;a<i;a++)s*=e[a];return r="int32"===t?new l(s):new w(s),(n={}).dtype=t,n.shape=e,n.strides=[e[1],1],n.offset=0,n.ndims=e.length,n.length=r.length,n.nbytes=r.byteLength,n.data=r,d(n,"get",h),d(n,"set",o),n;function h(t,i){var e=this.offset+t*this.strides[0]+i*this.strides[1];return this.data[e]}function o(t,i,e){return(t=this.offset+t*this.strides[0]+i*this.strides[1])>=0&&(this.data[t]=e),this}}var b=v;function y(){var t,i,e,r;for(t=b([this.D,this.K]),e=0;e<this.D;e++)for(r=0;r<this.K;r++)i=(this.nd.get(e,r)+this.alpha)/(this.ndSum[e]+this.K*this.alpha),t.set(e,r,i);return t}var c=u.isPrimitive,j=m,S=p;function T(t,i){return j(i)?S(i,"alpha")&&(t.alpha=i.alpha,!c(t.alpha))?new TypeError("invalid option. `alpha` option must be a positive number. Option: `"+t.alpha+"`."):S(i,"beta")&&(t.beta=i.beta,!c(t.beta))?new TypeError("invalid option. `beta` option must be a positive number. Option: `"+t.beta+"`."):null:new TypeError("invalid argument. Options must be an object. Value: `"+i+"`.")}var K=v;function E(){var t,i,e,r;for(t=K([this.K,this.W]),e=0;e<this.K;e++)for(r=0;r<this.W;r++)i=(this.nw.get(r,e)+this.beta)/(this.nwSum[e]+this.W*this.beta),t.set(e,r,i);return t}var V=f,z=g;function L(){var t,i,e,r,s,n;for(this.z=new Array(this.D),s=0;s<this.D;s++){for(this.z[s]=[],e=this.w[s].length,n=0;n<e;n++)i=z(V()*this.K),this.z[s].push(i);for(this.ndSum[s]=e,n=0;n<e;n++)r=this.w[s][n],t=this.z[s][n],this.nw.set(r,t,this.nw.get(r,t)+1),this.nd.set(s,t,this.nd.get(s,t)+1),this.nwSum[t]=this.nwSum[t]+1}}var D=v;function P(t,i,e){var r,s,n,a,h,o,u,m;for(n=t.shape[0],a=t.shape[1],o=D([n,a]),r=(e-1)/e,s=1/e,u=0;u<n;u++)for(m=0;m<a;m++)h=r*t.get(u,m)+s*i.get(u,m),o.set(u,m,h);return o}var W=i,A=f,F=P;function O(t,i,e){var r,s,n,a,h,o,u,m,p,f,g,l,w,d;if(!W(t))throw new TypeError("invalid argument. First argument must be a positive integer. Value: `"+t+"`.");if(!W(i))throw new TypeError("invalid argument. Second argument must be a positive integer. Value: `"+i+"`.");if(!W(e))throw new TypeError("invalid argument. Third argument must be a positive integer. Value: `"+e+"`.");for(s=this.W*this.beta,r=this.K*this.alpha,g=0;g<t;g++){for(f=0;f<this.D;f++)for(d=0;d<this.ndSum[f];d++){for(o=this.w[f][d],n=this.z[f][d],this.nw.set(o,n,this.nw.get(o,n)-1),this.nd.set(f,n,this.nd.get(f,n)-1),this.ndSum[f]-=1,this.nwSum[n]-=1,h=new Array(this.K),l=0;l<this.K;l++)h[l]=(this.nw.get(o,l)+this.beta)/(this.nwSum[l]+s)*(this.nd.get(f,l)+this.alpha)/(this.ndSum[f]+r);for(l=1;l<this.K;l++)h[l]+=h[l-1];for(w=h[this.K-1]*A(),n=0,p=0;p<this.K;p++)if(h[p]>w){n=p;break}this.nw.set(o,n,this.nw.get(o,n)+1),this.nd.set(f,n,this.nd.get(f,n)+1),this.nwSum[n]+=1,this.ndSum[f]+=1,this.z[f][d]=n}g%e==0&&g>i&&(u=this.getPhis(),a=this.getThetas(),this.phiList.push(u),this.thetaList.push(a),m=this.phiList.length,this.avgPhi=1===m?u:F(this.avgPhi,u,m),m=this.thetaList.length,this.avgTheta=1===m?a:F(this.avgTheta,a,m))}}var k=t,x=i,N=e,q=r,B=s,C=n,G=a,H=v,I=y,J=T,M=E,Q=L,R=O;function U(t,i){var e;for(e=0;e<t.length;e++)if(t[e]===i)return e;return-1}function X(t,i,e){var r,s,n,a,h,o,u,m,p,f,g,l,w,d,v,b,y;if(!N(t))throw new TypeError("invalid argument. First argument must be a string array. Value: `"+t+"`.");if(!x(i))throw new TypeError("invalid argument. Number of topics `K` must be a positive integer. Value: `"+i+"`.");if(o={},arguments.length>2&&(u=J(o,e)))throw u;for(l=t.length,a=o.alpha||50/i,h=o.beta||.1,s=[],y=new Array(l),m=0,w=0;w<l;w++)for(y[w]=[],p=(g=C(t[w])).length,d=0;d<p;d++)r=g[d],-1===(f=U(s,r))?(s.push(r),y[w].push(m),m+=1):y[w].push(f);return b=s.length,q(n={},"K",i),q(n,"D",l),q(n,"W",b),q(n,"alpha",a),q(n,"beta",h),q(n,"init",Q),q(n,"fit",R),q(n,"getPhis",M),q(n,"getThetas",I),q(n,"getTerms",c),n.nwSum=new G(i),n.ndSum=new G(l),n.nw=H([b,i],"int32"),n.nd=H([l,i],"int32"),n.phiList=[],n.thetaList=[],n.w=y,n.init(),n;function c(t,e){var r,n,a,h,o,u;if(!k(t)||t>=i)throw new TypeError("invalid argument. First argument must be a nonnegative integer smaller than the total number of topics. Value: `"+t+"`.");if(e){if(!x(e))throw new TypeError("invalid argument. Second argument must be a positive integer. Value: `"+e+"`.")}else e=10;for(a=new Array(e),r=[],u=0;u<e;u++){for(h=0,v=0;v<this.W;v++)(n=this.avgPhi.get(t,v))>h&&!B(r,v)&&(h=n,o=v);r.push(o),a[u]={word:s[o],prob:h}}return a}}var Y=X;export default Y;
//# sourceMappingURL=lda.js.map