Skip to content

Instantly share code, notes, and snippets.

@elliotkim916
Last active October 15, 2017 03:26
Show Gist options
  • Save elliotkim916/4b8266255676fd62a71c173a08cf6454 to your computer and use it in GitHub Desktop.
Save elliotkim916/4b8266255676fd62a71c173a08cf6454 to your computer and use it in GitHub Desktop.
Analyze a most frequent word program
function getTokens(rawString) {
// NB: `.filter(Boolean)` removes any falsy items from an array
return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
}
The getTokens function is a regular expression which is a sequence of characters that forms a search pattern.
The lower case method will return all the words in text in lower case form.
The regular expression is being used to search for all the punctuation marks in text such as [,!.";:-]+ and
is then using the split method to split the array into an array of substrings, returning a new array.
As noted above, the .filter(Boolean) is being used to remove any falsy items from an array.
The sort method is being used to sort all of the words in the text body in alphabetical order.
function mostFrequentWord(text) { There is a function entitled mostFrequentWord with the parameter/argument of text.
const words = getTokens(text); The variable words is set equal to the getTokens function with text within the parameter.
const wordFrequencies = {}; wordFrequencies is an empty object.
for (let i = 0; i <= words.length; i++) { The for loop is being used to loop through words and if words[i]
if (words[i] in wordFrequencies) { (the property name of the value within the object wordFrequencies), is in the
wordFrequencies[words[i]]++; wordFrequencies object it will return true, and words[i]
} (the value within the object wordFrequencies) will increment.
else { Basically the object wordFrequencies will have the words as the key and
wordFrequencies[words[i]]=1; the number of times that word is in text as the value.
}
}
let currentMaxKey = Object.keys(wordFrequencies)[0]; currentMaxKey is the first key in the wordFrequencies object.
let currentMaxCount = wordFrequencies[currentMaxKey]; currentMaxCount is the value of the first key of the
wordFrequencies object.
for (var word in wordFrequencies) { A for in loop is used to access the object wordFrequencies, wordFrequencies[word]
if (wordFrequencies[word] > currentMaxCount) { is referring to the value of the wordFrequencies object, which is a number.
currentMaxKey = word; If that number is greater than currentMaxCount, then currentMaxKey is equal to
currentMaxCount = wordFrequencies[word]; the object wordFrequencies property/key name & currentMaxCount is equal to
} the object wordFrequencies property value.
}
return currentMaxKey; Return currentMaxKey returns the property name, revealing the word that appears
} the most!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment