''' 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_csv_dataset( BABEL_FISH_URL ) # convert csv-based word mappings into dictionary format word_mappings = {} for row in dataset: # what is row? # compare this syntax to what we did in good_doggie.py; what similarities and differences do you see? word, translation = row # print( 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 line = '' # let's make output the accumulation for user_word in to_be_translated : # translate user_word if ( user_word in word_mappings.keys() ): # if we know about the word (i.e. it's in our mappings), we want to print its translation # can check if user_word is in word_mappings OR if it's in word_mappings.keys() output = word_mappings[ user_word ] else: # otherwise we have some kind of indication that we don't know about the word output = "*" + user_word + "*" line = line + output + " " print( output, end=" " ) # how does this method differ from the accumulation method? print() # separates output from line with a new line print( line ) # print( dataset ) # print( word_mappings ) # print( ) # this is a very rudimentary translation program, but lots of translation programs are kind of # rudimentary anyways...