''' Purpose: produce spell-check corrected text ''' # define annotation markers CORRECTED_WORD_START_MARKER = "*" CORRECTED_WORD_END_MARKER = "*" UNKNOWN_WORD_START_MARKER = "?" UNKNOWN_WORD_END_MARKER = "?" # get access to url support import url # CS 1112 most common words MOST_COMMON_URL = "http://www.cs.virginia.edu/~cs1112/words/most-common" # CS 1112 words correctsions CSV CORRECTIONS_URL = "http://www.cs.virginia.edu/~cs1112/words/corrections" # get contents of most common words url contents = url.get_contents( MOST_COMMON_URL ) # get_contents() returns a large string of everything on the webpage # split contents into a list of good words good_words = contents.split() # url.get_words( MOST_COMMON_URL ) # This is a list of words now # get corrections dictionary misspellings_dictionary = url.get_dictionary( CORRECTIONS_URL ) # get the user text to be checked reply = input( "Enter text: " ) # convert reply into a list of words text = reply.split() # list of words is stored in text # what now -- accumulate the spell check output of the user text output = '' # Accumulator where we wanna build up this string with our words list for current_word in text: # Find out if the current word is correctly spelled if ( current_word in good_words ): # If it's aleady correctly spelled, just use the word! meaning = current_word # Find out if the current word is commonly misspelled and find the correct spelling using the dictionary elif ( current_word in misspellings_dictionary ): # If it's not correctly spelling, try to find the right spelling! meaning = misspellings_dictionary.get( current_word ) # Get the dictionary mapping of the current_word meaning = '*' + meaning + '*' # We can't find the correct spelling (unknown) else: # It's misspelled and we can't find the dictionary, it's an unknown spelling meaning = '?' + current_word + '?' output = output + meaning + ' ' # Add the converted meanings of each word (correct spellings) to our output string # print spell check results print( output )