Skip to content

Instantly share code, notes, and snippets.

@malavbhavsar
Forked from lsauer/gist:1221325
Last active January 3, 2016 13:29
Show Gist options
  • Save malavbhavsar/8470034 to your computer and use it in GitHub Desktop.
Save malavbhavsar/8470034 to your computer and use it in GitHub Desktop.
//l.sauer 2011, public domain
//returns a hash table with the word as index and frequency as value; good for svg / canvas -plotting or other experiments
//[:punct:] Punctuation symbols . , " ' ? ! ; : # $ % & ( ) * + - / < > = @ [ ] \ ^ _ { } | ~
var wordcnt = function(id){
var hist = {}, words = document.getElementById(id).innerText.split(/[\s*\.*\,\;\+?\#\|:\-\/\\\[\]\(\)\{\}$%&0-9*]/)
for(var i in words)
if(words[i].length >1 )
hist[words[i]] ? hist[words[i]]+=1 : hist[words[i]]=1;
return hist;
};
wordcnt('res') //id of the Element, e.g. res is the div containing the results of a google search
----
//Solution in one continuous line of code:
text.split(/[\s*\.*\,\;\+?\#\|:\-\/\\\[\]\(\)\{\}$%&0-9*]/).map( function(k,v){ words||(words={});words[k]++||(words[k]=1); } )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment