Guest User

Untitled

a guest
Apr 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. function getTokens(rawString) { //creates a new function named getTokens with an argument input rawString
  2. return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort(); //block statement returns lowercase rawstring
  3. //splits characters (/[ ,!.";:-]+/) filters for true & false statements and proceeds to sort the array in alphabetical order
  4.  
  5. function mostFrequentWord(text) { //creates a new function named mostFrequentWord with text arguement input
  6. let words = getTokens(text); // on a local level defines the words variable as getTokens with the arguement of text
  7. // all the text input through mostFrequentWord undergoes the parsing of the getTokens function
  8. let wordFrequencies = {}; // defines a new variable wordFrequencies as an empty array?
  9. for (let i = 0; i <= words.length; i++) { //runs this sequence as for as long as the for arguement is valid
  10. if (words[i] in wordFrequencies) { //this line breaks the loop under the condition that there are no more characters in the text?
  11. wordFrequencies[words[i]]++; //everytime a word occurs 1 value is added?
  12. } else { // if the result is false then we execute the following statement
  13. wordFrequencies[words[i]] = 1;
  14. }
  15. }
  16. let currentMaxKey = Object.keys(wordFrequencies)[0]; //creates an defines an array for the frequency of words
  17. let currentMaxCount = wordFrequencies[currentMaxKey]; //creates our total for the max count
  18.  
  19. //this next block takes the varibles above and decides whether a new word is larger than the current count, and if so rewrites the new word
  20. //as the new most frequent word
  21.  
  22. for (let word in wordFrequencies) { //??
  23. if (wordFrequencies[word] > currentMaxCount) { // begins a loop to read if the new frequent word is larger than the amount from our current
  24. currentMaxKey = word;
  25. currentMaxCount = wordFrequencies[word];
  26. }
  27. }
  28. return currentMaxKey; //returns the results of the main function in the console
  29. }
Add Comment
Please, Sign In to add comment