0

I am trying to do word frequency and I have something working as far as printing out the word frequency. I now want to sort the output in Ascending Order.

var paragraph = "Into the bored patent composes the synonymous cheer. The playing essence mothers an offset. Does the alleged cap fast? Why can't the covered fish urge the word? The cyclist works within a laughing jam. When will my smooth year entitle the public?";

un_puncutated_paragraph = paragraph.replace(/[~`!@#$%^&*(){}\[\];:"'<,.>?\/\\|_+=-]/g,"");
let wordsHolder = un_puncutated_paragraph.split(' ');
let frequencyMap = {};

wordsHolder.forEach((word) => {
  if (!frequencyMap[word]) {
    frequencyMap[word] = 0;
  }
  frequencyMap[word] += 1;
});

Also, could someone explain this logic a bit better, not entirely sure what is going on?

  if (!frequencyMap[word]) {
    frequencyMap[word] = 0;
  }
  frequencyMap[word] += 1;

Thanks!

Josh Pittman
  • 7,024
  • 7
  • 38
  • 66
  • 1
    So.. you wrote this code but don't understand what you wrote? Or you copied this code from somewhere and want to understand it? There is a `sort` method on arrays... – Heretic Monkey Feb 07 '18 at 17:27
  • You seem to be using a [dictionary data structure](https://en.wikibooks.org/wiki/A-level_Computing/AQA/Paper_1/Fundamentals_of_data_structures/Dictionaries). This will help you https://stackoverflow.com/questions/25500316/sort-a-dictionary-by-value-in-javascript – Aman B Feb 07 '18 at 17:35
  • so if I am not wrong then you wanted to show updated paragraph text in ascending order of length right? – Dipak Feb 07 '18 at 17:37
  • I wrote everything but the if block. And I am trying to sort the output of the frequency map in ascending order. So for example Into is counted 6 times there for it should be at the top when frequencyMap is printed. – Dude Ranch Feb 07 '18 at 17:42
  • Please review my answer give me your feedback. – Dipak Feb 07 '18 at 17:45

3 Answers3

0

var paragraph, arrText, getVal, finalString;
var objText = [],
  arrGetText = [];

paragraph = "Into the bored patent composes the synonymous cheer. The playing essence mothers an offset. Does the alleged cap fast? Why can't the covered fish urge the word? The cyclist works within a laughing jam. When will my smooth year entitle the public?";

// Step 1. Split Text with space
arrText = paragraph.split(" ");

// Step 2. Create length and text based object
for (var index = 0; index < arrText.length; index++) {
  objText.push({
    len: arrText[index].length,
    text: arrText[index]
  });
}

// Step 3. Sort object by length
objText.sort(function(a, b) {
  return a.len - b.len;
});

// Step 4. Extract value from sorted object and push into a new array
for (var index = 0; index < arrText.length; index++) {
  getVal = Object.values(objText[index]);
  arrGetText.push(getVal[1]);
}

// Step 5. Finally join array with with space and produce ascending order sorted string.
var finalString = arrGetText.join(" ");
console.log(finalString);
Dipak
  • 2,248
  • 4
  • 22
  • 55
  • Thank you for your response. I think I may have worded this wrong. What I mean is I need to take those words from wordsHolder, count the number of occurrences of each word and then display those in ascending order. Example output: The 6, in 2, for 1. Etc – Dude Ranch Feb 07 '18 at 17:48
  • @DudeRanch I think gave an answer with you actual requirement written in your question. – Dipak Feb 07 '18 at 17:50
0
 if (!frequencyMap[word]) { // If word does not exists as key in array...
    frequencyMap[word] = 0; // ...add key in array
  }
 frequencyMap[word] += 1; //  if existing or just created increment word count
Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
0

To sort your final frequencyMap object by the frequency of each word you can try:

Object.keys(frequencyMap)
    .sort()
    .reduce((a, v) => {
      a[v] = frequencyMap[v];
      return a; }, {});
Josh Pittman
  • 7,024
  • 7
  • 38
  • 66
  • The second chunk of code in your question is creating a tally. To understand what is going on you can read through the `Creating a Tally with the Reduce Method In JavaScript` section in this link https://medium.freecodecamp.org/reduce-f47a7da511a9 (you are using forEach, not reduce, but the explanation of what is happening in this example is the same). If you still don't understand I recommend opening a separate stack question so that you don't conflate the two problems. Separate, focused questions will make it easier for other people with the same problems in the future. – Josh Pittman Feb 07 '18 at 18:00