Created
April 23, 2017 03:33
-
-
Save Schmerb/4149a419f67eefcf0742397270ecb420 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
Overall functionality: | |
mostFrequentWord(string) --> @return: returns the word that appears the most in a given string; | |
First, function calls a getTokens(text) method on some string 'text'. | |
This getTokens method returns an array of the words found in the string passed as an argument transofrming all | |
characters to lower case, using any numbers of white space, commas, exclamation points, periods, quotation marks, | |
semi-colons, colons, or dashes in any frequency as the delimiter to determine where each word begins/ends, removes falsy | |
items and sorts the words alphabetically. | |
--> tl:dr strips just the words consisting of letters A-Za-z9-0 into an array | |
After parsing all words from string into an array called words | |
it declares a wordFrequencies object | |
it then iterates through every member (word) of the words array | |
and | |
if the word is already a key | |
of the wordFrequencies object, it updates its value (count) by 1. | |
if word is not already a key, it adds the word (key) with a value (count) of 1. | |
Then, it sets a variable to keep track of the word with the highest count setting the base value as the first word (key) | |
of the wordFreq object with its corresponding value. | |
It then uses a for loop to iterate through every key in wordFreq obj and references that key | |
as 'word' | |
if that words count is higher than the current maxCount | |
it sets the currentMax as 'word' | |
and sets the current max count as the 'word's count value | |
this repeats until all key/value pairs in wordFreq object have been compared against current max key/value variables | |
Finally returns the word found to have the highest count based on algorithm above | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment