Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. This code begins by establishing a function called getTokens that takes one argument, a variable called rawString. This fuction will return the value of rawString, which is expected to be a "string" or series of characters, set to lower case letters, and with punctuation marks removed so that only words are returned. It also removes any false values that aren't strings and sorts all the results into alphabetical order.
  2.  
  3. Now that we have a function that produces an alphabetized list of words from a piece of text, the next function will identify the most frequent word. mostFrequentWord will take an argument "text" and produce a variable "words," which is the array produced from using the getTokens function on "text." This allows the result of the first function we defined to now be affected by the new function. It then establishes an object, called wordFrequencies, and sets up a loop, by setting a variable i to zero and telling it to increment until it reaches the number of items in words. The i variable will be used to correspond to each item in words and apply certain conditions depending on the attributes of that item. In this case, the loop will check how many times a given word appears in wordFrequencies, and increases its count by 1 each time the word is found again. If the word is only found once, it will be given the value 1.
  4.  
  5. Then we add some new variables. currentMaxKey will equal whatever the first item is in wordFrequencies, after object.keys turns the object's keys into an array. currentMaxCount will equal the value of the frequency number of the currentMaxKey.
  6.  
  7. Then a conditional statement, which also introduces a new variable "word" into wordFrequencies. If the item placement of word is larger than currentMaxCount, then word takes the value of currentMaxKey, and word's placement number becomes the currentMaxCount.
  8.  
  9. After assessing all of the words in the string, the function then returns the word associated with the highest frequency count, which has the ultimate value of currentMaxKey.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement