#!/usr/bin/python import cgi import string import os import locale locale.setlocale(locale.LC_ALL, "de_DE") # import cgitb for debugging purposes try: import cgitb cgitb.enable() except: sys.stderr = sys.stdout # determine script name if os.environ.has_key("SCRIPT_NAME"): POSTURL = os.environ["SCRIPT_NAME"] else: POSTURL = "" # HTML stuff should be kept in a separate file TEXT_AREA_ID = "text" HEADER = ''' Arbeitsprobe Alex Düsel ''' FORM = '''

Eingabe



''' % (POSTURL, TEXT_AREA_ID) FOOTER = ''' ''' # process browser request print "Content-Type: text/html; charset=ISO-8859-1" print # Empty line for end of header print HEADER try: data = cgi.FieldStorage() if data.has_key(TEXT_AREA_ID): # process raw text text = data[TEXT_AREA_ID].value # split up text into words raw_words = text.split() # characters to be deleted all_chr = string.maketrans('','') delete_chr = all_chr.translate(all_chr, string.letters+'-') statistics = {} for word in raw_words: if word == '': continue #remove all invalid characters clean_word = word.translate(all_chr, delete_chr) # remove only invalid characters at beginning and at end of word # clean_word = word.strip(delete_chr) # do statistics try: statistics[clean_word] += 1 except KeyError: statistics[clean_word] = 1 # display result print "

Statistik:

" print '' print " " print " " print " " print " " keys = statistics.keys() keys.sort() for key in keys: print " " print " " print ' " print ' " print " " print "
Wort:Häufigkeit:Länge:
"+key+"'+str(statistics[key])+"'+str(len(key))+"
" else: print FORM except: cgi.print_exception() print FOOTER