Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. Bloc 4.12 Most frequent word analyzer challenge:
  2.  
  3. First the text to be analyzed is run through the getTokens function, where it is converted to lower-case by the .toLowerCase method, parsed into individual words (apparently excluding punctuation marks?) by the .split method and (/[,!.”;:-]+/) regular expression, cleansed of falsy items like empty strings by the .filter(Boolean) method, and sorted alphabetically by the .sort method. The end result is an array of lower-cased, individual, ‘Boolean-logically’ true, alphabetically-ordered substrings (i.e. words) derived from the original text.
  4.  
  5. This array is then passed to the mostFrequentWord function where it is renamed ‘words’ (let words = getTokens(text);), and an empty object is created (let wordFrequencies = {};) to contain the output of a for-loop that iterates the renamed ‘words’ array through an if /else-conditional statement.
  6.  
  7. for (let i = 0; i <= words.length; i++) {
  8. if (words[i] in wordFrequencies) {
  9. wordFrequencies [words[i]]++;
  10. } else {
  11. wordFrequencies [words[i]] = 1;
  12. }
  13. }
  14.  
  15. The purpose of this for-loop /conditional-statement mechanism is to create key/value pairs from the items in the ‘words’ array, with each word derived from the original text as the ‘key,’ and the number of times that word appears in the array as the value. As these key/value pairs are created they are, of course, inserted into the wordFrequencies object which would appear something like:
  16.  
  17. e.g. wordFrequencies = {wordA: 2, wordB: 5, wordC: 3}
  18.  
  19. Next, two variables are declared.
  20.  
  21. let currentMaxKey = Object.keys(wordFrequencies)[0];
  22. let currentMaxCount = wordFrequencies[currentMaxKey];
  23.  
  24. The first variable, ‘currentMaxKey,’ represents the key of the first key/value pair of the wordFrequencies object, arrived at by passing that object to the Object.keys method and requesting the [0]-index-place key. The second variable, ‘currentMaxCount,’ represents the value that corresponds to the key represented by the currentMaxKey variable. Hence we have the current maximum key (i.e. word), and the current max count (i.e. number) that together would represent the first key/value pair from the wordFrequencies object (e.g. ‘wordA: 2’ from the example above).
  25.  
  26. Next, an iterating for-loop /if-conditional statement mechanism is introduced to return the most frequently appearing word in the original text, as shown here:
  27.  
  28. for (let word in wordFrequencies) {
  29. if (wordFrequencies[word] > currentMaxCount) {
  30. currentMaxKey = word;
  31. currentMaxCount = wordFrequencies[word];
  32. }
  33. }
  34. return currentMax Key;
  35. }
  36.  
  37. To do this, each value of the key/value pairs in the wordFrequencies object is compared against the currentMaxCount value. If the newly-compared value is found to be ‘greater than’ (>) the currentMaxCount value, that value’s corresponding key is set to the currentMaxKey and the value itself is set to the currentMaxCount. This process is repeated until all the key/value pairs in the wordFrequencies object have been evaluated and, finally, the highest currentMaxKey key (i.e. the most frequently appearing word to be found in the original text) is returned by the mostFrequentWord function.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement