Guest User

Untitled

a guest
Feb 18th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. function getTokens(rawString) {
  2. return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
  3. }
  4.  
  5. function mostFrequentWord(text) { // Sets up parent function
  6. let words = getTokens(text); // sets words equal to getTokens function above
  7. let wordFrequencies = {}; /* sets WorkFrequences equal to an empty function so results have somewhere to go later */
  8. for (let i = 0; i <= words.length; i++) { /* Loop setting up i to focus on the number of words */
  9. if (words[i] in wordFrequencies) { /* Comparative block setting up pulling out all unique words from the text (ie the, a, all, Madagascar, whatever) */
  10. wordFrequencies[words[i]]++; // Unfamiliar with this line
  11. } else {
  12. wordFrequencies[words[i]] = 1; // Unfamiliar with this one as well
  13. }
  14. }
  15. let currentMaxKey = Object.keys(wordFrequencies)[0]; /* Turns wordFrequencies object into an array,*/
  16. let currentMaxCount = wordFrequencies[currentMaxKey]; /* whose item is now currentMaxKey, and all now a part of currentMaxCount */
  17.  
  18. for (let word in wordFrequencies) { /* Establishes a new loop, using word/wordFrequencies as its arguments. This I'm unfamiliar with */
  19. if (wordFrequencies[word] > currentMaxCount) {
  20. currentMaxKey = word;
  21. currentMaxCount = wordFrequencies[word]; /* New comparitive stating if a word's frequency is greater than the current highest word frequency, then that word takes its place and the loop moves to the next one. (ie new word is 'the' used 37 times, and current highest word frequency is 'a' 23 times. 'the' would become the new highest word frequency and the loop would now start checking 'all'*/
  22. }
  23. }
  24. return currentMaxKey; // returns the result.
  25. }
Add Comment
Please, Sign In to add comment