Created
June 15, 2017 22:59
-
-
Save anonymous/ecf9d9866afe64c7eb68dfbef6a6b810 to your computer and use it in GitHub Desktop.
mostFrequentJS created by dtstanley - https://repl.it/FfGw/116
This file contains 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 | |
return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort(); | |
} | |
//This function counts the number of times each word appears in the text (colletion) | |
function mostFrequentWord(text) { | |
//the getTokens function is invoked and the results are saved as the variable words | |
var words = getTokens(text); | |
//the wordFrequencies array is created | |
var wordFrequencies = {}; | |
//each word in the wordFrequencies array is identified and counted | |
for (var i = 0; i <= words.length; i++) { | |
if (words[i] in wordFrequencies) { | |
wordFrequencies[words[i]]++; | |
} | |
else { | |
//if a word is only found once, it is identified as such | |
wordFrequencies[words[i]]=1; | |
} | |
} | |
//the currentMaxKey variable is defined and initiated with the first key in the array | |
var currentMaxKey = Object.keys(wordFrequencies)[0]; | |
//the currentMaxCount variable is defined and initiated with the first value, of the first key, in the array | |
var currentMaxCount = wordFrequencies[currentMaxKey]; | |
//the loop identifies which word has the highest count and what the count is | |
for (var word in wordFrequencies) { | |
//the currentMaxCount is compared with the count for each word in the array | |
//if the count for the word is greater than the currentMaxCount, | |
// then the word becomes the new currentMaxKey | |
if (wordFrequencies[word] > currentMaxCount) { | |
currentMaxKey = word; | |
//and the word's count replaces the currentMaxCount | |
currentMaxCount = wordFrequencies[word]; | |
} | |
} | |
//the word with the highest number count is returned to the invoking call | |
return currentMaxKey; | |
} | |
var spaceOddityText = 'Ground Control to Major Tom\n \ | |
Ground Control to Major Tom\n \ | |
Take your protein pills and put your helmet on\n \ | |
Ground Control to Major Tom (ten, nine, eight, seven, six)\n \ | |
Commencing countdown, engines on (five, four, three)\n \ | |
Check ignition and may God\'s love be with you (two, one, liftoff)\n \ | |
\n \ | |
This is Ground Control to Major Tom\n \ | |
You\'ve really made the grade\n \ | |
And the papers want to know whose shirts you wear\n \ | |
Now it\'s time to leave the capsule if you dare\n \ | |
"This is Major Tom to Ground Control\n \ | |
I\'m stepping through the door\n \ | |
And I\'m floating in a most peculiar way\n \ | |
And the stars look very different today\n \ | |
For here\n \ | |
Am I sitting in a tin can\n \ | |
Far above the world\n \ | |
Planet Earth is blue\n \ | |
And there\'s nothing I can do\n \ | |
\n \ | |
Though I\'m past one hundred thousand miles\n \ | |
I\'m feeling very still\n \ | |
And I think my spaceship knows which way to go\n \ | |
Tell my wife I love her very much she knows\n \ | |
Ground Control to Major Tom\n \ | |
Your circuit\'s dead, there\'s something wrong\n \ | |
Can you hear me, Major Tom?\n \ | |
Can you hear me, Major Tom?\n \ | |
Can you hear me, Major Tom?\n \ | |
Can you \"Here am I floating \'round my tin can\n \ | |
Far above the moon\n \ | |
Planet Earth is blue\n \ | |
And there\'s nothing I can do\"'; | |
var rawStringLowerCased = 'ground control to major tim \n \ | |
ground control to major tim\n \ | |
take your protein pills and put your helmet on\n \ | |
ground control to major tim (ten, nine, eight, seven, six)\n \ | |
commencing countdown, engines on (five, four, three)\n \ | |
check ignition and may god\'s love be with you (two, one, liftoff)\n \ | |
\n \ | |
this is ground control to major tom\n \ | |
you\'ve really made the grade\n \ | |
and the papers want to know whose shirts you wear\n \ | |
now it\'s time to leave the capsule if you dare\n \ | |
"this is major tom to ground control\n \ | |
i\'m stepping through the door\n \ | |
and i\'m floating in a most peculiar way\n \ | |
and the stars look very different today\n \ | |
for here\n \ | |
am i sitting in a tin can\n \ | |
far above the world\n \ | |
planet earth is blue\n \ | |
and there\'s nothing i can do\n \ | |
\n \ | |
though i\'m past one hundred thousand miles\n \ | |
i\'m feeling very still\n \ | |
and i think my spaceship knows which way to go\n \ | |
tell my wife i love her very much she knows\n \ | |
ground control to captain tom\n \ | |
your circuit\'s dead, there\'s something wrong\n \ | |
can you hear me, captain tom?\n \ | |
can you hear me, captain tom?\n \ | |
can you hear me, captain tom?\n \ | |
can you \"here am i floating \'round my tin can\n \ | |
far above the moon\n \ | |
planet earth is blue\n \ | |
and there\'s nothing i can do\" '; | |
mostFrequentWord(spaceOddityText) + '\' and also \'' + mostFrequentWord(rawStringLowerCased); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment