Guest User

Untitled

a guest
Dec 11th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. the function 'getTokens' is being declared with the parameter 'rawString'
  2.  
  3. the function 'getTokens' is run, it returns the rawString in all lower case letters { .toLowerCase() }, splits the string into a new array with the argument of a regular expression which removes any white space and certain characters { .split(/[ ,!.";:-]+/) }, then filters out any falsey items { .filter(Boolean) }, and sorts it alphabetically { .sort() }
  4. Bascially speaking, we return an array of items of all the words used in the 'rawString' in alphabetical order
  5.  
  6. the function 'mostFrequentWord' is declared with the parameter 'text'
  7. assign 'words' value to the function 'getTokens' (makes sense because the function's goal is to return an array of all the words used in a rawString as individual items) [we use 'let' because we are allowing the value to change - since it is an array]
  8. assign 'wordFrequencies' value to an empty array
  9. this line is starting the loop to go through all the items (words from the body of text) in the array
  10. if statement - if the word (item) is found in the 'wordFrequencies' array is true,
  11. then we add 1 to its value. (++). [we add 1 because it is already found, and we are looking for the most frequent word]
  12. else statement - if the word item is not found in the 'wordFrequencies' array (meaning it is false,
  13. then we set its value to 1. [we set its value to one because it is the first time the loop is seeing the word]
  14.  
  15.  
  16. assign 'currentMaxKey' value to the object.keys(wordFrequencies)[0] - this will give us the key (which is the word) in the first position of the 'wordFrequencies' array. It will give us the first word because we want the word in the 0 positon - we use let because that value (or word) is subject to change as we go through the array
  17. assign 'currentMaxCount' value to the number of the currentMaxKey found in the array of 'wordFrequencies'
  18.  
  19. for loop - as long as 'word' found in the 'wordFrequencies' array is true, then execute block of code
  20. if the 'word' in 'wordFrequencies' is greater than the currentMaxCount - then run block of code =>
  21. set the currentMaxKey to that 'word' (this is allowed because we 'let' currentMaxKey's value to change as we go)
  22. set the currentMaxCount to that 'word' number value (this is allowed because we 'let' currentMaxCount's value to change as we go)
  23.  
  24.  
  25. once the statements turn out to be false after going through all the items (or 'words') in the array, we then return the currentMaxKey - which is the most frequently used word in the body of 'text' that originally came from a 'rawString'
Add Comment
Please, Sign In to add comment