Skip to content

Instantly share code, notes, and snippets.

@chirag64
Last active January 19, 2016 13:33
Show Gist options
  • Save chirag64/199bf529a32ff387f21f to your computer and use it in GitHub Desktop.
Save chirag64/199bf529a32ff387f21f to your computer and use it in GitHub Desktop.
A quick and hacky script to fetch key sentences from a blog or news article. Basically fetches the sentences with most words in them.
// Get all sentences
var sentences = document.body.innerText.split(".");
// Remove all sentences with newline characters in them since they're likely tags or sidebar data
for (i=sentences.length - 1; i>-1; i--)
{
if (sentences[i].trim().indexOf("\n") !== -1)
{
sentences.splice(i,1);
}
}
// Sort all sentences by character length
var slength = {};
for (i=0; i<sentences.length;i++)
{
slength[i] = sentences[i].length;
}
var NUM = 5;
var sorted = Object.keys(slength).sort(function(a,b){return slength[b]-slength[a]});
sorted = sorted.slice(0,NUM);
sorted.sort(function(a,b){return parseInt(a)-parseInt(b)});
// Put all sentences with highest count of characters in a string and then print it
var str = "";
for (i=0; i<sorted.length; i++)
{
//console.log(sorted[i]);
str += (sentences[parseInt(sorted[i])].trim() + ".\n\n");
}
console.log(str);
//alert(str);
document.write(str.replace(/\n\n/g,"<br /><br />"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment