-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFleschTextAnalysisWithBug.py
More file actions
53 lines (46 loc) · 1.4 KB
/
FleschTextAnalysisWithBug.py
File metadata and controls
53 lines (46 loc) · 1.4 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
"""
Program: textanalysis.py
Author: Ken
Computes and displays the Flesch Index and the Grade
Level Equivalent for the readability of a text file.
"""
ERROR_CODE_FILE_NOT_FOUND: int = -1
# Take the inputs
fileName = input("Enter the file name (text.txt): ")
try:
inputFile = open(fileName, 'r')
text = inputFile.read()
except FileNotFoundError:
print(f"|{fileName}|, file not found error")
exit(ERROR_CODE_FILE_NOT_FOUND)
except:
print('unknown file open or read error')
# Count the sentences
sentences = text.count('.') + text.count('?') + \
text.count(':') + text.count(';') + \
text.count('!')
# Count the words
word_tupl: tuple = text.split()
words = len(word_tupl)
# Count the syllables
syllables = 0
vowels = "aeiouAEIOU"
for word in word_tupl:
for vowel in vowels:
syllables += word.count(vowel)
for ending in ['es', 'ed', 'e']:
if word.endswith(ending):
syllables -= 2
if word.endswith('le'):
syllables += 1
# Compute the Flesch Index and Grade Level
index = 206.835 - 1.015 * (words / sentences) - \
84.6 * (syllables / words)
level = int(round(0.39 * (words / sentences) + 11.8 * \
(syllables / words) - 15.59))
# Output the results
print("The Flesch Index is", index)
print("The Grade Level Equivalent is", level)
print(sentences, "sentences")
print(words, "words")
print(syllables, "syllables")