forked from winner9871/the-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflesch.py
60 lines (51 loc) · 1.77 KB
/
flesch.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
"""
This program calculate and display Flesch index of writting inputted throug a
file
"""
import os
# Constants Defination
VOWELS = ("a", "e", "i", "o", "u")
SYLLABLE_END_EXCEPTIONS = ("es", "ed")
while True:
fname = input("Enter the file whose flesch index to be calculate :\t")
if os.path.exists(fname):
break
else:
print(f"{fname} doesn't exists please enter a valid file name")
with open(fname , "rt") as fobj:
content = fobj.read()
# Getting the sentances from content
sentences = content.replace(".", "#Sentence Ended Here#")
sentences = sentences.replace("?", "#Sentence Ended Here#")
sentences = sentences.replace("!", "#Sentence Ended Here#")
sentences = sentences.replace(":", "#Sentence Ended Here#")
sentences = sentences.replace(";", "#Sentence Ended Here#")
sentences = sentences.split("#Sentence Ended Here#")
# Getting Words from sentances
words = []
for sentence in sentences: words += sentence.split()
# Getting Syllable from words
syllablesCount = 0
for word in words:
for vowel in VOWELS:
syllablesCount += word.count(vowel)
for ending in ['es', 'ed', 'e']:
if word.endswith(ending):
syllablesCount -= 1
if word.endswith('le'):
syllablesCount += 1
# Counters
wordCount = len(words)
sentencesCount = len(sentences)
# syllablesCount defined at line 34
# Compute the Flesch Index and Grade Level
index = 206.835 - 1.015 * (wordCount / sentencesCount) - \
84.6 * (syllablesCount / wordCount)
level = round(0.39 * (wordCount / sentencesCount) + 11.8 * \
(syllablesCount / wordCount) - 15.59)
# Output the results
print("The Flesch Index is", index)
print("The Grade Level Equivalent is", level)
print(len(sentences), "sentences")
print(len(words), "words")
print(syllablesCount, "syllables")