-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldavis.v3.0.1.min.js
1 lines (1 loc) · 18.8 KB
/
ldavis.v3.0.1.min.js
1
"use strict";var LDAvis=function(B,t,L,q){var C,S,z,_,F,G={lambda:1,topic:0,term:""},H={old:1,current:1},L=void 0!==L?L:"#1f77b4",q=void 0!==q?q:"#d62728",P=750,D=30,R=30,N=70,W=30,j=530,Q=530,O=530,V=530,Z=60,J=.25,K=B+"-lambda",U=B.replace("#",""),X=U+"-topic",Y=U+"-lambda",$=U+"-term",tt=X+"-down",et=X+"-up",rt=X+"-clear",nt=U+"-leftpanel",at=U+"-bar-freqs",it=U+"-top",lt=U+"-lambdaInput",ot=U+"-lambdaZero",dt=U+"-sliderdiv",ct=U+"-lamlabel";function st(r,n){return n=void 0===n?1:n,function(t,e){return t[r]<e[r]?+n:t[r]>e[r]?-1*n:0}}function r(i){C=i.mdsDat.x.length,S=Math.min(i.R,30),z=[];for(var t=0;t<C;t++){var e={};for(r in i.mdsDat)e[r]=i.mdsDat[r][t];z.push(e)}_=[];for(t=0;t<i["token.table"].Term.length;t++){e={};for(r in i["token.table"])e[r]=i["token.table"][r][t];_.push(e)}F=[];for(t=0;t<i.tinfo.Term.length;t++){var r,e={};for(r in i.tinfo)e[r]=i.tinfo[r][t];F.push(e)}var n=F.slice(0,S);!function(t,e,r){var n=document.createElement("div");n.setAttribute("id",it),n.setAttribute("style","width: 1210px"),document.getElementById(r).appendChild(n);var a=document.createElement("div");a.setAttribute("style","padding: 5px; background-color: #e8e8e8; display: inline-block; width: 530px; height: 50px; float: left"),n.appendChild(a),(r=document.createElement("label")).setAttribute("for",t),r.setAttribute("style","font-family: sans-serif; font-size: 14px"),r.innerHTML="Selected Topic: <span id='"+t+"-value'></span>",a.appendChild(r),(r=document.createElement("input")).setAttribute("style","width: 50px"),r.type="text",r.min="0",r.max=C,r.value="0",r.step="1",r.id=t,a.appendChild(r),(r=document.createElement("button")).setAttribute("id",tt),r.setAttribute("style","margin-left: 5px"),r.innerHTML="Previous Topic",a.appendChild(r),(r=document.createElement("button")).setAttribute("id",et),r.setAttribute("style","margin-left: 5px"),r.innerHTML="Next Topic",a.appendChild(r),(r=document.createElement("button")).setAttribute("id",rt),r.setAttribute("style","margin-left: 5px"),r.innerHTML="Clear Topic",a.appendChild(r),(r=document.createElement("div")).setAttribute("style","margin-top:-10px;margin-right:5px;width: 530px;font-family: sans-serif; font-size: 12px"),r.innerHTML="<p>"+i.corpusinfo.replace(/\n/,"</br>")+"</p>",a.appendChild(r),(a=document.createElement("div")).setAttribute("id",lt),a.setAttribute("style","padding: 5px; background-color: #e8e8e8; display: inline-block; height: 50px; width: 530px; float: right; margin-right: 30px"),n.appendChild(a),(r=document.createElement("div")).setAttribute("style","padding: 5px; height: 20px; width: 220px; font-family: sans-serif; float: left"),r.setAttribute("id",ot),a.appendChild(r),d3.select("#"+ot).append("text").attr("x",0).attr("y",0).style("font-size","14px").text("Slide to adjust relevance metric:"),d3.select("#"+ot).append("text").attr("x",125).attr("y",-5).style("font-size","10px").style("position","absolute").text("(2)"),(n=document.createElement("div")).setAttribute("id",dt),n.setAttribute("style","padding: 5px; height: 40px; width: 250px; float: right; margin-top: -5px; margin-right: 10px"),a.appendChild(n),(r=document.createElement("input")).setAttribute("style","width: 250px; margin-left: 0px; margin-right: 0px"),r.type="range",r.min=0,r.max=1,r.step=i["lambda.step"],r.value=G.lambda,r.id=e,r.setAttribute("list","ticks"),n.appendChild(r),(r=document.createElement("label")).setAttribute("id",ct),r.setAttribute("for",e),r.setAttribute("style","height: 20px; width: 60px; font-family: sans-serif; font-size: 14px; margin-left: 80px"),r.innerHTML="λ = <span id='"+e+"-value'>"+G.lambda+"</span>",a.appendChild(r),a=d3.select("#"+dt).append("svg").attr("width",250).attr("height",25),r=d3.scaleLinear().domain([0,1]).range([7.5,242.5]).nice(),r=d3.axisBottom(r).tickSize(10).ticks(6),a.append("g").attr("class","slideraxis").attr("margin-top","-10px").call(r)}(X,Y,U),console.log("lambda_select",K),d3.select(K).on("mouseup",function(){console.log("lambda_select mouseup"),H.old=H.current,H.current=document.getElementById(Y).value,G.lambda=+this.value,d3.select(K).property("value",G.lambda),d3.select(K+"-value").text(G.lambda);var t=H.old<G.lambda;0<G.topic&&b(t),M(!0),document.getElementById(Y).value=G.lambda}),d3.select("#"+et).on("click",function(){var t=document.getElementById($+G.term);void 0!==t&&E(t),G.term="";var e=document.getElementById(X).value,t=Math.min(C,+e+1).toFixed(0);document.getElementById(X).value=t,T(document.getElementById(X+e)),v(document.getElementById(X+t)),G.topic=t,M(!0)}),d3.select("#"+tt).on("click",function(){var t=document.getElementById($+G.term);void 0!==t&&E(t),G.term="";var e=document.getElementById(X).value,t=Math.max(0,+e-1).toFixed(0);document.getElementById(X).value=t,T(document.getElementById(X+e)),v(document.getElementById(X+t)),G.topic=t,M(!0)}),d3.select("#"+X).on("keyup",function(){var t=document.getElementById($+G.term);void 0!==t&&E(t),G.term="",T(document.getElementById(X+G.topic));t=document.getElementById(X).value;!isNaN(t)&&0<t&&(t=Math.min(C,Math.max(1,t)),v(document.getElementById(X+t)),G.topic=t,M(!0),document.getElementById(X).value=G.topic)}),d3.select("#"+rt).on("click",function(){k(),M(!0)});var a,l,o=d3.extent(z,function(t){return t.x}),d=o[1]-o[0],c=d3.extent(z,function(t){return t.y}),s=c[1]-c[0];l=s<d?(a=d3.scaleLinear().range([0,j]).domain([o[0]-.05*d,o[1]+.05*d]),d3.scaleLinear().range([Q,0]).domain([c[0]-.5*(d-s)-.05*d,c[1]+.5*(d-s)+.05*d])):(a=d3.scaleLinear().range([0,j]).domain([o[0]-.5*(s-d)-.05*s,o[1]+.5*(s-d)+.05*s]),d3.scaleLinear().range([Q,0]).domain([c[0]-.05*s,c[1]+.05*s]));var u=d3.select(B).append("svg").attr("width",j+O+W+90+R).attr("height",Q+2*D+N+120),o=u.append("g").attr("id",nt).attr("class","points").attr("transform","translate("+W+","+2*D+")");o.append("rect").attr("x",0).attr("y",0).attr("height",Q).attr("width",j).style("fill",L).attr("opacity",0).on("click",function(){k(),M(!0)}),o.append("line").attr("x1",0).attr("x2",j).attr("y1",265).attr("y2",265).attr("stroke","gray").attr("opacity",.3),o.append("text").attr("x",0).attr("y",260).text(i["plot.opts"].xlab).attr("fill","gray"),o.append("line").attr("x1",265).attr("x2",265).attr("y1",0).attr("y2",Q).attr("stroke","gray").attr("opacity",.3),o.append("text").attr("x",270).attr("y",7).text(i["plot.opts"].ylab).attr("fill","gray");var p=Math.sqrt(1404.5/Math.PI),d=Math.sqrt(3511.25/Math.PI),c=Math.sqrt(7022.5/Math.PI),m=10+c,f=m+1.5*c,s=function(t,e){d3.select("#"+nt).append("circle").attr("class","circleGuide"+e).attr("r",t).attr("cx",m).attr("cy",Q+t).style("fill","none").style("stroke-dasharray","2 2").style("stroke","#999"),d3.select("#"+nt).append("line").attr("class","lineGuide"+e).attr("x1",m).attr("x2",f).attr("y1",Q+2*t).attr("y2",Q+2*t).style("stroke","gray").style("opacity",.3)};s(p,"Small"),s(d,"Medium"),s(c,"Large");var y="2%",x="10%";d3.select("#"+nt).append("text").attr("x",10).attr("y",520).attr("class","circleGuideTitle").style("text-anchor","left").style("fontWeight","bold").text("Marginal topic distribution"),d3.select("#"+nt).append("text").attr("x",f+10).attr("y",Q+2*p).attr("class","circleGuideLabelSmall").style("text-anchor","start").text(y),d3.select("#"+nt).append("text").attr("x",f+10).attr("y",Q+2*d).attr("class","circleGuideLabelMedium").style("text-anchor","start").text("5%"),d3.select("#"+nt).append("text").attr("x",f+10).attr("y",Q+2*c).attr("class","circleGuideLabelLarge").style("text-anchor","start").text(x);c=o.selectAll("points").data(z).enter();c.append("text").attr("class","txt").attr("x",function(t){return a(+t.x)}).attr("y",function(t){return l(+t.y)+4}).attr("stroke","black").attr("opacity",1).style("text-anchor","middle").style("font-size","11px").style("fontWeight",100).text(function(t){return t.topics}),c.append("circle").attr("class","dot").style("opacity",.2).style("fill",L).attr("r",function(t){return Math.sqrt(t.Freq/100*j*Q*J/Math.PI)}).attr("cx",function(t){return a(+t.x)}).attr("cy",function(t){return l(+t.y)}).attr("stroke","black").attr("id",function(t){return X+t.topics}).on("mouseover",function(t){var e=X+G.topic;0<G.topic&&e!=this.id&&T(document.getElementById(e)),v(this)}).on("click",function(t){d3.event.stopPropagation();var e=X+G.topic;0<G.topic&&e!=this.id&&T(document.getElementById(e)),document.getElementById(X).value=G.topic=t.topics,M(!0),v(this)}).on("mouseout",function(t){G.topic!=t.topics&&T(this),0<G.topic&&v(document.getElementById(X+G.topic))}),u.append("text").text("Intertopic Distance Map (via multidimensional scaling)").attr("x",265+W).attr("y",30).style("font-size","16px").style("text-anchor","middle");var o=n.filter(function(t){return"Default"==t.Category}),h=d3.scaleBand().domain(o.map(function(t){return t.Term})).rangeRound([0,V]).padding(.15),g=d3.scaleLinear().domain([1,d3.max(o,function(t){return t.Total})]).range([0,O]).nice(),c=(d3.axisLeft(h),u.append("g").attr("transform","translate("+ +(j+W+90)+","+2*D+")").attr("id",at)),n=100,u=15;d3.select("#"+at).append("rect").attr("x",0).attr("y",540).attr("height",u).attr("width",n).style("fill",L).attr("opacity",.4),d3.select("#"+at).append("text").attr("x",n+5).attr("y",540+u/2).style("dominant-baseline","middle").text("Overall term frequency"),d3.select("#"+at).append("rect").attr("x",0).attr("y",540+u+5).attr("height",u).attr("width",n/2).style("fill",q).attr("opacity",.8),d3.select("#"+at).append("text").attr("x",n/2+5).attr("y",540+1.5*u+5).style("dominant-baseline","middle").text("Estimated term frequency within the selected topic"),d3.select("#"+at).append("a").attr("xlink:href","http://vis.stanford.edu/files/2012-Termite-AVI.pdf").attr("target","_blank").append("text").attr("x",0).attr("y",540+3*u+5).style("dominant-baseline","middle").text("1. saliency(term w) = frequency(w) * [sum_t p(t | w) * log(p(t | w)/p(t))] for topics t; see Chuang et. al (2012)"),d3.select("#"+at).append("a").attr("xlink:href","http://nlp.stanford.edu/events/illvi2014/papers/sievert-illvi2014.pdf").attr("target","_blank").append("text").attr("x",0).attr("y",540+4*u+5).style("dominant-baseline","middle").text("2. relevance(term w | topic t) = λ * p(w | t) + (1 - λ) * p(w | t)/p(w); see Sievert & Shirley (2014)");o=c.selectAll(B+" .bar-totals").data(o).enter();o.append("rect").attr("class","bar-totals").attr("x",0).attr("y",function(t){return h(t.Term)}).attr("height",h.bandwidth()).attr("width",function(t){return g(t.Total)}).style("fill",L).attr("opacity",.4),o.append("text").attr("x",-5).attr("class","terms").attr("y",function(t){return h(t.Term)+12}).attr("cursor","pointer").attr("id",function(t){return $+t.Term}).style("text-anchor","end").text(function(t){return t.Term}).on("mouseover",function(){A(this)}).on("mouseout",function(){G.term="",E(this),M(!0)}),c.append("text").attr("x",265).attr("y",-30).attr("class","bubble-tool").style("text-anchor","middle").style("font-size","16px").text("Top-"+S+" Most Salient Terms").append("tspan").attr("baseline-shift","super").attr("font-size","12px").text("(1)");o=d3.axisTop(g).tickSize(0).ticks(6);function b(t){for(var e=F.filter(function(t){return t.Category=="Topic"+G.topic}),r=0;r<e.length;r++)e[r].relevance=G.lambda*e[r].logprob+(1-G.lambda)*e[r].loglift;e.sort(st("relevance"));var n=e.slice(0,S),a=d3.scaleBand().domain(n.map(function(t){return t.Term})).rangeRound([0,V]).padding(.15),i=d3.scaleLinear().domain([1,d3.max(n,function(t){return t.Total})]).range([0,O]).nice(),l=d3.select("#"+at).selectAll(B+" .bar-totals").data(n,function(t){return t.Term}),o=d3.select("#"+at).selectAll(B+" .terms").data(n,function(t){return t.Term}),d=d3.select("#"+at).selectAll(B+" .overlay").data(n,function(t){return t.Term}),c=d3.axisTop(i).tickSize(0).ticks(6),s=d3.selectAll(B+" .xaxis"),u=l.enter().append("rect").attr("class","bar-totals").attr("x",0).attr("y",function(t){return a(t.Term)+V+N+120}).attr("height",a.bandwidth()).style("fill",L).attr("opacity",.4),p=o.enter().append("text").attr("x",-5).attr("class","terms").attr("y",function(t){return a(t.Term)+12+V+N+120}).attr("cursor","pointer").style("text-anchor","end").attr("id",function(t){return $+t.Term}).text(function(t){return t.Term}).on("mouseover",function(){A(this)}).on("mouseout",function(){G.term="",E(this),M(!0)}),n=d.enter().append("rect").attr("class","overlay").attr("x",0).attr("y",function(t){return a(t.Term)+V+N+120}).attr("height",a.bandwidth()).style("fill",q).attr("opacity",.8);t?(u.attr("width",function(t){return i(t.Total)}).transition().duration(P).delay(P).attr("y",function(t){return a(t.Term)}),p.transition().duration(P).delay(P).attr("y",function(t){return a(t.Term)+12}),n.attr("width",function(t){return i(t.Freq)}).transition().duration(P).delay(P).attr("y",function(t){return a(t.Term)}),l.transition().duration(P).attr("width",function(t){return i(t.Total)}).transition().duration(P).attr("y",function(t){return a(t.Term)}),o.transition().duration(P).delay(P).attr("y",function(t){return a(t.Term)+12}),d.transition().duration(P).attr("width",function(t){return i(t.Freq)}).transition().duration(P).attr("y",function(t){return a(t.Term)}),l.exit().transition().duration(P).attr("width",function(t){return i(t.Total)}).transition().duration(P).attr("y",function(t,e){return V+N+6+18*e}).remove(),o.exit().transition().duration(P).delay(P).attr("y",function(t,e){return V+N+18+18*e}).remove(),d.exit().transition().duration(P).attr("width",function(t){return i(t.Freq)}).transition().duration(P).attr("y",function(t,e){return V+N+6+18*e}).remove(),s.transition().duration(P).call(c).transition().duration(P)):(u.attr("width",100).transition().duration(P).attr("y",function(t){return a(t.Term)}).transition().duration(P).attr("width",function(t){return i(t.Total)}),p.transition().duration(P).attr("y",function(t){return a(t.Term)+12}),n.attr("width",50).transition().duration(P).attr("y",function(t){return a(t.Term)}).transition().duration(P).attr("width",function(t){return i(t.Freq)}),l.transition().duration(P).attr("y",function(t){return a(t.Term)}).transition().duration(P).attr("width",function(t){return i(t.Total)}),o.transition().duration(P).attr("y",function(t){return a(t.Term)+12}),d.transition().duration(P).attr("y",function(t){return a(t.Term)}).transition().duration(P).attr("width",function(t){return i(t.Freq)}),l.exit().transition().duration(P).attr("y",function(t,e){return V+N+6+18*e+120}).remove(),o.exit().transition().duration(P).attr("y",function(t,e){return V+N+18+18*e+120}).remove(),d.exit().transition().duration(P).attr("y",function(t,e){return V+N+6+18*e+120}).remove(),s.transition().duration(P).transition().duration(P).call(c))}function v(t){if(null!=t){var e=t.__data__,r=Math.round(10*e.Freq)/10,n=e.topics;t.style.opacity=.6,t.style.fill=q,d3.select(B+" .bubble-tool").remove(),d3.select("#"+at).append("text").attr("x",265).attr("y",-30).attr("class","bubble-tool").style("text-anchor","middle").style("font-size","16px").text("Top-"+S+" Most Relevant Terms for Topic "+n+" ("+r+"% of tokens)");for(var a=F.filter(function(t){return t.Category=="Topic"+n}),i=0;i<a.length;i++)a[i].relevance=H.current*a[i].logprob+(1-H.current)*a[i].loglift;a.sort(st("relevance"));var r=a.slice(0,S),l=d3.scaleBand().domain(r.map(function(t){return t.Term})).rangeRound([0,V]).padding(.1),o=d3.scaleLinear().domain([1,d3.max(r,function(t){return t.Total})]).range([0,O]).nice();d3.selectAll(B+" .overlay").remove(),d3.selectAll(B+" .bar-totals").data(r).attr("x",0).attr("y",function(t){return l(t.Term)}).attr("height",l.bandwidth()).attr("width",function(t){return o(t.Total)}).style("fill",L).attr("opacity",.4),d3.selectAll(B+" .terms").data(r).attr("x",-5).attr("y",function(t){return l(t.Term)+12}).attr("id",function(t){return $+t.Term}).style("text-anchor","end").text(function(t){return t.Term}),d3.select("#"+at).selectAll(B+" .overlay").data(r).enter().append("rect").attr("class","overlay").attr("x",0).attr("y",function(t){return l(t.Term)}).attr("height",l.bandwidth()).attr("width",function(t){return o(t.Freq)}).style("fill",q).attr("opacity",.8);r=d3.axisTop(o).tickSize(0).ticks(6);d3.selectAll(B+" .xaxis").call(r)}}function T(t){if(null==t)return t;t.style.opacity=.2,t.style.fill=L,d3.selectAll(B+" .bubble-tool").text("Top-"+S+" Most Salient Terms").append("tspan").attr("baseline-shift","super").attr("font-size",12).text(1),d3.selectAll(B+" .overlay").remove();var t=F.filter(function(t){return"Default"==t.Category}),e=d3.scaleBand().domain(t.map(function(t){return t.Term})).rangeRound([0,V]).padding(.15),r=d3.scaleLinear().domain([1,d3.max(t,function(t){return t.Total})]).range([0,O]).nice();d3.selectAll(B+" .bar-totals").data(t).attr("x",0).attr("y",function(t){return e(t.Term)}).attr("height",e.bandwidth()).attr("width",function(t){return r(t.Total)}).style("fill",L).attr("opacity",.4),d3.selectAll(B+" .terms").data(t).attr("x",-5).attr("y",function(t){return e(t.Term)+12}).style("text-anchor","end").text(function(t){return t.Term});t=d3.axisTop(r).tickSize(0).ticks(6);d3.selectAll(B+" .xaxis").attr("class","xaxis").call(t)}function A(t){var e=$+G.term;""!=G.term&&e!=t.id&&E(document.getElementById(e)),G.term=t.innerHTML,w(t),M(!0)}function w(t){if(null!=t){t.style.fontWeight="bold";for(var e=t.__data__.Term,r=_.filter(function(t){return t.Term==e}),n=r.length,a=[],i=0;i<C;++i)a[i]=0;for(i=0;i<n;i++)a[r[i].Topic-1]=r[i].Freq;for(var l=[],i=0;i<C;++i)l[i]=0;for(i=0;i<n;i++)l[r[i].Topic-1]=11;d3.scaleSqrt().domain([0,1]).range([0,Z]);d3.selectAll(B+" .dot").data(a).transition().attr("r",function(t){return Math.sqrt(t*j*Q*.25/Math.PI)}),d3.selectAll(B+" .dot").data(z),d3.selectAll(B+" .txt").data(l).transition().style("font-size",function(t){return+t}),d3.select(B+" .circleGuideTitle").text("Conditional topic distribution given term = '"+t.innerHTML+"'")}}function E(t){null!=t&&(t.style.fontWeight="normal",d3.selectAll(B+" .dot").data(z).transition().attr("r",function(t){return Math.sqrt(t.Freq/100*j*Q*J/Math.PI)}),d3.selectAll(B+" .txt").transition().style("font-size","11px"),d3.select(B+" .circleGuideTitle").text("Marginal topic distribution"),d3.select(B+" .circleGuideLabelLarge").text(x),d3.select(B+" .circleGuideLabelSmall").attr("y",Q+2*p).text(y),d3.select(B+" .circleGuideSmall").attr("r",p).attr("cy",Q+p),d3.select(B+" .lineGuideSmall").attr("y1",Q+2*p).attr("y2",Q+2*p))}c.append("g").attr("x","xaxis").call(o);o=location.hash.split("&");function I(){return location.origin+location.pathname+"#topic="+G.topic+"&lambda="+G.lambda+"&term="+G.term}function M(t){t?history.replaceState(G,"Query",I()):history.pushState(G,"Query",I())}function k(){0<G.topic&&T(document.getElementById(X+G.topic)),""!=G.term&&E(document.getElementById($+G.term)),G.term="",M(!(document.getElementById(X).value=G.topic=0))}1<o.length&&(G.topic=o[0].split("=")[1],G.lambda=o[1].split("=")[1],G.term=o[2].split("=")[1],G.topic=Math.round(Math.min(C,Math.max(0,G.topic))),G.lambda=Math.min(1,Math.max(0,G.lambda)),document.getElementById(Y).value=G.lambda,document.getElementById(Y+"-value").innerHTML=G.lambda,isNaN(G.topic)||(document.getElementById(X).value=G.topic,0<G.topic&&v(document.getElementById(X+G.topic)),G.lambda<1&&0<G.topic&&b(!1)),H.current=G.lambda,void 0!==(o=document.getElementById($+G.term))&&w(o))}"string"==typeof t?d3.json(t,function(t,e){r(e)}):r(t)};