IFRAME SYNC
import re
def calculate_flesch_kincaid(text):
# Remove non-alphabetic characters and split the text into words
words = re.findall(r'\b\w+\b', text)
# Count the number of words, sentences, and syllables
num_words = len(words)
num_sentences = text.count('.') + text.count('!') + text.count('?')
num_syllables = sum(count_syllables(word) for word in words)
# Calculate the Flesch-Kincaid Grade Level
if num_words == 0 or num_sentences == 0:
return 0 # Avoid division by zero
else:
fk_grade = 0.39 * (num_words / num_sentences) + 11.8 * (num_syllables / num_words) - 15.59
return round(fk_grade, 2)
def count_syllables(word):
# Simple syllable counting algorithm
vowels = "aeiouy"
count = 0
word = word.lower()
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
def main():
# Example text
text = """Readability is the ease with which a reader can understand a written text. In natural language,
the readability of text depends on its content (the complexity of its vocabulary and syntax) and its
presentation (such as typographic aspects like font size, line height, and line length)."""
# Calculate Flesch-Kincaid Grade Level
fk_grade = calculate_flesch_kincaid(text)
# Display the result
print(f"Flesch-Kincaid Grade Level: {fk_grade}")
if __name__ == "__main__":
main()
IFRAME SYNC
Comments
Post a Comment