Ralph ZellerMon Feb 17 14:26:28 PST 2003
Sorry this message is long, but I know at least Horst will read it! My wife is a writer and a new requirement given to her by her publishers is to edit her material to within certain scores on a readability index. Apparently MS-Word has some capability of computing readabiliy indexes, so these indexes are presumed to be readily available to writers. It seemed like an interesting exercise to create a flesch score program using Python. This program is slower than one I found which was written in cpp, but it serves my need. --- #!/usr/bin/env python2 # Created 2/16/03 Ralph Zeller [Python 2.2 (#1, Apr 12 2002, 15:29:57)] # given a text file, compute the Flesch readability score. # # Rudolph Flesh invented this index which is used by publishers to # judge the readability of English writing, with an objective measure. # The model purports that long sentences filled with long words are # less readable than short sentences with short words. # The index is better described at: # http://www.mang.canterbury.ac.nz/courseinfo/AcademicWriting/Flesch.htm # # The formula I used for counting syllables is based loosely on comments # that I read in a c++ program created by Roger Frank that I found at # http://www.rfrank.us/ import sys,string try: fn=sys.argv[1] f=open(fn,'r') f.close() except: print "Usage:",sys.argv[0], "filename.txt" sys.exit() print "Reading file:",fn f=open(fn,'r') t='0' dot=0 count_syllables=0.0 count_words=0.0 count_sentences=0.0 #words=[] # Container for database of words # Setup a translation table to remove punctuation tt='' for x in range(256): tt+=chr(x) dc='~!@#$%^&*()_+=-`{}|\\][\';/.,<>? :"' #delete chars # Main loop while t<>'': t=f.readline() s=t.split() for word in s: # First look for sentences. for c in ['.',';',':','?','!']: if word.find(c)>=0: count_sentences+=1 # print a dot for every sentence (to show progress). dot+=1 if dot>4: dot=0 print '.', # Strip out the funny characters word=word.translate(tt,dc) word=word.lower() #print word, if len(word)>0: count_words+=1 #words.append(word) # fill database of words (why? statistics) # Count syllables count = 0 vowels=['a','e','i','o','u','y'] # Count all vowels as a syllable, unless they are together. # This doesn't catch words like "variety", oh well. for x in range(len(word)-1): if word[x] in vowels: if word[x+1] not in vowels: count+=1 # count a syllable if last character is a vowel other than 'e' if word[len(word)-1] in ['a','i','o','u','y']: count+=1 # Every word counts as at least one syllable if count<1: count=1 count_syllables+=count #report the results if count_words<1: print "No words found." else: print " " print "Flesch Readability Score" print str(int(round(count_syllables,0))),"syllables" print str(int(round(count_words,0))), "words" print str(int(round(count_sentences,0))),"sentences" asl=count_words/count_sentences awl=count_syllables/count_words print "Average sentence length:",str(round(asl,1)),"words per sentence" print "Average word length:",str(round(awl,1)),"syllables per word" fscore=206.835-(1.015*asl+84.6*awl) print "**********Flesch score:",str(round(fscore,1)),'**********' print "Scale 90-100 = very easy = 5th grade = Comics" print " 80- 90 = easy = 6th grade = Magazine ads" print " 70- 80 = very easy = 7th grade = Movie screen" print " 60- 70 = plain English = 8th & 9th = Reader's Digest" print " 50- 60 = fairly difficult = 10th-12th = Time Magazine" print " 40 = difficult to read = college = Wall St Journal" print " 10- 20 = very difficult = grad coll = Insurance policy" f.close() _______________________________________________ Think mailing list Think at zoneverte.org http://mail.zoneverte.org/cgi-bin/listinfo/think