''' 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/datasets/words/babelfish' # get contents of babel fish url dataset = url.get_dataset( BABEL_FISH_URL ) # convert csv-based word mappings into dictionary format word_mappings = {} for entry in dataset: foreign, english = entry word_mappings[ foreign ] = english print( word_mappings) print() # 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 translation = '' for word in to_be_translated : # translate word if word in word_mappings: # seeing if the word is in the dict english = word_mappings.get(word) #grabbing the english of that word else: english = '>' + word + '<' # word not in dictionary translation = translation + english + ' ' #adding english word to accumulator # print translation print( translation )