Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 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. //*** makes string divides words into array at space and punctiation, removes flasy items from array
  5. }
  6.  
  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. //*** makes words
  17. }
  18. let currentMaxKey = Object.keys(wordFrequencies)[0];
  19. let currentMaxCount = wordFrequencies[currentMaxKey];
  20.  
  21. for (let word in wordFrequencies) {
  22. if (wordFrequencies[word] > currentMaxCount) {
  23. currentMaxKey = word;
  24. currentMaxCount = wordFrequencies[word];
  25. }
  26. }
  27. return currentMaxKey;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement