-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathreadability.py
66 lines (42 loc) · 1.56 KB
/
readability.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
https://cs50.harvard.edu/x/2023/psets/6/readability/
"""
def main():
sentence = input("Text: ")
# Number of letters, words and sentences in the string.
letters = count_letters(sentence)
words = count_words(sentence)
sentences = count_sentences(sentence)
# Average number of letters and stentences per 100 words in text.
avg_letters = (100 / float(words)) * float(letters)
avg_sentences = (100 / float(words)) * float(sentences)
# Coleman-Liau index.
index = calculate_index(avg_letters, avg_sentences)
if index < 1:
print("Before Grade 1")
elif index >= 16:
print("Grade 16+")
else:
print(f"Grade {index}")
def count_letters(sentence):
"""Counts and returns the number of letters in a string."""
letters = 0
for character in sentence:
if character.isalpha():
letters += 1
return letters
def count_words(sentence):
"""Counts and returns the number of words in a string."""
return len(sentence.split())
def count_sentences(sentence):
"""Counts and returns the number of sentences in a string."""
sentences = 0
for character in sentence:
if character in [".", "!", "?"]:
sentences += 1
return sentences
def calculate_index(avg_letters, avg_sentences):
"""Calculates the approximate grade level needed to comprehend some text according to the Coleman-Liau formula. Returns the result of the formula."""
return round(0.0588 * avg_letters - 0.296 * avg_sentences - 15.8)
if __name__ == "__main__":
main()