Getting the summary

We will first need to sort our ranked sentences by their score. To do this we can use the array sort method and a fairly common numerical comparator.

const sorted = ranked.sort((a, b) => {
  if (a.score < b.score) {
    return 1;
  } else if (a.score > b.score) {
    return -1;
  }
  return 0;
});

This will compare the scores and put the entries in the correct order. Once we have done this, all we need to do is take the n first items, where n is the number of sentences we want, in this case, I will take 5.

const top5 = sorted.slice(0, 5);
console.log({ top5 });

And with that, you should now have a 5 sentence summary of the block of text you provided as input 🎉

Last updated