I'm wanting to write my first Jekyll plug-in. I've recently come across the flesch-kincaid algorithm for determining general reading level. What I'm thinking is to write something that adds a data attribute to each <p>
tag or any element marked with [data-read-level]
.
Flesch Reading Ease Score = 206.835 − 1.015 × ( Total Words / Total Sentences ) − 84.6 × ( Total Syllables / Total Words )
What category does this fall under though? I'm thinking generator myself. Is there maybe some better way to show the results rather than an attribute?
I've written a somewhat working JS based version here, it needs work probably and only works with English right now.
JS Version:
// Flesch Reading Ease Score = 206.835 − 1.015 × ( Total Words / Total Sentences ) − 84.6 × ( Total Syllables / Total Words )
let fkExamine = function() {
window.addEventListener('DOMContentLoaded', init);
const fkConstA = 206.835;
const fkConstB = 1.015;
const fkConstC = 84.6;
function init() {
console.log('-- fkExamine.js initiated');
let parseArray = [].slice.call(document.querySelectorAll('p, [data-read-level]'));
parseArray.map(function(parseItem) {
let txtContent = parseItem.textContent;
let totalWords = getTotalWords(txtContent);
let totalSentences = getTotalSentences(txtContent);
let totalSyllables = getTotalSyllables(txtContent);
let fkScore = fkConstA - fkConstB * (totalWords / totalSentences) - fkConstC * (totalSyllables / totalWords);
parseItem.setAttribute('fk-score', fkScore);
});
}
function getTotalWords(input) {
const words = input.split(' ');
return words.filter(word => word !== '').length;
}
function getTotalSentences(input) {
return input.match(/[\w|\)][.?!](\s|$)/g).length;
}
function getTotalSyllables(input) {
let syllables = 0;
let words = input.split(' ');
words = words.filter(word => word !== '');
for(let i = 0; i < words.length; i++) {
words[i].toLowerCase();
words[i].replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '');
words[i].replace(/^y/, '');
syllables += words[i].match(/[aeiouy]{1,2}/g).length;
}
return syllables;
}
}();