Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. We declare a function called getTokens that has a single parameter called rawString. This function creates a varable
  2. called words and assignes to this variable an array which is the result of:
  3.  
  4. 1. converting the characters in rawString to lower case characters;
  5. 2. splitting this lower case text into an array of substrings using as a separator punctuation marks (btw there is
  6. a question mark in the regex) or empty space that might occur one or more times;
  7. 3. removing any falsy items from this array;
  8. 4. and finally, sorting the items in the array alphabetically.
  9.  
  10.  
  11. Then we declare a function mostFrequentWord which passes in a parameter called text.
  12. Inside the function we create a variable called words and assight to this variable the result of executing the getTokens function
  13. on the parameter text. In other words, the variable world is an array which items are all words from the text.
  14.  
  15. We create an empty object called wordFrequenses.
  16.  
  17. Using a for loop, we iterate over the items of the world array and if any item is one of the existing properties of the object,
  18. we increment its value by 1. If there is no such property, we create it and provide it with a value of 1.
  19.  
  20. After we've finished running throught all the words from the array and have created our object, which properties are words from
  21. the text, and their values are their occurencies in the text, we take the first property of the object (we just create an array
  22. of all properties with the key method, take the first item of that array), and assight the name of that property to the
  23. variable currentMaxKey. The other variable called currentMaxCount is going to be the value of that property, i.e. the number
  24. of times this property word occures in the text.
  25.  
  26. We iterate over all properties of the object, and if the value of some property is higher that the currentMaxCount,
  27. we re-assign the values to currentMaxKey and currentMaxCount (the new property name and its value respectively).
  28.  
  29. After we've run through all the properties in the object, we return the value of the currentMaxKey, which is the word that
  30. orrurs most frequently in the text.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement