daily pastebin goal
26%
SHARE
TWEET

Untitled

a guest Jul 11th, 2018 56 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  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. The above code takes the text and strips it down to lower case words with no punctuation, no chance of the counter to confuse upper and lowercase words, and to remove returns, etc as well as creating the array with the list of words.
  6.  
  7. function mostFrequentWord(text) {
  8.   let words = getTokens(text);
  9. The above code takes the text that has been “scrubbed clean” by getTokens function
  10.   let wordFrequencies = {};
  11.   for (let i = 0; i <= words.length; i++) {
  12.     if (words[i] in wordFrequencies) {
  13.       wordFrequencies[words[i]]++;
  14.     } else {
  15.       wordFrequencies[words[i]] = 1;
  16.     }
  17. The above code counts the frequency of each word. This will put it into an output Key (word) and value (number) which will contain the words and how many times they are used.
  18.   }
  19.   let currentMaxKey = Object.keys(wordFrequencies)[0];
  20.   let currentMaxCount = wordFrequencies[currentMaxKey];
  21.   for (let word in wordFrequencies) {
  22.     if (wordFrequencies[word] > currentMaxCount) {
  23.       currentMaxKey = word;
  24.       currentMaxCount = wordFrequencies[word];
  25.     }
  26. The above code goes through each key and value. When the value is more than the current max value then the new currentMaxKey and currentMaxValue are updated.
  27.   }
  28.   return currentMaxKey;
  29. }
  30. This action will show the word most frequently used.
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