Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*The function getTokens takes a string as an argument. It takes that string and
- converts all the letters to lower case. The lower case letters are then split into an array using the split method.
- The regular expression defined in the split removes those special characters so that only words and letters are included in the array.
- The array is then sorted using the sort() method, which by default sorts in alphabetical order.
- The result is an array of strings, consisinting only of letters or words, sorted in alphabetical order.
- The function mostFrequentWord takes an argument: text. It then defines the variable words to equal the function getTokens.
- Since getTokens will result in the array mentioned above, the variable words is also that same array.
- An empty object is then declared into the variable wordFrequencies.
- A for loop is then used to go through each item is the words array.
- The counter and loop stop when there are no more items in the array to go through. This part of the code also fills
- the wordFrequencies object with key.value pairs.
- The loop checks to see if any of the items from the words array appear as keys in the wordFrequencies object.
- If the word appears it increments the value of that key(being the number of times the word shows up) by 1 .
- 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.
- Then this function must find the most used word in the array words.
- It sets the variable currentMaxKey to the first key in wordFrequencies as the base line.
- We also what to know what value the first key item holds.
- The function sets the variable currentMaxCount to equal the value of currentMaxKey. AKA the first key/value pair in
- the array are our baselines and we put them into these variables.
- A for/in loop is used to loop through each key in the wordFrequencies object. The variable word (defined by let word)
- represents those keys. The if statement says that if the value of a key (accessed by wordFrequencies[word]) is greater
- than the currentMaxCount then make that key equal to the currentMaxKey. AND make the value belonging to
- our new currentMaxKey the new currentMaxCount.
- Once the loop is done return the updated currentMaxKey.
- This should be the word that appears most frequents in the array words.
- AKA the key in wordFrequencies that holds the highest value number.
- */
- function getTokens(rawString) {
- // NB: `.filter(Boolean)` removes any falsy items from an array
- return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
- }
- function mostFrequentWord(text) {
- let words = getTokens(text);
- let wordFrequencies = {};
- for (let i = 0; i <= words.length; i++) {
- if (words[i] in wordFrequencies) {
- wordFrequencies[words[i]]++;
- } else {
- wordFrequencies[words[i]] = 1;
- }
- }
- let currentMaxKey = Object.keys(wordFrequencies)[0];
- let currentMaxCount = wordFrequencies[currentMaxKey];
- for (let word in wordFrequencies) {
- if (wordFrequencies[word] > currentMaxCount) {
- currentMaxKey = word;
- currentMaxCount = wordFrequencies[word];
- }
- }
- return currentMaxKey;
- }
Add Comment
Please, Sign In to add comment