''' Purpose: translate user text to English ''' # get access to url support import url # CS 1112 translation dictionary -- entries are of form: word,translation BABEL_FISH_URL = 'http://www.cs.virginia.edu/~cs1112/words/babelfish' # get contents of babel fish url dataset = url.get_dataset( BABEL_FISH_URL ) # returns a list of lists/ table of rows # convert csv-based word mappings into dictionary format word_mappings = {} # declares an empty dict\ # we need a way to make A LOT of mappings...how do? # repeated actions... think for loop. for row in dataset: word, translation = row word_mappings[ word ] = translation #print( word_mappings ) # get the user text to be translated reply = input( 'Enter phrase to be translated: ' ) # convert reply into a list of words to_be_translated = reply.split() # process the list word by word -- that is, build up the output phrase # word by word # WE NEED A LOOP for an unknown number of words output_phrase = '' for w in to_be_translated: # how would we find the meaning of w?..we must access the value of a key #print( w, "=", t ) # How would we test whether or not the word is in our dictionary if ( w in word_mappings ): t = word_mappings.get(w) output_phrase = output_phrase + t + " " else: output_phrase = output_phrase + "*" + w + "*" + " " # print translation print( output_phrase )