''' 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" #If word is not known, tell user that we cannot translate it --> surround # with ?"wrd"? # delimiters for unknown words UNKNOWN_WORD_START_MARKER = "?" UNKNOWN_WORD_END_MARKER = "?" # get contents of babel fish url as a dictionary babelfish_dictionary = url.get_dictionary( BABEL_FISH_URL ) #use url.get_dictionary(link) to get the correct format for dict # get the user text to be translated ''' This part translates only one word! reply = input( "Enter one word: " ) #just get one word from user #clean it up word = reply.strip() #get rid of any accidental leading/trailing whitespaces word = word.lower() #lower it bc all words in dict are in lower case #if word is NOT in babelfish --> blows up for now until we handle it later #so check if word is IN babelfish so that the code doesn't blow up #when we run the following line if ( word in babelfish_dictionary ): #checks if word is a key in dictionary #then it must have a value drow = babelfish_dictionary[ word ] #get value (translation) of #key "word" from dictionary else: #else: want to put ? around the word drow = UNKNOWN_WORD_START_MARKER + word + UNKNOWN_WORD_END_MARKER print( drow ) #print translation #word = lapizz --> drow = ?lapizz? #because lapizz is not in babelfish #so it cannot be translated ''' ########### This part translates a whole sentence ############## # get user reply reply = input( "Enter phrase to be translated: " ) text = reply.lower().strip() #strip and lower on the same line # convert reply into a list of words # get list because we want to translate ONE word at a time # convert string to list of string words words_to_be_translated = text.split() #print( words_to_be_translated ) # initialize translation accumulator # going to store final translated STRING after accumulation translation = "" # string because final answer is a string # empty because we don't have anything right now # when building strings, concatenate two strings into one string with "+" # process the words for translation one by one. each word contributes to the translation # for EACH word in list, need to translate it # dictionary is a set of relationships # key : value # in this case key = word (to be translated); # value = translation of key word #you cannot index into a dictionary: #babelfish_dictionary[0] is not a valid operation #unless 0 is a key in babelfish_dictionary # what list are you looking in? for current_word in words_to_be_translated: # whether current word is in the dictionary, determines our translation action #you currently have a "key" = current_word # and now you need to get the "value" #print( 'current_word =', current_word ) if (current_word in babelfish_dictionary): meaning = babelfish_dictionary[ current_word ] else: meaning = UNKNOWN_WORD_START_MARKER + current_word + UNKNOWN_WORD_END_MARKER # variable meaning takes on different values depending # on the conditional statement translation = translation + meaning + " " # meaning = babelfish_dictionary.get( current_word ) # get() returns the value of current_word; # returns None if the value is None # None is a key word! not a string # It's logical expression (like True, False) # print translation print( translation )