Guest User

Untitled

a guest
Jun 23rd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. function getTokens(rawString) {
  2. return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
  3. // Basically, the above function is implementing several methods to, in effect, standardize the information that will be provided into the "mostFrequentWord" function below.
  4. // 1. we are taking the argument 'rawString' and making all words contained lowercase(effectively acheiving that the words "the" and "The" will be treated the same)
  5. // 2. the split method is used to create a new array that removes a certain number of items that coincide with the parameters set in the regexp function. In this case, we are eliminating any punctuations.
  6. // 3. As mentioned above `.filter(Boolean)` removes any falsy items from the newly created array.
  7. // 4. Finally, the .sort method rearranges the newly created array. Because no parameters were given, the sort() method sorts the values as strings in alphabetical and ascending order.
  8. }
  9.  
  10. function mostFrequentWord(text) {
  11. //this is the actual function that will be called upon to sort the 'text' to identify the mostFrequentWord.
  12. let words = getTokens(text);
  13. //here we are creating a new 'words' variable by running the provided 'text' through the 'getTokens' function we created above
  14. let wordFrequencies = {};
  15. //here we are subsequently creating an object(identified by the {}) that consist of keys (ie specific words) and values (ie 0 or 1)
  16. for (let i = 0; i <= words.length; i++) {
  17. //here is a loop statements that allows us to apply the below behaviors over the entire length of the 'words' variable.
  18. if (words[i] in wordFrequencies) {
  19. //here we are stating that if the variable (words[i]) is already in the object(wordFrequencies) then we will cycle by an increment of 1
  20. wordFrequencies[words[i]]++;
  21. }
  22. else {
  23. wordFrequencies[words[i]] = 1;
  24. //here we are stating that if the variable (words[i]) is not in the object then we want to add it and set it equal to 1
  25. }
  26. }
  27. let currentMaxKey = Object.keys(wordFrequencies)[0];
  28. //here we create a variable 'currentMaxKey' that consist of the keys of the wordFrequencies object that begins at the first argument
  29. let currentMaxCount = wordFrequencies[currentMaxKey];
  30. //here we create a variable 'currentMaxCount' that consist of the values of the wordFrequencies object
  31.  
  32. for (let word in wordFrequencies) {
  33. //here we are looking to utilize the variable 'word' to iterate over the object 'wordFrequencies'
  34. if (wordFrequencies[word] > currentMaxCount) {
  35. currentMaxKey = word;
  36. currentMaxCount = wordFrequencies[word];
  37. //if wordFrequencies[word] is greater than the currentMaxCount then we are going to reset the value of currentMaxKey to the new 'key' value, and reassign the currentMaxCount to the new corresponding 'value'
  38. }
  39. }
  40. return currentMaxKey;
  41. //returns the word (ie key) that appears most in the provided text
  42. }
Add Comment
Please, Sign In to add comment