Guest User

Untitled

a guest
Jul 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. /*The function getTokens takes a string as an argument. It takes that string and
  2. converts all the letters to lower case. The lower case letters are then split into an array using the split method.
  3. The regular expression defined in the split removes those special characters so that only words and letters are included in the array.
  4. The array is then sorted using the sort() method, which by default sorts in alphabetical order.
  5. The result is an array of strings, consisinting only of letters or words, sorted in alphabetical order.
  6.  
  7. The function mostFrequentWord takes an argument: text. It then defines the variable words to equal the function getTokens.
  8. Since getTokens will result in the array mentioned above, the variable words is also that same array.
  9. An empty object is then declared into the variable wordFrequencies.
  10. A for loop is then used to go through each item is the words array.
  11. The counter and loop stop when there are no more items in the array to go through. This part of the code also fills
  12. the wordFrequencies object with key.value pairs.
  13.  
  14. The loop checks to see if any of the items from the words array appear as keys in the wordFrequencies object.
  15. If the word appears it increments the value of that key(being the number of times the word shows up) by 1 .
  16. If the word does not appear when the loop runs, then code makes the value of the key matching the word in wordFrequencies equal 1.
  17.  
  18. Then this function must find the most used word in the array words.
  19. It sets the variable currentMaxKey to the first key in wordFrequencies as the base line.
  20. We also what to know what value the first key item holds.
  21. The function sets the variable currentMaxCount to equal the value of currentMaxKey. AKA the first key/value pair in
  22. the array are our baselines and we put them into these variables.
  23.  
  24. A for/in loop is used to loop through each key in the wordFrequencies object. The variable word (defined by let word)
  25. represents those keys. The if statement says that if the value of a key (accessed by wordFrequencies[word]) is greater
  26. than the currentMaxCount then make that key equal to the currentMaxKey. AND make the value belonging to
  27. our new currentMaxKey the new currentMaxCount.
  28.  
  29. Once the loop is done return the updated currentMaxKey.
  30. This should be the word that appears most frequents in the array words.
  31. AKA the key in wordFrequencies that holds the highest value number.
  32. */
  33.  
  34. function getTokens(rawString) {
  35. // NB: `.filter(Boolean)` removes any falsy items from an array
  36. return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
  37. }
  38.  
  39. function mostFrequentWord(text) {
  40. let words = getTokens(text);
  41. let wordFrequencies = {};
  42. for (let i = 0; i <= words.length; i++) {
  43. if (words[i] in wordFrequencies) {
  44. wordFrequencies[words[i]]++;
  45. } else {
  46. wordFrequencies[words[i]] = 1;
  47. }
  48. }
  49. let currentMaxKey = Object.keys(wordFrequencies)[0];
  50. let currentMaxCount = wordFrequencies[currentMaxKey];
  51.  
  52. for (let word in wordFrequencies) {
  53. if (wordFrequencies[word] > currentMaxCount) {
  54. currentMaxKey = word;
  55. currentMaxCount = wordFrequencies[word];
  56. }
  57. }
  58. return currentMaxKey;
  59. }
Add Comment
Please, Sign In to add comment