Guest User

Untitled

a guest
Dec 11th, 2017
73
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();
  4. }
  5. // takes a string, converts all to lower case, removes all characters in (), removes falsy items, sorts the array in alphabetical order.
  6.  
  7. function mostFrequentWord(text)
  8. //this function seems to
  9. {
  10. let words = getTokens(text);
  11. // declares varaible [words], set equal to function getTokens, with he argument (text).
  12.  
  13. let wordFrequencies = {};
  14.  
  15. //wordFrequencies = some set.
  16.  
  17. for (let i = 0; i <= words.length; i++)
  18.  
  19. // this program runs through each item, probably in
  20. {
  21. if (words[i] in wordFrequencies) {
  22. wordFrequencies[words[i]]++;
  23. }
  24.  
  25. //if word number i is in the set wordFrequencies, then add the word to the set wordFrequencies
  26.  
  27.  
  28. else
  29. {
  30. wordFrequencies[words[i]] = 1;
  31. }
  32.  
  33. }
  34. let currentMaxKey = Object.keys(wordFrequencies)[0];
  35.  
  36. // set currentMaxKey to take the key of the first key in the object, wordFrequencies.
  37.  
  38. let currentMaxCount = wordFrequencies[currentMaxKey];
  39.  
  40. // set currentMaxCount to wordFreuqnecies[currentMaxKey]
  41.  
  42. for (let word in wordFrequencies) {
  43. if (wordFrequencies[word] > currentMaxCount) {
  44. currentMaxKey = word;
  45. currentMaxCount = wordFrequencies[word];
  46. }
  47. //cycles through every word in wordFrequencies, evaluating whether that wordFrequencies of [word] > currentMaxCount. If so, set it to word, and have the currentMaxCount move up.
  48. }
  49. return currentMaxKey;
  50. }
Add Comment
Please, Sign In to add comment