Created
March 30, 2018 01:21
-
-
Save liseferguson/ceb38ec5a91138673e1cd44b79257427 to your computer and use it in GitHub Desktop.
mostFrequentWord
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
function getTokens(rawString) { | |
// NB: `.filter(Boolean)` removes any falsy items from an array | |
//Does it turn punctuation marks into falsy items? | |
return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort(); | |
} | |
// This returns all words in the string as lowercase. It also returns a new array of all punctuation marks, and turns them to falsy items (???) Might remove from the algorithm so that only words will be counted | |
//The sort() method will make the words in the string in alphabetical order, which will make it easier for the algorithm to see word duplicates | |
function mostFrequentWord(text) { | |
let words = getTokens(text); | |
let wordFrequencies = {}; | |
for (let i = 0; i <= words.length; i++) { | |
if (words[i] in wordFrequencies) { | |
wordFrequencies[words[i]]++; | |
} else { | |
wordFrequencies[words[i]] = 1; | |
} | |
} | |
// The above code sets the new string of words (getTokens) to the property "words", and creates an empty object called wordFrequencies. Then the for loop iterates through the length of the string. | |
//IF the word in the string is in the wordFrequencies object, the loop will count how many of that word there are?? | |
//ELSE, the loop will assign the number 1 to the word, indicating that it only appears once in the string | |
let currentMaxKey = Object.keys(wordFrequencies)[0]; | |
// Here we are returning an array of the object's properties and setting the increment at 0. The property of the object (the key) is words, the value of the key is how many times that word occurs in the string? | |
let currentMaxCount = wordFrequencies[currentMaxKey]; | |
// | |
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