Skip to content

Instantly share code, notes, and snippets.

@mikedolan03
Created January 17, 2018 02:22
Show Gist options
  • Save mikedolan03/6870c921c45c580194f6f6b3a1f8f6fa to your computer and use it in GitHub Desktop.
Save mikedolan03/6870c921c45c580194f6f6b3a1f8f6fa to your computer and use it in GitHub Desktop.
The function GetTokens takes the argument -rawstring - makes it all lowercase and then splits it any time there is a space, punctuation, quote or dash. Then it removes any falsy items like empty stings or null values. Finally it sorts the array alphabetically and returns the array of strings.
The function mostFrequentWord creates a new array of words and sets it to the result of getTokens, while sending a raw string called text as an argument. The getTokens function takes the string and returns a sorted formatted array of words. Next we create an object to track wordFrequencies. Then iterate through the the words array, adding words to the wordfrequencies object - tracking how often they appear. If the word was already in the wordFrequencies object then we just increment its frequency value, otherwise we add the word to the object with a frequency of 1.
Now that we have the wordFrequency data object, we need to compare the values and determine the most frequent word. We create some variables to track which word appears the most. For each word in the wordFrequencies object we check to see if its frequency is greater than the current max. If it is, we set that word to the current max and its frequency to the max count. We then keep going through the list. Once we are done comparing we return the word with the highest frequency.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment