Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. function getTokens(rawString) {
  2. // NB: `.filter(Boolean)` removes any falsy items from an array
  3. //rawString converts letters to lower case
  4. //split converts string into an array, and regular expression says to look for any of the characters within the brackets and use those to separate the words. the plus sign means that it applies to multiple consecutive spaces, commas, exclamations, etc. Split ensures these items are not included in values, which should exclusively be words.
  5. // sort alphabetizes the now lowercase words, devoid of any false values.
  6.  
  7. return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
  8. }
  9. //you can call mostFrequentWord(text); with a string passed in. The string will be run through the getTokens
  10. //function. The resulting array will be passed into the variable 'words'
  11. //the variable wordFrequencies declares an empty object.
  12. //for loop will iterate over the 'words' array.
  13. //words[0] returns a string value, which can be checked against an object uses an in statement.
  14. //wordFrequencies[words[i]] will be set to 1.
  15. //the first time we see a word in the array, it gets saved as wordFrequencies[words[i]]=1; it saves both the word and the value 1 in the wordFrequencies arra
  16.  
  17. //the else statement adds a string value as the key and the number one as its value to the object wordFrequencies.
  18. function mostFrequentWord(text) {
  19. var words = getTokens(text);
  20. var wordFrequencies = {};
  21. for (var i = 0; i <= words.length; i++) {
  22. //The object begins empty, so the if condition will be false, so we go to the else statement. after the word was stored in the object as a key with a value of one, when the if condition checks if that same string of a later index is stored in the array again, the condition is true this time, so it runs the statement. The statement passes in the value of words[i], which will be the same string as the words[i] with an earlier index that was set to 1 in the else statement. The value associated with this key will now increase by 1.
  23. if (words[i] in wordFrequencies) {
  24. wordFrequencies[words[i]]++;
  25. }
  26. else {
  27. wordFrequencies[words[i]]=1;
  28. }
  29. }
  30. //We use currentMaxKey as a variable to store an array of the keys from the object wordFrequencies. We are setting the current max key to a specific index's value (the first value of the array or the first key) presumably to use as comparison as we iterate over the array.
  31. //currentMaxCount will contain the value of the key currentMaxKey in the object wordFrequencies.
  32. var currentMaxKey = Object.keys(wordFrequencies)[0];
  33. var currentMaxCount = wordFrequencies[currentMaxKey];
  34. //this must be a different usage of in than the earlier one. This will loop through the properties of the object wordFrequencies.
  35. for (var word in wordFrequencies) {
  36. //for loop starts with the first key in the object wordFrequencies.
  37. //the if condition starts off checking if the value of the first key in wordFrequencies is greater than the currentMaxCount. They will both be referencing the first key of the object at this point, so it should not run the statement (values are equal, not greater or than). As soon as the for loop reaches a word in wordFrequencies that has a value greater than that of currentMaxCount, the statement will run.
  38. //The variable currentMaxKey will be updated to the new word and currentMaxCount will be updated to the value of that word (wordFrequencies[word]).
  39. if (wordFrequencies[word] > currentMaxCount) {
  40. currentMaxKey = word;
  41. currentMaxCount = wordFrequencies[word];
  42. }
  43. }
  44. //once the for loop finishes it will return the currentMaxKey.
  45. return currentMaxKey;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement