Skip to content

Instantly share code, notes, and snippets.

@ernestlv
Last active August 29, 2015 14:09
Show Gist options
  • Save ernestlv/7f3032be33655181408b to your computer and use it in GitHub Desktop.
Save ernestlv/7f3032be33655181408b to your computer and use it in GitHub Desktop.
Bookmarklet to create a word cloud base on word frequency
(function(){
var words={},
html = "",
writer = function(inx,ele){
html += (inx > 0 ? "<h"+inx+">"+ele+"</h"+inx+">" : "<span>"+ele+"</span>");
};
document.body.textContent.split(" ")
.filter(function(ele){ return !/\d/.test(ele) && ele.length > 3 })
.map(function(ele){
return ele.toLowerCase().replace(/\s*/, "").replace(/[\~\`\!\@\#\$\%\^\&\*\(\)\_\+\-\=\{\}\[\]\:\"\;\'\<\>\,\.\?\/]+/, "");
})
.forEach(function(ele){ if (words[ele]){ words[ele]++; } else { words[ele]=1; } });
Object.keys(words)
.sort()
.forEach(function(ele){
if (words[ele]>20) { writer(1, ele); }
else if (words[ele]>12) { writer(2, ele); }
else if (words[ele]>7) { writer(3, ele); }
else if(words[ele]>4) { writer(4, ele); } else { writer(0, ele); }
});
html = "<html><style> body {font-family:sans-serif} h1,h2,h3,h4,h5,h6 { display:inline; margin:1rem 1rem 0rem 0rem; } span {font-size:0.8rem; margin:1rem 1rem 0rem 0rem;} div {margin:1.5rem; -webkit-column-count: 2; -moz-column-count: 2; column-count:2; -webkit-column-gap: 3rem; -moz-column-gap: 3rem; column-gap: 3rem;}</style><body><div>" + html + "</div></body></html>";
document.write(html);
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment