Skip to content

Instantly share code, notes, and snippets.

@johnayoung
Created October 29, 2018 21:01
Show Gist options
  • Save johnayoung/5ada74a8935aa2dfeef28cd7dbe64f94 to your computer and use it in GitHub Desktop.
Save johnayoung/5ada74a8935aa2dfeef28cd7dbe64f94 to your computer and use it in GitHub Desktop.
function getTokens(rawString) {
// This first function does the following things:
// - Takes a raw string
// - Converts the entire string to lowercase
// - Splits all words into an array that do not match a single character specified between the []
// - Removes falsy items
// - Sorts the array of strings in place
return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
}
// The goal of this function is take a body of text and return the word that occurs the most frequently.
function mostFrequentWord(text) {
// Convert body of text into array of lowercase strings
let words = getTokens(text);
// Create new object that will house the words, and number of times present in the text
let wordFrequencies = {};
// Loops through the words array. If a word already exists, it increases the counter in wordFrequencies
// Otherwise it adds the word to wordFrequencies and starts the counter at 1
for (let i = 0; i <= words.length; i++) {
if (words[i] in wordFrequencies) {
wordFrequencies[words[i]]++;
} else {
wordFrequencies[words[i]] = 1;
}
}
// Declares variable to hold the word with the highest word count
let currentMaxKey = Object.keys(wordFrequencies)[0];
// Holds the number for the word with the highest word count
let currentMaxCount = wordFrequencies[currentMaxKey];
// Finally, for all words in wordFrequencies, if a specified word count is greater than the
// current max count, then the max key and max count are both updated. The absolute max key is
// returned at the end.
for (let word in wordFrequencies) {
if (wordFrequencies[word] > currentMaxCount) {
currentMaxKey = word;
currentMaxCount = wordFrequencies[word];
}
}
return currentMaxKey;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment