Gunning Fog Index

The Gunning Fog Index is a readability test that estimates the years of formal education needed to understand a text on the first reading. It takes into account the number of words, the number of complex words (words with three or more syllables), and the number of sentences in a text.
import re

def count_words(text):
    """Counts the number of words in a text."""
    words = re.findall(r'\w+', text)
    return len(words)

def count_sentences(text):
    """Counts the number of sentences in a text."""
    sentences = re.split(r'[.!?]+', text)
    return len(sentences) - 1 if text[-1] in '.!?' else len(sentences)

def count_complex_words(text):
    """Counts the number of complex words in a text (words with three or more syllables)."""
    def syllable_count(word):
        word = word.lower()
        vowels = "aeiouy"
        count = 0
        if word[0] in vowels:
            count += 1
        for index in range(1, len(word)):
            if word[index] in vowels and word[index - 1] not in vowels:
                count += 1
        if word.endswith("e"):
            count -= 1
        if count == 0:
            count += 1
        return count

    words = re.findall(r'\w+', text)
    complex_words = [word for word in words if syllable_count(word) >= 3]
    return len(complex_words)

def gunning_fog_index(text):
    """Calculates the Gunning Fog Index for a given text."""
    num_words = count_words(text)
    num_sentences = count_sentences(text)
    num_complex_words = count_complex_words(text)

    if num_sentences == 0:
        return 0  # Avoid division by zero

    average_sentence_length = num_words / num_sentences
    percentage_complex_words = (num_complex_words / num_words) * 100

    fog_index = 0.4 * (average_sentence_length + percentage_complex_words)
    return fog_index

# Example usage
text = (
    "The Gunning Fog Index is a readability test for English writing. "
    "It estimates the years of formal education needed to understand the text on the first reading. "
    "Complex words are those with three or more syllables."
)

fog_index = gunning_fog_index(text)
print(f"The Gunning Fog Index for the given text is: {fog_index:.2f}")


OUTPUT

The Gunning Fog Index for the given text is: 10.40


Gunning Fog Index Explanation

The Gunning Fog Index is a readability test that estimates the years of formal education needed to understand a text on the first reading. The implementation calculates the Gunning Fog Index using the following steps:

  1. Words:
  2. Sentences:
  3. Complex Words:
  4. Gunning Fog Index Calculation:

Usage:

This implementation provides a way to rank texts based on readability using the Gunning Fog Index, which is useful in various Natural Language Processing (NLP) applications such as document analysis, content creation, and education.