Created
December 5, 2021 22:55
-
-
Save boulama/334a9867c2a7de523ec76eb264de42cb to your computer and use it in GitHub Desktop.
JavaScript Implementation of the Flesch Reading-Ease algorithm to determine how difficult a passage in English is to understand.
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
/** | |
* Copyright (c) 2021 | |
* | |
* JavaScript Implementation of the Flesch Reading-Ease algorithm to determine how difficult a passage in English is to understand. | |
* Reference: https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests | |
* @summary Flesch Reading-Ease algorithm to determine how difficult a passage in English is to understand. | |
* @author Boulama K. <boulama@otimbi.com> | |
*/ | |
/* | |
Function to calculate the number of syllables in a word. | |
*/ | |
function countSyllables(word) { | |
word = word.toLowerCase(); //word.downcase! | |
if(word.length <= 3) { return 1; } //return 1 if word.length <= 3 | |
word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, ''); //word.sub!(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '') | |
word = word.replace(/^y/, ''); //word.sub!(/^y/, '') | |
return word.match(/[aeiouy]{1,2}/g).length; //word.scan(/[aeiouy]{1,2}/).size | |
} | |
/* | |
Function to calculate the number of syllables in a piece of text. | |
*/ | |
function countAllSylables(text) { | |
let all_words = getAllWords(text); | |
let total_words = all_words.length; | |
var total_syllables = 0; | |
for(i=0;i<total_words;i++) { | |
total_syllables += countSyllables(all_words[i]); | |
} | |
return total_syllables; | |
} | |
/* | |
Function to get the words present in a piece of text. | |
*/ | |
function getAllWords(text) { | |
return text.split(' '); | |
} | |
/* | |
Function to calculate the number of words present in a piece of text. | |
*/ | |
function countWords(text) { | |
return getAllWords(text).length; | |
} | |
/* | |
Function to calculate the number of sentences. | |
*/ | |
function countSentences(text) { | |
let sentences = text.split('.'); | |
let total_sentences = sentences.length; | |
var total_actual_sentences = 0; | |
for(i=0;i<total_sentences;i++) { | |
var current_string = sentences[i]; | |
current_string = current_string.replace(/\s/g, ""); | |
/* | |
I will determine here the minimum length of a sentence to be 5 characters to be considered. | |
*/ | |
if(current_string.length > 5) { | |
total_actual_sentences++ | |
} | |
} | |
return total_actual_sentences; | |
} | |
/* | |
Calculate the readability score of a piece of text using the Flesch–Kincaid readability algorithm | |
Reference: https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests | |
*/ | |
function readability(text = null) { | |
let total_words = countWords(text); | |
let total_sentences = countSentences(text); | |
let total_syllables = countAllSylables(text); | |
let school_level; | |
let notes; | |
var FK = 206.835 - (1.015 * (total_words/total_sentences)) - (84.6 * (total_syllables/total_words)); | |
console.log(FK); | |
if(FK >= 90 && FK <= 100) { | |
school_level = "5th grade" | |
notes = "Very easy to read. Easily understood by an average 11-year-old student." | |
} | |
if(FK >= 80 && FK < 90) { | |
school_level = "6th grade" | |
notes = "Easy to read. Conversational English for consumers." | |
} | |
if(FK >= 70 && FK < 80) { | |
school_level = "7th grade" | |
notes = "Fairly easy to read." | |
} | |
if(FK >= 60 && FK < 70) { | |
school_level = "8th & 9th grade" | |
notes = "Plain English. Easily understood by 13- to 15-year-old students." | |
} | |
if(FK >= 50 && FK < 60) { | |
school_level = "10th to 12th grade" | |
notes = "Fairly difficult to read." | |
} | |
if(FK >= 30 && FK < 50) { | |
school_level = "College" | |
notes = "Difficult to read." | |
} | |
if(FK >= 10 && FK < 30) { | |
school_level = "College graduate" | |
notes = "Very difficult to read. Best understood by university graduates." | |
} | |
if(FK >= 0 && FK < 10) { | |
school_level = "Professional" | |
notes = "Extremely difficult to read. Best understood by university graduates." | |
} | |
return [school_level, notes]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment