''' Purpose: produce spell-check corrected text ''' # get access to url support import url # define annotation markers CORRECTED_WORD_START_MARKER = "*" CORRECTED_WORD_END_MARKER = "*" UNKNOWN_WORD_START_MARKER = "?" UNKNOWN_WORD_END_MARKER = "?" # CS 1112 most common words MOST_COMMON_URL = "http://www.cs.virginia.edu/~cs1112/words/most-common" # CS 1112 words corrections CSV CORRECTIONS_URL = "http://www.cs.virginia.edu/~cs1112/words/corrections" # get words from most common words url good_words = url.get_words( MOST_COMMON_URL ) # new function from url -- hands back # contents of MOST_COMMON_URL as list # of words # get corrections dictionary misspellings_dictionary = url.get_dictionary( CORRECTIONS_URL ) # get the user text to be checked reply = input( "Enter text: " ) # clean up the user response cleaned_up_reply = reply.strip().lower() # strip() hands back a stripped string # to lower(). lower() takes that string # hands back a string that is both # lower case and stripped. # convert cleaned up response into a list of words from the user text = cleaned_up_reply.split() # what now -- accumulate the spell check output of the user text spell_checked_text = "" for current_word in text: # for each word there is one of three possibilities for the word: # it is correctly spelled; # it is incorrectly spelled but the likely correct spelling is known; # or, it is incorrecty spelled and no correction is available. # determine which possibility for the word applies to determine its entry # to the corrected text. if ( current_word in good_words ): # correctly spelled -- so the current word is what we want to add to # spell checked text entry = current_word elif ( current_word in misspellings_dictionary ): # incorrectly spelled -- but a correction is known, so get it correction = misspellings_dictionary[ current_word ] # its entry is the correction surrounded by the correct markers entry = CORRECTED_WORD_START_MARKER + correction + CORRECTED_WORD_END_MARKER else : # it is incorrecty spelled and no correction is available. so its entry # is the misspelled word surrounded by unknown word markers entry = UNKNOWN_WORD_START_MARKER + current_word + UNKNOWN_WORD_END_MARKER # to finish off this iteration of the loop, add the entry to the output accumulation spell_checked_text = spell_checked_text + entry + " " # print spell checked result print( spell_checked_text ) # outside the loop, so we only print # when done # HAVE A GREAT SPRING BREAK