Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. Starts with a function that called 'getTokens' that looks for a rawstring of text as the paramater. on that text it uses:
  2. (1) .toLowerCase to make sure all the strings are the same format, otherwise "The" and "the" would be counted as different words, even though for our purposes we want to count them as the same.
  3. (2) .split with a regex to separate out any punctuation from words, again to make sure we are only counting words.
  4. (3) .filter(Boolean) with the split to remove any false values (punctaution in this case) from our results
  5. (4) .sort(). Since it has no parameters listed, it will automatically sort them alphabetically
  6.  
  7. Then there is another function 'mostFrequentWord' that accepts text as a parameter.
  8. In the function, it creates a 'words' variable within the function that is assigned to the same value as to what 'getTokens' returns.
  9. A new array is created locally within 'mostFrequentWord' called 'wordFrequencies', named so that we can store word frequencies (good coding practice to name things after what they do or what they store).
  10. There is for loop that goes through the 'words' and checks if a word is already in the array. If it is, it will increase its count in the 'wordFrequencies' array by 1; otherwise, it will add the word to the array with a count of 1.
  11. Two variables are created in the function block called 'currentMaxKey' and 'currentMaxCount'. 'currentMaxKey' is assigned the value of the value at index 0 of the wordFrequencies array and 'currentMaxCount' is assigned to the 'currentMaxKey' word index count.
  12. There is another loop that iterates through the words of 'wordFrequencies' to see if the count of the word is greater than the "currentMaxCount". If it is greater, and only if it is greater, will it replace the 'currentMaxKey' and 'currentMaxCount' with the Word and its count. Otherwise, it will keep iterating through the wordFrequencies array until it has gone through the entire array.
  13. At the end, it returns the value of currentMaxKey, which will be the word with the highest count.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement