Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. function getTokens(rawString) {
  2. // NB: `.filter(Boolean)` removes any falsy items from an array
  3. return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort(); // This is a return statement signifying to retuern rawstring in lower case and split the string into an array of substrings, and returns the new array. Make a new one that contains only items that a filtering function returns true. Sort the original array and make a new copy.
  4. }
  5.  
  6. function mostFrequentWord(text) {
  7. // A function mostFrequentWord is declared
  8. let words = getTokens(text);
  9. let wordFrequencies = {};
  10. // this is signifying to let wordfrequencies = the value based on the conditional satement.
  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. }
  18. //The code above will log the numbers 1 to 0 to the console. It says beginning at i= 0, log the value of i. After logging i, if i is less than or equal to the value of words.length, increment i by one, and then repeat the process.
  19. let currentMaxKey = Object.keys(wordFrequencies)[0];
  20. //let current MaxKey = the object in the word frequencies.
  21. let currentMaxCount = wordFrequencies[currentMaxKey];
  22.  
  23. for (let word in wordFrequencies) {
  24. if (wordFrequencies[word] > currentMaxCount) {
  25. //If the word in wordFrequencies is greater than the currentMaxCount return the currentMaxKey.
  26. currentMaxKey = word;
  27. currentMaxCount = wordFrequencies[word];
  28. }
  29. }
  30. return currentMaxKey;
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement