Created
July 5, 2016 20:07
-
-
Save sbeleidy/93a5402dcffdaa8a7e48fcce2af4c2d3 to your computer and use it in GitHub Desktop.
Calculate and display Flesch–Kincaid readability scores as a user types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>FK Readability Score</title> | |
<meta charset="UTF-8"> | |
</head> | |
<body> | |
<textarea id="theText" rows="5" cols="100"></textarea> | |
</body> | |
<script> | |
document.getElementById("theText").addEventListener("keyup", function(){ | |
var text = document.getElementById("theText").value; | |
var sylCount = 0; | |
var processedItems = 0; | |
var textArray = text.split(' '); | |
textArray.forEach(function(word) { | |
sylCount += syllable_count(word); | |
processedItems++; | |
if(processedItems==textArray.length){ | |
// console.log("syllables", sylCount); | |
// console.log("words",textArray.length); | |
// console.log("sentences", text.split(".").length-1); | |
// console.log("Reading Ease is"); | |
// console.log(readingEase(sylCount, textArray.length -1, text.split(".").length)); | |
// console.log("Grade level is"); | |
// console.log(gradeLevel(sylCount, textArray.length -1, text.split(".").length)); | |
document.getElementById("re-score").innerText = readingEase(sylCount, textArray.length -1, text.split(".").length); | |
document.getElementById("rg-score").innerText = gradeLevel(sylCount, textArray.length -1, text.split(".").length); | |
} | |
}); | |
}); | |
function syllable_count(word) { | |
if(word.indexOf(",") > -1){word = word.substring(0,-1);} | |
if(!isNaN(word)){return 1;} | |
// Copied from Blake Tarter http://codepen.io/blaketarter/pen/hJICz | |
if(word.length === 0) {return 0; } | |
word = word.toLowerCase(); | |
if(word.length <= 3) { return 1; } | |
word = word.replace(' ', '
'); | |
word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, ''); | |
word = word.replace(/^y/, ''); | |
return word.match(/[aeiouy]{1,2}/g).length; | |
} | |
function readingEase(syllables, words, sentences){ | |
return 206.835 - 1.015*(words/sentences) - 84.6*(syllables/words); | |
} | |
function gradeLevel(syllables, words, sentences){ | |
return 0.39*(words/sentences) + 11.8 * (syllables/words) - 15.59; | |
} | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment