How can I get and display the count of each word in the string?
I am able to sort the words from most frequent, but I cannot display the count. The object: frequencies originally displayed the count (key).
I understand that Object.keys and the map() method may help me, but I am unsure how to include it the map method in my code.
Any help is much appreciated...
var stringtest = "Example: This is a string. I've excluded stopwords from becoming most frequent words. This is a string of words.";
function getFreq(string, cutOff){
var refineStr = string.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g,"").toLowerCase().replace(/ to\b| is\b| for\b| you\b| the\b|your\b|an\b| on\b| by\b|this\b|will\b| of\b| a\b/g, ""),
words = refineStr.split(" "),
frequencies = {},
word, frequency, i;
for(i = 0; i < words.length; i++){
word = words[i];
frequencies[" " + words[i]] = (frequencies[" " + words[i]] || 0) + 1;
//frequencies[word]++;
}
words = Object.keys(frequencies);
return words.sort(function (a,b)
{
return frequencies[b] - frequencies[a];}).slice(0, cutOff).toString();
}
document.getElementById("wordlist").innerHTML = getFreq(stringtest, 20);