Created
August 9, 2017 15:03
-
-
Save jamessergeant/670fbd1cdc35357eb3c4a608f0222e17 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// discussed when used in mostFrequentWord | |
function getTokens(rawString) { | |
// NB: `.filter(Boolean)` removes any falsy items from an array | |
return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort(); | |
} | |
function mostFrequentWord(text) { | |
var words = getTokens(text); // splits text by spaces and punctuation into array of words (strings), removes empty strings and sorts | |
var wordFrequencies = {}; // declare variable and initialise with empty object, this is used as a frequency counter | |
for (var i = 0; i <= words.length; i++) { // loop over words | |
if (words[i] in wordFrequencies) { // if the current word already exists in the wordFrequencies object | |
wordFrequencies[words[i]]++; // increment its count | |
} | |
else { // otherwise | |
wordFrequencies[words[i]]=1; // add the new word to the wordFrequencies counter and set it's value to one | |
} | |
} | |
var currentMaxKey = Object.keys(wordFrequencies)[0]; // initialise the cuurent max key to the first word | |
var currentMaxCount = wordFrequencies[currentMaxKey]; // and the associate count of the first word recorded as the current max | |
for (var word in wordFrequencies) { // for all words counted | |
if (wordFrequencies[word] > currentMaxCount) { // if the frequency count of the current word is greater than the current max | |
currentMaxKey = word; // set the key of the current word as the new current max | |
currentMaxCount = wordFrequencies[word]; // along with its frequency count | |
} | |
} | |
return currentMaxKey; // return the key of the word with the greatest frequency in the text | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment