Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getTokens(rawString) {
- return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
- // Basically, the above function is implementing several methods to, in effect, standardize the information that will be provided into the "mostFrequentWord" function below.
- // 1. we are taking the argument 'rawString' and making all words contained lowercase(effectively acheiving that the words "the" and "The" will be treated the same)
- // 2. the split method is used to create a new array that removes a certain number of items that coincide with the parameters set in the regexp function. In this case, we are eliminating any punctuations.
- // 3. As mentioned above `.filter(Boolean)` removes any falsy items from the newly created array.
- // 4. Finally, the .sort method rearranges the newly created array. Because no parameters were given, the sort() method sorts the values as strings in alphabetical and ascending order.
- }
- function mostFrequentWord(text) {
- //this is the actual function that will be called upon to sort the 'text' to identify the mostFrequentWord.
- let words = getTokens(text);
- //here we are creating a new 'words' variable by running the provided 'text' through the 'getTokens' function we created above
- let wordFrequencies = {};
- //here we are subsequently creating an object(identified by the {}) that consist of keys (ie specific words) and values (ie 0 or 1)
- for (let i = 0; i <= words.length; i++) {
- //here is a loop statements that allows us to apply the below behaviors over the entire length of the 'words' variable.
- if (words[i] in wordFrequencies) {
- //here we are stating that if the variable (words[i]) is already in the object(wordFrequencies) then we will cycle by an increment of 1
- wordFrequencies[words[i]]++;
- }
- else {
- wordFrequencies[words[i]] = 1;
- //here we are stating that if the variable (words[i]) is not in the object then we want to add it and set it equal to 1
- }
- }
- let currentMaxKey = Object.keys(wordFrequencies)[0];
- //here we create a variable 'currentMaxKey' that consist of the keys of the wordFrequencies object that begins at the first argument
- let currentMaxCount = wordFrequencies[currentMaxKey];
- //here we create a variable 'currentMaxCount' that consist of the values of the wordFrequencies object
- for (let word in wordFrequencies) {
- //here we are looking to utilize the variable 'word' to iterate over the object 'wordFrequencies'
- if (wordFrequencies[word] > currentMaxCount) {
- currentMaxKey = word;
- currentMaxCount = wordFrequencies[word];
- //if wordFrequencies[word] is greater than the currentMaxCount then we are going to reset the value of currentMaxKey to the new 'key' value, and reassign the currentMaxCount to the new corresponding 'value'
- }
- }
- return currentMaxKey;
- //returns the word (ie key) that appears most in the provided text
- }
Add Comment
Please, Sign In to add comment