Using Javascript to find most common words in string?
You should split the string into words, then loop through the words and increment a counter for each one:
var wordCounts = { };
var words = str.split(/\b/);
for(var i = 0; i < words.length; i++)
wordCounts["_" + words[i]] = (wordCounts["_" + words[i]] || 0) + 1;
The "_" +
allows it to process words like constructor
that are already properties of the object.
You may want to write words[i].toLowerCase()
to count case-insensitively.
I started with Gustavo Maloste's suggestion and added filtering for sticky words.
let str = 'Delhi is a crowded city. There are very few rich people who travel by their own vehicles. The majority of the people cannot afford to hire a taxi or a three-wheeler. They have to depend on D.T.C. buses, which are the cheapest mode of conveyance. D.T.C. buses are like blood capillaries of our body spreading all over in Delhi. One day I had to go to railway station to receive my uncle. I had to reach there by 9.30 a.m. knowing the irregularity of D.T.C. bus service; I left my home at 7.30 a.m. and reached the bus stop. There was a long queue. Everybody was waiting for the bus but the buses were passing one after another without stopping. I kept waiting for about an hour. I was feeling very restless and I was afraid that I might not be able to reach the station in time. It was 8.45. Luckily a bus stopped just in front of me. It was overcrowded but somehow I managed to get into the bus. Some passengers were hanging on the footboard, so there was no question of getting a seat. It was very uncomfortable. We were feeling suffocated. All of a sudden, an old man declared that his pocket had been picked. He accused the man standing beside him. The young man took a knife out of his pocket and waved it in the air. No body dared to catch him. I thanked God when the bus stopped at the railway station. I reached there just in time.';
//console.log(findMostRepeatedWord(str)); // Result: "do"
let occur = nthMostCommon(str, 10);
console.log(occur);
function nthMostCommon(str, amount) {
const stickyWords =[
"the",
"there",
"by",
"at",
"and",
"so",
"if",
"than",
"but",
"about",
"in",
"on",
"the",
"was",
"for",
"that",
"said",
"a",
"or",
"of",
"to",
"there",
"will",
"be",
"what",
"get",
"go",
"think",
"just",
"every",
"are",
"it",
"were",
"had",
"i",
"very",
];
str= str.toLowerCase();
var splitUp = str.split(/\s/);
const wordsArray = splitUp.filter(function(x){
return !stickyWords.includes(x) ;
});
var wordOccurrences = {}
for (var i = 0; i < wordsArray.length; i++) {
wordOccurrences['_'+wordsArray[i]] = ( wordOccurrences['_'+wordsArray[i]] || 0 ) + 1;
}
var result = Object.keys(wordOccurrences).reduce(function(acc, currentKey) {
/* you may want to include a binary search here */
for (var i = 0; i < amount; i++) {
if (!acc[i]) {
acc[i] = { word: currentKey.slice(1, currentKey.length), occurences: wordOccurrences[currentKey] };
break;
} else if (acc[i].occurences < wordOccurrences[currentKey]) {
acc.splice(i, 0, { word: currentKey.slice(1, currentKey.length), occurences: wordOccurrences[currentKey] });
if (acc.length > amount)
acc.pop();
break;
}
}
return acc;
}, []);
return result;
}