Guest User

Untitled

a guest
Dec 12th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 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. } // this function returns the rawString in all lowercase alabetically, splits the string into multipe strings by spaces, removes the
  5. //characters , ! . " ; : ] - + /
  6.  
  7. function mostFrequentWord(text) {
  8. let words = getTokens(text);
  9. //defines words as individual words from the string by running them through the getTokens function.
  10.  
  11. let wordFrequencies = {};
  12. //stores wordFrequencies as an empty object
  13. for (let i = 0; i <= words.length; i++) {
  14. if (words[i] in wordFrequencies) {
  15. wordFrequencies[words[i]]++;
  16. } else {
  17. wordFrequencies[words[i]] = 1;
  18. }
  19. }
  20. //runs the strings (individual words) through the function building keys and values for the object wordFrequencies.
  21. //the first word is added to the object as {*word*: 1} because it is the first time the word is run through the function
  22. //because it is the first time it is run it is passed through the else statement because it is recieved as false because
  23. //it is the first time the function is seeing the word and it is not yet in wordFrequencies. The second time passing through
  24. //function if the word is already in the object wordFrequencies it will increase the value of that word (key) to 2 so it wil
  25. // look like {*word*: 2}. however if it is a word (key) that is not yet in the object it will create a new key/value pair
  26. //starting with a value of 1. This function will iterate through the entire string and continue to count the number of times
  27. //a key shows up and present that in the value until there are no more words.
  28.  
  29. let currentMaxKey = Object.keys(wordFrequencies)[0];
  30. // currentMaxKey is a variable that gives the key an index of 0
  31. let currentMaxCount = wordFrequencies[currentMaxKey];
  32. //currentMaxCount is a variable that gives the values of the keys
  33.  
  34. for (let word in wordFrequencies) {
  35. if (wordFrequencies[word] > currentMaxCount) {
  36. currentMaxKey = word;
  37. currentMaxCount = wordFrequencies[word];
  38. }
  39. }
  40. // this for loop iterates through the keys in the object wordFrequencies, if the value of that key is greater than the
  41. // current max count then the currentMaxKey is replaced with that key, and currentMaxCount is changed to that value or frequency
  42. return currentMaxKey;
  43. } // after the for loop is done iterating through the object, it will return or print the final key that was updated to
  44. // the currentMaxKey
  45.  
  46. //summary: 1) broke the string into an array of seperate strings that represent each word and removed all non-word characters
  47. // 2) created an object from the array of strings where the word is the key and the value is the number of times the word
  48. // appears in the string
  49. //3) we then created a variable for the keys/words and the values/count
  50. //4) we then iterated through the object and returned the key with the greatest value
Add Comment
Please, Sign In to add comment