Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 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)
  4.  
  5. //this line returns the rawString array and greats a boolean
  6. //for the symboles in the text.
  7.  
  8. .sort();
  9. }
  10.  
  11. function mostFrequentWord(text) {
  12. let words = getTokens(text);
  13. let wordFrequencies = {};
  14. for (let i = 0; i <= words.length; i++) {
  15. if (words[i] in wordFrequencies) {
  16. wordFrequencies[words[i]]++;
  17. } else {
  18. wordFrequencies[words[i]] = 1;
  19. }
  20. }
  21.  
  22. //a variable for words was created in line 12 and is given the value of getTokens, in line 14 if the frequent words of (i) exceeds 0 then the words doesn't get a token. unless the wordFrequent is greater than 1 then words get a token.
  23.  
  24. let currentMaxKey = Object.keys(wordFrequencies)[0];
  25. let currentMaxCount = wordFrequencies[currentMaxKey];
  26.  
  27. //the variable currentMaxKey and currentMaxCount is given functions.
  28.  
  29. for (let word in wordFrequencies) {
  30. if (wordFrequencies[word] > currentMaxCount) {
  31. currentMaxKey = word;
  32. currentMaxCount = wordFrequencies[word];
  33. }
  34. }
  35. return currentMaxKey;
  36. }
  37. //if the wordFrequencies is greater than the currentMaxCount then the currentMaxKey gets a token.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement