Skip to content

Instantly share code, notes, and snippets.

@bobbzorzen
Created October 15, 2014 14:56
Show Gist options
  • Save bobbzorzen/f4a5f7fb5a3fed2dd656 to your computer and use it in GitHub Desktop.
Save bobbzorzen/f4a5f7fb5a3fed2dd656 to your computer and use it in GitHub Desktop.
text analysis
javascript:
var highlightedText = window.getSelection().toString();
highlightedText = highlightedText.replace("?",".");
highlightedText = highlightedText.replace("!",".");
highlightedText = highlightedText.replace(/\.\.*/g,".");
var sentances = highlightedText.split(".");
var sentanceCount = (highlightedText.match(/\.\s/g) != null) ? highlightedText.match(/\.\s/g).length : 0;
sentanceCount += (highlightedText.slice(-1) == ".") ? 1:0;
var wordCount = highlightedText.split(" ").length;
var avgWordsPerSentance = (wordCount/sentanceCount).toFixed(2);
var commasCount = highlightedText.split(",").length;
var avgCommasPerSentance = (commasCount/sentanceCount).toFixed(2);
console.log("\n\n\n\n\n\n");
var analysis = "ANALYZED TEXT!\n";
analysis += "Sentances: "+sentanceCount+"\n";
analysis += "Words: "+wordCount+"\n";
analysis += "Avg words/sentance: "+avgWordsPerSentance+"\n";
analysis += "commasCount: "+commasCount+"\n";
analysis += "Avg commas/sentance: "+avgCommasPerSentance+"\n";
console.log(analysis); alert(analysis);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment