SHARE
TWEET

Untitled

a guest Sep 11th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getTokens(rawString) {
  2.   // NB: `.filter(Boolean)` removes any falsy items from an array
  3.   return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
  4. }
  5.  
  6. function mostFrequentWord(text) {
  7.   let words = getTokens(text);
  8.   let wordFrequencies = {};
  9.   for (let i = 0; i <= words.length; i++) {
  10.     if (words[i] in wordFrequencies) {
  11.       wordFrequencies[words[i]]++;
  12.     } else {
  13.       wordFrequencies[words[i]] = 1;
  14.     }
  15.   }
  16.   let currentMaxKey = Object.keys(wordFrequencies)[0];
  17.   let currentMaxCount = wordFrequencies[currentMaxKey];
  18.  
  19.   for (let word in wordFrequencies) {
  20.     if (wordFrequencies[word] > currentMaxCount) {
  21.       currentMaxKey = word;
  22.       currentMaxCount = wordFrequencies[word];
  23.     }
  24.   }
  25.   return currentMaxKey;
  26. }
  27.  
  28.  
  29.  
  30. The provided function mostFrequentWord takes a single argument, text.
  31. The next line of code declares a variable, words, and it holds the value of the function getTokens which takes a single argument, text.
  32. Another variable declaration, wordFrequencies, and it holds the value of an empty object.
  33. This line creates a for loop with 3 conditions. It declares variable i to be 0, and if i is less than or equal to the length of the array output for the variable words, add 1 to i.
  34. If those conditions are met, it will loop through the following if/else conditional.
  35. If the value of variable, words, at the location i in the array, is in wordFrequencies, found through looping through the object, add 1 to the wordFrequencies.
  36. If the value of variable, words, at location i, is NOT found in, wordFrequencies, set the value of wordFrequencies to 1.
  37.  
  38. Line 16 declares a variable currentMaxKey and it holds the value of an array of the objects in wordFrequencies at location 0 of the array.
  39. The currentMaxCount variable is declared and it holds the value of the variable wordFrequencies taking the argument currentMaxKey.
  40.  
  41. Another for loop where the variable, word, is declared and the wordFrequencies object is looped through looking for that variable.
  42. if statement where the variable wordFrequencies when passed the argument, word, is greater than the currentMaxCount, perform the following block of code.
  43. Set the value of currentMaxKey to word.
  44. Set the value of currentMaxCount to wordFrequencies taking the argument, word.
  45.  
  46. At last we return the currentMaxKey. The purpose of the function mostFrequentWord appears to be to iterate through objects to find the word used the most throughout the string provided in the function getTokens.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top