Text file reading Grade
WHAT DOES MY PYTHON SCRIPT DO?
Any sequence of characters that are not whitespace.
Any sequence of words that have punctuation mark( a question mark,colon or semicolon,exclamation mark,and a full stop)
Any word of three characters or less; or any vowel
(a, e, i, o, u) or a pair of consecutive vowels, except
for a final -es, -ed, or -e that is not -le.
VERSION OF PYTHON AM USING.
Algorithm of Flesch Index.
- Get user’s text file
- count words
- count sentence
- count syllables
- compute Flesch Index
FLESCH INDEX PYTHON SCRIPT
UserFileName=raw_input(“Enter file name:”)
if (UserFileName in listOfdir) and (UserFileName.endswith(‘.txt’)):
sentence=text.count(“.”) + text.count(‘!’) +
text.count(“;”) + text.count(“:”) +
for word in text.split():
for vowel in [‘a’,‘e’,‘i’,‘o’,‘u’]:
syllable += word.count(vowel)
for ending in [‘es’,‘ed’,‘e’]:
syllable -= 1
syllable += 1
if G >= 0 and G <=30:
print ‘The Readability level is College’
elif G >= 50 and G <=60:
print ‘The Readability level is High School’
elif G >= 90 and G <=100:
print ‘The Readability level is fourth grade’
print ‘This text has %d words’ %(words)
elif UserFileName not in listOfdir:
print “This text file does not exist in current directory”
elif not(UserFileName.endswith(‘.txt’)): print “This is not a text file.”
If you look closely at the above python index script,you will notice some additions that were made that was not in the algorithm.I added them to prevent ERROR in our program.Also,the program was not properly indented.
Explanation of the FLESCH INDEX PYTHON SCRIPT.
The python os module provides methods that help you perform file processing operation such as viewing the list of file in the current directory.
The getcwd() displays the current working directory of where our python program is located.Our text file has to be saved in the same directory of our python program to avoid ERROR in our program.
dire is the location of our currently working directory.
The listdir(dire) displays a list of files in the current working directory of the object dire.
So far,we have succeeded in knowing the name of files in our
current working directory.
Our next block of code is:
The if statement avoids ERROR in case the input file is not a text file or it does not exist in our current working directory.
Our next block of code executes the algorithm of our FLESCH INDEX PYTHON SCRIPT.
Our program will have to output a result, so the conditional statement will do that perfectly well. The last line prints the number of words in our text file.
We are almost through,but we have to write an error message to complete our program.The first elif statement tells the user that the text file does not exist in the current directory while the second elif statement tells the user that the input file is not a text file.
Finally,we are through and I believe you can now write your FLESCH INDEX PYTHON SCRIPT TO DETERMINE THE READABILITY OF A TEXT FILE.
Download this program here
If you are a python developer,view this project on my Github page
If you need further explanation,please feel free to comment or likewise share this post.