Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. function getTokens(rawString)//function name is defined//
  2. {
  3. // NB: `.filter(Boolean)` removes any falsy items from an array
  4. return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort(); // passes to rawstring; converts the string to lowercase; .split(/,!.";:-]+/) is a regex and splits the string into an array of strings using the specified seperator(split at any of those symbols- if an 'a' was in that sequence it would split the words where there was an 'a').The array is seperated whenever it comes across those punctuation marks. The .filter(Boolean) removes falsy items as written above. And then it is sorted.
  5. }
  6. //function below is telling us we are looking for the most frequent word within this text. Let is declaring the block scope variable of the getToken function let wordFrequencies is declaring an object for our count.(lines 7,8,9)
  7. function mostFrequentWord(text) {
  8. let words = getTokens(text);
  9. let wordFrequencies = {};
  10. for (let i = 0; i <= words.length; i++) //for indicates loop that tests each word individually.
  11.  
  12. {
  13. if (words[i] in wordFrequencies) {
  14. wordFrequencies[words[i]]++;
  15. } //if the word is already in the list add 1 to the count.
  16. else {
  17. wordFrequencies[words[i]] = 1;
  18. }//if there is no matching word in the wordfrequencies assign the value of 1
  19. }
  20. let currentMaxKey = Object.keys(wordFrequencies)[0]; /*gives us the
  21. let currentMaxCount*/
  22.  
  23. for (let word in wordFrequencies)//initializes a loop for counting the word frequencies
  24. {
  25. if (wordFrequencies[word] > currentMaxCount)
  26. {
  27. currentMaxKey = word;
  28. currentMaxCount = wordFrequencies[word];
  29. //this is saying if the word frequency is greater than the current max count, that word is the most frequent.
  30. }
  31. }
  32. return currentMaxKey;// gives us the most frequent word as it will stop the function when it hits the max word key
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement