Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 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. //^this function looks like it will filter any words from non word like punctuations and return something in all lowercase
  6. //(below) this function looks like it will find the most frequent words and use a for loop to help narrow down.
  7. function mostFrequentWord(text) {
  8. let words = getTokens(text);
  9. let wordFrequencies = {};
  10. for (let i = 0; i <= words.length; i++) {
  11. if (words[i] in wordFrequencies) {
  12. wordFrequencies[words[i]]++;
  13. } else {
  14. wordFrequencies[words[i]] = 1;
  15. }
  16. }
  17. let currentMaxKey = Object.keys(wordFrequencies)[0];
  18. //^This looks like it setting currentMaxKey with the word frequencies to 0
  19. let currentMaxCount = wordFrequencies[currentMaxKey];
  20. //^This appears to set currentMaxCount equal to wordFrequencies
  21. for (let word in wordFrequencies) {
  22. if (wordFrequencies[word] > currentMaxCount) {
  23. currentMaxKey = word;
  24. currentMaxCount = wordFrequencies[word];
  25. }
  26. }
  27. //returns single value
  28. return currentMaxKey;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement