Created
April 7, 2017 18:08
-
-
Save tian-chaiyaporn/d07cde8d828cb345452d6bccc62d3f7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getTokens(rawString) { | |
// NB: `.filter(Boolean)` removes any falsy items from an array | |
return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort(); | |
} | |
function mostFrequentWord(text) { | |
// get words that have been sorted with spaces and non-alphabet characters removed | |
var words = getTokens(text); | |
var wordFrequencies = {}; | |
// add word in words and initialize count starting from 1 to each word not in the wordFrequencies object. If the word already exists in object, then simply add to the count. | |
for (var i = 0; i <= words.length; i++) { | |
if (words[i] in wordFrequencies) { | |
wordFrequencies[words[i]]++; | |
} | |
else { | |
wordFrequencies[words[i]]=1; | |
} | |
} | |
// get the first the word and count from wordFrequencies object | |
var currentMaxKey = Object.keys(wordFrequencies)[0]; | |
// get the count of the selected word | |
var currentMaxCount = wordFrequencies[currentMaxKey]; | |
// loop through all the wordFrequencies objects, and if the word has more count than the current count of the selected word, change the selected word to the word that has more count, and then change the selected count to the count corresponding to the word | |
for (var word in wordFrequencies) { | |
if (wordFrequencies[word] > currentMaxCount) { | |
currentMaxKey = word; | |
currentMaxCount = wordFrequencies[word]; | |
} | |
} | |
// return the word with maximum count | |
return currentMaxKey; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment