Skip to content

Instantly share code, notes, and snippets.

@MobliMic
Last active April 24, 2019 12:08
Show Gist options
  • Save MobliMic/61c93ea60c78b33c16b736d68e87c45f to your computer and use it in GitHub Desktop.
Save MobliMic/61c93ea60c78b33c16b736d68e87c45f to your computer and use it in GitHub Desktop.
Readability score
(()=>{
var CHARACTER_WEIGHT = 4.71;
var SENTENCE_WEIGHT = 0.5;
var BASE = 21.43;
function automatedReadability(counts) {
if (!counts || !counts.sentence || !counts.word || !counts.character) {
return NaN;
}
//flesch-kincaid
// return (206.835 - (1.015 * (counts.word / counts.sentence))) - (84.6 * (counts.syllables / counts.words));
// ARI
return (CHARACTER_WEIGHT * (counts.character / counts.word)) + (SENTENCE_WEIGHT * (counts.word / counts.sentence)) - BASE;
}
// const ari = Math.round(automatedReadability({
// sentence: 3,
// word: 69,
// character: 683
// }));
const ari = Math.round(automatedReadability({
sentence: 11,
word: 275,
character: 1348
}));
const vals = {
90: 'Very Easy',
80: 'Easy',
70: 'Fairly Easy',
60: 'Standard',
50: 'Fairly Difficult',
30: 'Difficult',
0: 'Very Confusing',
};
const valKeys = Object.keys(vals);
let currentDiff, smallestDiff = Math.abs(ari - valKeys[0]), closest = 0;
for (let i = 0; i < valKeys.length; i++) {
currentDiff = Math.abs(ari - valKeys[i]);
if (currentDiff < smallestDiff) {
smallestDiff = currentDiff;
closest = i;
}
}
return vals[valKeys[closest]];
}
)();
(() => {
const data = {};
const content = 'Cat ipsum dolor sit amet, cereal boxes make for five star accommodation roll over and sun my belly. Leave dead animals as gifts russian blue, sleep catch mouse and gave it as a present. Eat owner\'s food. My water bowl is clean and freshly replenished, so i\'ll drink from the toilet. Eat half my food and ask for more. Use lap as chair.';
// sentence count
data.sentence = content.split('.').length
data.word = content.trim().replace(/\s+/gi, ' ').split(' ').length;
data.character = content.trim().replace(/[^\w]/gi, '').length;
return data;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment