""" Purpose: translate user text to English """ # get access to url support import url # translation dictionary -- entries are of form: word,translation BABEL_FISH_URL = "http://www.cs.virginia.edu/~cs1112/words/babelfish" # delimiters for unknown words UNKNOWN_WORD_MARKER = "_" # we changed this from a ? # get contents of babel fish url as a dictionary babelfish_dictionary = url.get_dictionary( BABEL_FISH_URL ) # we changed this because dictionary was misspelled # get the user text to be translated reply = input( "Enter phrase to be translated: " ) # convert reply into a list of words words_to_be_translated = reply.split() # initialize translation accumulator translation = "" # want a string, so starts as an empty string # process the words for translation one by one. each word contributes to the translation for current_word in words_to_be_translated : # whether current word is in the dictionary, determines our translation action if ( current_word in babelfish_dictionary ) : translation_of_current_word = babelfish_dictionary.get( current_word ) # since we checked for membership, we could also say # translation_of_current_word = babelfish_dictionary[ current_word ] else : translation_of_current_word = UNKNOWN_WORD_MARKER + current_word + UNKNOWN_WORD_MARKER # for each word, look at it. if it is a key in the babelfish dictionary, get the translation # if it is not in the dictionary, get the word surrounded by underscores, our UNKNOWN_WORD_MARKER # now, we need to update our accumulator translation = translation + translation_of_current_word + " " # print translation print( translation )