Skip to content

Instantly share code, notes, and snippets.

@jamster10
Created March 5, 2019 20:30
Show Gist options
  • Save jamster10/de5f3d4cc7d9ee6991109dc2c8dbe9d9 to your computer and use it in GitHub Desktop.
Save jamster10/de5f3d4cc7d9ee6991109dc2c8dbe9d9 to your computer and use it in GitHub Desktop.
Code Description.js
function getTokens(rawString) {
// NB: `.filter(Boolean)` removes any falsy items from an array
return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
}
function mostFrequentWord(text) {//this is our entry point. It accepts a raw text parameter
/*the text parameter is scrubbed using the getTokens function.
The getTokens function converts the entered text all to lowercase if it isnt already, using toLowerCase(). Splits the text into an array, splitting on any non-word character such as a comma or full stop, using split() along with regex to match single or multiple instances of said characters. The filter() method is then employed to assist in removing any empty array values that split may have created. Things like an empty string, or undefined array element. Not 100% sure what split would have missed. Maybe test to find out. Finally sort is used to place all the elements in alphabetical order. sort by default doesn't need an argument as it compares it's first term to its second and in this case goes based on the unicode of the letters. */ //Is this too much detail? Or not enough?
let words = getTokens(text); //words is now an alphabetized array of all the words in text.
let wordFrequencies = {}; // initializing an empty object. Based on the name, seemingly to sort the words by count.
/* Here the loop goes through every word in words. For each word, if the object does not have a key matching that word, it gets created with a value of 1. If it does have that key, it's value, previously created and initialized to 1, is increased by one. Therefore over time counting up how many times a word appears in words. */
for (let i = 0; i <= words.length; i++) {
if (words[i] in wordFrequencies) {
wordFrequencies[words[i]]++;
} else {
wordFrequencies[words[i]] = 1;
}
}
/* Here some varaibles are initialized to help us determine which word appears the most.
currentMaxKey keeps track of which key, by using an array created from Object.keys on wordFrequencies.
currentMaxCount is initialized to the first value from wordFrequencies.
*/
let currentMaxKey = Object.keys(wordFrequencies)[0];
let currentMaxCount = wordFrequencies[currentMaxKey];
/* This loop cylces through the object worFrequencies with every key. For each key, the if statement checks if the value at that key is more than the currentMaxCount, if it is, it changes the currentMaxCount to that value, and the currentMaxKey to the key. Otherwise it remains unchanged
*/
for (let word in wordFrequencies) {
if (wordFrequencies[word] > currentMaxCount) {
currentMaxKey = word;
currentMaxCount = wordFrequencies[word];
}
}
//the key (word), that is most freqent is returned
return currentMaxKey;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment