-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreadability.py
More file actions
59 lines (41 loc) · 1.25 KB
/
Copy pathreadability.py
File metadata and controls
59 lines (41 loc) · 1.25 KB
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
# Computes the approximate grade level needed to comprehend some text
# according to the Coleman-Liau formula
from cs50 import get_string
def main():
# asks user for input
text = get_string("Text: ")
grade = calculate_readability(text)
print(handle_grade(grade))
# calculates readability of the given text
def calculate_readability(text):
letter_count = 0
word_count = 1
sentence_count = 0
for i in range(len(text)):
c = text[i]
if (str.isalpha(c)):
letter_count += 1
if (c == ' '):
word_count += 1
if (is_punctuation_end(c)):
sentence_count += 1
# letters per 100 words
L = letter_count / word_count * 100
# sentences per 100 words
S = sentence_count / word_count * 100
# Coleman-Liau formula to calculate readability of a text
index = round(0.0588 * L - 0.296 * S - 15.8)
return index
# checks whether or not char is punctuation mark
def is_punctuation_end(c):
return c == '.' or c == '!' or c == '?'
# return's user-frienldy output
def handle_grade(g):
if (g < 0):
return "Before Grade 1"
elif (g >= 16):
return "Grade 16+"
else:
return f"Grade {g}"
if __name__ == "__main__":
main()