-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadability.py
More file actions
69 lines (43 loc) · 1.46 KB
/
Copy pathreadability.py
File metadata and controls
69 lines (43 loc) · 1.46 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
60
61
62
63
64
65
66
67
68
69
from cs50 import get_string
def count_letters(text: str) -> int:
"""Count letters in text"""
letter_list = [letter for letter in text if letter.isalpha()]
return len(letter_list)
def count_words(text: str) -> int:
"""Count words in text"""
if len(text) == 0:
return 0
word_count = text.count(' ')
return word_count + 1
def count_sentences(text: str) -> int:
"""Count sentences in text"""
if len(text) == 0:
return 0
sentence_count = text.count('.') + text.count('?') + text.count('!')
return sentence_count
def get_coleman_liau_index(words: int, letters: int, sentences: int) -> int:
"""Calculate Coleman-Liau index for a text"""
L = (letters / words) * 100
S = (sentences / words) * 100
index = int(round(0.0588 * L - 0.296 * S - 15.8))
return index
def print_grade_level(index: int) -> None:
"""Print Coleman-Liau index for a text"""
if index >= 16:
print('Grade 16+')
elif index < 1:
print('Before Grade 1')
else:
print(f'Grade {index}')
def get_text_grade_level() -> None:
"""Outputs the grade level for a text,
according to the Coleman-Liau formula
"""
text = get_string("Text: ")
letters = count_letters(text)
words = count_words(text)
sentences = count_sentences(text)
index = get_coleman_liau_index(words, letters, sentences)
print_grade_level(index)
if __name__ == "__main__":
get_text_grade_level()