Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- We declare a function called getTokens that has a single parameter called rawString. This function creates a varable
- called words and assignes to this variable an array which is the result of:
- 1. converting the characters in rawString to lower case characters;
- 2. splitting this lower case text into an array of substrings using as a separator punctuation marks (btw there is
- a question mark in the regex) or empty space that might occur one or more times;
- 3. removing any falsy items from this array;
- 4. and finally, sorting the items in the array alphabetically.
- Then we declare a function mostFrequentWord which passes in a parameter called text.
- Inside the function we create a variable called words and assight to this variable the result of executing the getTokens function
- on the parameter text. In other words, the variable world is an array which items are all words from the text.
- We create an empty object called wordFrequenses.
- 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,
- we increment its value by 1. If there is no such property, we create it and provide it with a value of 1.
- After we've finished running throught all the words from the array and have created our object, which properties are words from
- the text, and their values are their occurencies in the text, we take the first property of the object (we just create an array
- of all properties with the key method, take the first item of that array), and assight the name of that property to the
- variable currentMaxKey. The other variable called currentMaxCount is going to be the value of that property, i.e. the number
- of times this property word occures in the text.
- We iterate over all properties of the object, and if the value of some property is higher that the currentMaxCount,
- we re-assign the values to currentMaxKey and currentMaxCount (the new property name and its value respectively).
- After we've run through all the properties in the object, we return the value of the currentMaxKey, which is the word that
- orrurs most frequently in the text.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement