daily pastebin goal
65%
SHARE
TWEET

Untitled

a guest Dec 13th, 2018 54 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.   // Receives a string that most likely contains multiple words, like a sentence or paragraph.
  5.   // Then changes all letters in the string to lower case, splits them into individual strings that should be comprised of individual words, by marking splits at spaces and punctuation. Then clears out any non-words, such as numbers. Then sorts this array, and returns it.
  6. }
  7.  
  8. function mostFrequentWord(text) {
  9.   //function is meant to be sent a large string
  10.   let words = getTokens(text);
  11.   //calls getTokens, is returned an array of strings which is assigned to 'words' variable
  12.   let wordFrequencies = {};
  13.   //creates a blank slate object called 'wordFrequencies'
  14.   for (let i = 0; i <= words.length; i++) {
  15.     //a for loop that runs a number of times equal to the size of the words array
  16.     if (words[i] in wordFrequencies) {
  17.       wordFrequencies[words[i]]++;
  18.     } else {
  19.       wordFrequencies[words[i]] = 1;
  20.     }
  21.           //checks to see if there is an object with a name in wordFrequencies that matches words[i], if there isn't, it creates one with a value of 1, if there is, it increments that value for that word by 1. It creates a tally of the number of uses of each word in the original string this way.
  22.   }
  23.   let currentMaxKey = Object.keys(wordFrequencies)[0];
  24.   let currentMaxCount = wordFrequencies[currentMaxKey];
  25.   // creates a variable to hold the number of occurrences of the first word in wordFrequencies
  26.  
  27.   for (let word in wordFrequencies) {
  28.     if (wordFrequencies[word] > currentMaxCount) {
  29.       currentMaxKey = word;
  30.       currentMaxCount = wordFrequencies[word];
  31.     }
  32.   }
  33.   //goes through wordFrequencies and checks if each different word is more frequent than the one that is represented by currentMaxCount. If it is less frequent the for loop continues, if it is more frequent, the new value replaces the old one in currentMaxKey and currentMaxCount before continuing
  34.   return currentMaxKey;
  35.   //returns the word with the most frequent value at the end
  36. }
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