forked from dana-ross/flesch-kincaid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflesch-kincaid-es5-v2.min.js
1 lines (1 loc) · 1.61 KB
/
flesch-kincaid-es5-v2.min.js
1
var syllables=function(e){var r=e.toLowerCase().replace(/'/g,"").replace(/e\b/g,""),n=r.split(/[^aeiouy]+/).filter(Boolean);return null==e||""===e?0:1===r.length?1:[/cial/,/tia/,/cius/,/cious/,/giu/,/ion/,/iou/,/sia$/,/.ely$/,/sed$/].map((function(e){return(r.match(e)||[]).length})).reduce((function(e,r){return e-r}))+[/ia/,/riet/,/dien/,/iu/,/io/,/ii/,/[aeiouym]bl$/,/[aeiou]{3}/,/^mc/,/ism$/,/([^aeiouy])\1l$/,/[^l]lien/,/^coa[dglx]./,/[^gq]ua[^auieo]/,/dnt$/].map((function(e){return(r.match(e)||[]).length})).reduce((function(e,r){return e+r}))+n.length-(n.length>0&&""===n[0]?1:0)+r.split(/\b/).map((function(e){return e.trim()})).filter(Boolean).filter((function(e){return!e.match(/[.,'!?]/g)})).map((function(e){return e.match(/[aeiouy]/)?0:1})).reduce((function(e,r){return e+r}))},words=function(e){return(e.split(/\s+/)||[""]).length},sentences=function(e){return(e.split(". ")||[""]).length},syllablesPerWord=function(e){return syllables(e)/words(e)},wordsPerSentence=function(e){return words(e)/sentences(e)},rate=function(e){return 206.835-1.015*wordsPerSentence(e)-84.6*syllablesPerWord(e)},grade=function(e){return.39*wordsPerSentence(e)+11.8*syllablesPerWord(e)-15.59};function gradeFromScore(e){for(var r of[{min:90,max:150,grade:"5th Grade"},{min:80,max:90,grade:"6th Grade"},{min:70,max:80,grade:"7th Grade"},{min:60,max:70,grade:"8th Grade"},{min:50,max:60,grade:"10th Grade"},{min:30,max:50,grade:"College"},{min:0,max:30,grade:"Graduate"}])if(e>=r.min&&e<=r.max)return r.grade;return"-"}function readabilityScore(e){const r=parseInt(rate(e));return r>0?r:0}function gradeFromText(e){return gradeFromScore(readabilityScore(e))}