''' 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 as a dictionary babelfish_dictionary = url.get_dictionary( BABEL_FISH_URL ) # Get dictionary takes text from page, converts it to # dataset and then converts it into a dictionary! url.get_dictionary() does this for you from a csv/file on a page! # Get contents just gets the text from a page as a big string. RETURNS BIG STRING! "..." # Get dataset will return the csv as a list of lists (dataset). RETURNS LIST OF LISTS! [[],[],[]] # Get dictionary will return a dictionary of the text file as a dictionary. RETURNS DICTIONARY! { k:v, k:v,...} # get the user text to be translated reply = input( "Enter phrase to be translated: " ) # reply is "donde esta mi lapiz" # convert reply into a list of words words_to_be_translated = reply.split() # ['donde', 'esta', 'mi', 'lapiz'] # WORDS BASED OFF OF STRING # initialize translation accumulator translation = "" # WE WANT TO BUILD UP A TRANSLATION SO THIS TRANSLATION WILL EVENTUALLY BE "where is my pencil" # process the words to be translated one by one. each word will contribute to # the accumulation for word in words_to_be_translated : # Go through each word to translate one by one # see if word is in the dictionary, that will determine our translation action # !!!! drow is the translation of each word -> get translation if it's in our dictionary otherwise it's ?word? if ( word in babelfish_dictionary ): # IF THE WORD IS A KEY IN BABELFISH DICTIONARY (if 'donde is in our dict) drow = babelfish_dictionary.get( word ) # get the translation of the word from our dictionary .get( word ) # since word is the key and we want the value (translation) # You can also do babelfish_dictionary[ word ] else: # If the word is not in our dictionary for us to translate drow = "?" + word + "?" # The translation will be ?word? translation = translation + drow + " " # Add each word to our translation so that it builds up the translation # print(word, drow) # print word to translate in our list of words from reply and translation # If you indent the translation accumulator update into else, it only add the wrong words to the translation. # Likewise, only indenting the translation accumulator update into if only adds the right words (the # words found in our dictionary to translate) # We want to add the translation of the word regardless of whether it's in our dictionary or not because # we have our translation as either the translation from the dictionary or the flagged word (?word?)! # EACH WORD HAS A "translation" # Hint: Use if/else the word is in our dictionary and see what we add to our translation :) # Remember: We wanna process each word in the user text word by word and get it's translation from the # dictionary if it exists and if it's not in our dictionary, it should be ?word? with the word! # What dictionary functions can we use? # Remember dictionaries look like { key: value, key : value, ...} # print translation print( translation ) print( reply )