-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadability.py
71 lines (35 loc) · 1.06 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
67
68
69
70
71
import re
def main():
while True:
text = input("Text: ")
if len(text) > 0:
break
letters = count_letters(text)
words = count_words(text)
sentences = count_sentences(text)
cl_index = round(0.0588 * ((letters/words)*100) - 0.296 * ((sentences/words)*100) - 15.8)
if cl_index < 1:
print("Before Grade 1")
elif cl_index > 16:
print("Grade 16+")
else:
print(f"Grade {cl_index}")
return
def count_letters(target_text):
pattern = re.compile('[a-z]', re.IGNORECASE)
matches = pattern.findall(target_text)
# print(len(matches))
# letters = len(matches)
return len(matches)
def count_words(target_text):
pattern = re.compile('\w+')
matches = pattern.findall(target_text)
# words = len(matches)
return len(matches)
def count_sentences(target_text):
pattern = re.compile(r'[A-Z][^\.!?]*[.!?]')
matches = pattern.findall(target_text)
# sentences = len(matches)
return len(matches)
if __name__ == "__main__":
main()