Ranking the sentences
Using our word stems from the last section, we will give some scores to our sentences
Our next step will be to go through the sentences we have, and give them a score (the higher the score, the more important the sentence) based on how many frequent words they contain. We will also "weight" the scores based on how early in the text the sentence appeared.
To do this, we will need to do some very similar stuff to what we did to compute the stems. In this case, we are going to duplicate some code, which is often regarded as a bad practice, but here it will help us solve the problem simply. After we have the code working we can go back and refactor it.
We will define a new array to store the sentences and their scores in, much like how we did with the stems object. After this, we will iterate through the sentences again, this time assigning them an ever decreasing base score according to how early the occur in the text.
We can then use the same map
and filter
statements from before to clean up our words again, and finally, use a reduce
function to enumerate the total score for the sentence.
Finally, we will push an object to our new array containing the sentence and its associated score.
Now that we have scored each sentence, we will get the n
highest score sentences and return them as our summary.
Last updated
Was this helpful?