Hi iv been looking around for a way to develop a simple bag of words type model in javascript and have spent time looking around at some examples, however most require jnode or browserify to be installed from what i have seen. I am trying to simply read text, split it up, and get the most frequently used words in the text, however im having issues using javascript's array object to return the text value, so far i can only return the numbered index:
function bagOfWords(text){
text=text.toLowerCase(); //make everything lower case
var bag = text.split(" "); //remove blanks
//count duplicates
var map = bag.reduce(function(prev, cur) {
prev[cur] = (prev[cur] || 0) + 1;
return prev;
}, {});
var arr = Object.keys( map ).map(function ( key ) { return map[key]; }); //index based on values to find top 10 possible tags
arr=arr.sort(sortNumber); //sort the numbered array
var top10 = new Array(); //the final array storing the top 10 elements
for (i = arr.length; top10.length < 10; i--) {
if(top10.length<10){
top10.push(arr[i]);}
}
}
Is there a simpler way using the reduce method to find, count and search the top 10 words using the reduce method without having to iterate the index's and referencing the original text input (without creating new sorted arrays)?