#!/usr/bin/python2.4 import cgi import cgitb; cgitb.enable() from count import wordcount def main(): form = cgi.FieldStorage() print "Content-Type: text/html" print fog = Fog(form) fog.ui() class Fog: def __init__(self, form): self.debug = 0 self.contents = {} self.comment = form.getvalue("comment", "") self.sentencecount, self.words = wordcount(self.comment) if self.debug: self.contents["message"] = '
' + `form` + '

\n\n' else: self.contents["message"] = '' try: self.contents["state"] = int(form.getvalue("s", 1)) except: self.contents["state"] = 0 if 'back' in form: self.contents["state"] -= 1 if 'next' in form: self.contents["state"] += 1 if self.contents["state"] == 1 or self.contents["state"] > 3 or not self.comment or not self.sentencecount: self.contents["state"] = 1 if not self.sentencecount and 'next' in form: self.contents["message"] += '

Please enter the sample with sentences in the text box below:

' self.contents["message"] += '

An example:

' self.contents["message"] += '
Helsinki is the capital of Finland. Did you you know that?
' self.first() elif self.contents["state"] < 1: self.contents["state"] = 1 self.comment = '' self.first() elif self.contents["state"] == 2: self.second() elif self.contents["state"] == 3: self.third(form) def first(self): self.contents["message"] += '' self.contents["back"] = 'Clear' def second(self): self.contents["message"] += '

Please select hard or big words, with more than 3 syllabyles

' #self.contents["message"] += '

Sentence count: %s

' % self.sentencecount #self.contents["message"] += '

Word count: %s

' % len(self.words) for word in self.unique(self.words.keys()): self.contents["message"] += '%s' % (word, word) self.contents["message"] += '' % self.comment def unique(self, list): """ Return (sorted) unique elements """ d = {} for x in list: d[x] = 1 subset = d.keys() subset.sort(self.bigstrings) return subset def bigstrings(self, x, y): """ comparison function to return largest string """ if len(x) < len(y): return 1 if len(x) > len(y): return -1 if len(x) == len(y): return 0 def valueadd(self, d, l): """ Adds the dict d values for the corresponding keys in the list l """ total = 0 for i in l: if i in d: total += d[i] return total def third(self, form): wcount = len(self.words) hardwords = form.getlist("hardwords") q = form.getvalue("q", 0.4) q = float(q) # check if self.debug: self.contents["message"] += '
%s
' % hardwords hardwordcount = self.valueadd(self.words, hardwords) if hardwordcount: avghardword = (float(hardwordcount) / float(wcount)) * 100 else: avghardword = 0 avgwordpersentence = (float(wcount) / float(self.sentencecount)) self.contents["message"] += '

= %s * ((number of words) / (number of sentences) + percentage of hard words)

' % q self.contents["message"] += '

= %s * (%s + %s)

' % (q, avgwordpersentence, avghardword) self.contents["message"] += '

= %s

' % (q * (avgwordpersentence + avghardword)) self.contents["message"] += '
' self.contents["message"] += '
Number of words
%s
' % wcount self.contents["message"] += '
Number of sentences
%s
' % self.sentencecount self.contents["message"] += '
Average number of words per sentence
%s
' % avgwordpersentence self.contents["message"] += '
Number of hard words
%s
' % hardwordcount self.contents["message"] += '
Average number of hard words
%s
' % (str(avghardword) + '%') self.contents["message"] += '
' self.contents["message"] += '' % self.comment self.contents["message"] += desc self.contents["next"] = 'Start over' def ui(self): if "back" not in self.contents: self.contents["back"] = 'Back' if "next" not in self.contents: self.contents["next"] = 'Next' print template % self.contents template = """ Gunning Fog Index - (%(state)d of 3)
%(message)s
""" desc = """

Scale guideline

Easy reading range is 6-10. The average person reads at the level 9. Anything above 17th level is difficult for university students.

Limitations

Best use of this Index

Links

""" if __name__ == "__main__": main()