Categories We Write About

Build a tool to calculate content readability

Creating a tool to calculate content readability typically involves implementing readability formulas such as the Flesch-Kincaid Grade Level, Flesch Reading Ease, Gunning Fog Index, SMOG Index, or Coleman-Liau Index. Here’s a simple Python script that calculates several readability scores for any given text input.

python
import re import math def count_syllables(word): word = word.lower() vowels = "aeiouy" syllables = 0 if word[0] in vowels: syllables +=1 for index in range(1, len(word)): if word[index] in vowels and word[index-1] not in vowels: syllables +=1 if word.endswith("e"): syllables -= 1 if syllables == 0: syllables = 1 return syllables def count_words(text): words = re.findall(r'bw+b', text) return len(words) def count_sentences(text): sentences = re.findall(r'[.!?]+', text) return len(sentences) if len(sentences) > 0 else 1 def count_syllables_in_text(text): words = re.findall(r'bw+b', text) syllables = sum(count_syllables(word) for word in words) return syllables def flesch_reading_ease(text): words = count_words(text) sentences = count_sentences(text) syllables = count_syllables_in_text(text) # Flesch Reading Ease formula score = 206.835 - 1.015 * (words / sentences) - 84.6 * (syllables / words) return round(score, 2) def flesch_kincaid_grade(text): words = count_words(text) sentences = count_sentences(text) syllables = count_syllables_in_text(text) # Flesch-Kincaid Grade Level formula score = 0.39 * (words / sentences) + 11.8 * (syllables / words) - 15.59 return round(score, 2) def gunning_fog_index(text): words = count_words(text) sentences = count_sentences(text) complex_words = count_complex_words(text) score = 0.4 * ((words / sentences) + 100 * (complex_words / words)) return round(score, 2) def count_complex_words(text): words = re.findall(r'bw+b', text) complex_words = [word for word in words if count_syllables(word) >= 3] return len(complex_words) def smog_index(text): sentences = count_sentences(text) complex_words = count_complex_words(text) if sentences >= 3: score = 1.0430 * math.sqrt(complex_words * (30 / sentences)) + 3.1291 return round(score, 2) else: return None # Not enough sentences for SMOG calculation def coleman_liau_index(text): words = count_words(text) sentences = count_sentences(text) letters = len(re.findall(r'[A-Za-z]', text)) L = (letters / words) * 100 S = (sentences / words) * 100 score = 0.0588 * L - 0.296 * S - 15.8 return round(score, 2) def readability_report(text): report = { "Flesch Reading Ease": flesch_reading_ease(text), "Flesch-Kincaid Grade Level": flesch_kincaid_grade(text), "Gunning Fog Index": gunning_fog_index(text), "SMOG Index": smog_index(text), "Coleman-Liau Index": coleman_liau_index(text) } return report # Example usage: if __name__ == "__main__": sample_text = """Your content here. Replace this with the text you want to analyze for readability.""" results = readability_report(sample_text) for metric, score in results.items(): print(f"{metric}: {score}")

This tool:

  • Counts words, sentences, syllables, and complex words.

  • Calculates five common readability scores.

  • Returns a dictionary of scores for your content.

You can integrate this into a website backend, CLI, or any Python environment to measure readability. Would you like it adapted for a web interface or another language?

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About