Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getTokens(rawString) {
- // NB: `.filter(Boolean)` removes any falsy items from an array
- return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
- }
- // takes a string, converts all to lower case, removes all characters in (), removes falsy items, sorts the array in alphabetical order.
- function mostFrequentWord(text)
- //this function seems to
- {
- let words = getTokens(text);
- // declares varaible [words], set equal to function getTokens, with he argument (text).
- let wordFrequencies = {};
- //wordFrequencies = some set.
- for (let i = 0; i <= words.length; i++)
- // this program runs through each item, probably in
- {
- if (words[i] in wordFrequencies) {
- wordFrequencies[words[i]]++;
- }
- //if word number i is in the set wordFrequencies, then add the word to the set wordFrequencies
- else
- {
- wordFrequencies[words[i]] = 1;
- }
- }
- let currentMaxKey = Object.keys(wordFrequencies)[0];
- // set currentMaxKey to take the key of the first key in the object, wordFrequencies.
- let currentMaxCount = wordFrequencies[currentMaxKey];
- // set currentMaxCount to wordFreuqnecies[currentMaxKey]
- for (let word in wordFrequencies) {
- if (wordFrequencies[word] > currentMaxCount) {
- currentMaxKey = word;
- currentMaxCount = wordFrequencies[word];
- }
- //cycles through every word in wordFrequencies, evaluating whether that wordFrequencies of [word] > currentMaxCount. If so, set it to word, and have the currentMaxCount move up.
- }
- return currentMaxKey;
- }
Add Comment
Please, Sign In to add comment