function dvWordCloud (elem,data,width,height,options) {
var words, countedWords, wordcloud;
if (typeof(options) === "undefined") {var options = {}}
if (typeof(options.maxWords) === "undefined") {options.maxWords = 1000;}
if (typeof(options.rotate) === "undefined") {options.rotate = 0;}
if (typeof(options.padding) === "undefined") {options.padding = 0;}
if (typeof(options.fontScale) === "undefined") {options.fontScale = 15;}
if (typeof(width) === "undefined" || width == '') {width = 300;}
if (typeof(height) === "undefined" || height == '') {height = 200;}
if (typeof(options.transitionMS) === "undefined" || options.transitionMS == '') {options.transitionMS = 0;}
function splitWords (text) {
let stopwords = new Set("i,me,my,myself,we,us,our,ours,ourselves,you,your,yours,yourself,yourselves,he,him,his,himself,she,her,hers,herself,it,its,itself,they,them,their,theirs,themselves,what,which,who,whom,whose,this,that,these,those,am,is,are,was,were,be,been,being,have,has,had,having,do,does,did,doing,will,would,should,can,could,ought,i'm,you're,he's,she's,it's,we're,they're,i've,you've,we've,they've,i'd,you'd,he'd,she'd,we'd,they'd,i'll,you'll,he'll,she'll,we'll,they'll,isn't,aren't,wasn't,weren't,hasn't,haven't,hadn't,doesn't,don't,didn't,won't,wouldn't,shan't,shouldn't,can't,cannot,couldn't,mustn't,let's,that's,who's,what's,here's,there's,when's,where's,why's,how's,a,an,the,and,but,if,or,because,as,until,while,of,at,by,for,with,about,against,between,into,through,during,before,after,above,below,to,from,up,upon,down,in,out,on,off,over,under,again,further,then,once,here,there,when,where,why,how,all,any,both,each,few,more,most,other,some,such,no,nor,not,only,own,same,so,than,too,very,say,says,said,shall".split(","));
let words = text.split(/[\s.]+/g)
.map(w => w.replace(/^[“‘"\-—()\[\]{}]+/g, ""))
.map(w => w.replace(/[;:.!?()\[\]{},"'’”\-—]+$/g, ""))
.map(w => w.replace(/['’]s$/g, ""))
.map(w => w.substring(0, 30))
.map(w => w.toLowerCase())
.filter(w => w && !stopwords.has(w));
return words;
}
if (typeof(data) == "object") {
countedWords = data;
} else if (typeof(data) == "string") {
words = splitWords(data);
countedWords = d3.rollups(words, group => group.length, w => w)
.sort(([, a],[, b]) => d3.descending(a,b))
.slice(0,options.maxWords)
.map(([text, value]) => ({text, value}));
}
//console.log(countedWords);
const cloud = d3Cloud()
.size([width,height])
.words(countedWords)
.padding(options.padding)
.rotate(options.rotate)
.fontSize(d => Math.sqrt(d.value) * options.fontScale)
.on("end", drawCloud)
.start();
function drawCloud() {
wordcloud = elem.selectAll("text")
.data(countedWords)
.join("text")
.transition().duration(options.transitionMS)
.style("font-size", d => d.size + "px")
.attr("transform", d => "translate(" + [d.x, d.y] + ") rotate(" +d.rotate + ")")
.text(d => d.text);
}
return wordcloud;
}