''' Purpose: translate user text to English ''' # CS 1112 translation dictionary BABEL_FISH_URL = "http://www.cs.virginia.edu/~cs1112/words/babelfish" # delimiters for unknown words UNKNOWN_WORD_START_MARKER = "?" UNKNOWN_WORD_END_MARKER = "?" # get contents of babel fish url import url # need access to enhanced web support # load dictionary from the web file babelfish_dictionary = url.get_dictionary( BABEL_FISH_URL ) # get the user text to be translated reply = input( "Enter text for translation: " ) # get phrase of interest reply = reply.strip() # clean up phrase phrase = reply.lower() # put in lower case because dictionary is lower case # get phrase as list of words words_for_translation = phrase.split() # split phrase on whitespace # finally ready to start translating -- will do word by word translation = "" # translation starts off empty for word in words_for_translation: # process words one at a time # whether current word is known, determines our action if ( word in babelfish_dictionary ): # is current word part of the dictionary response = babelfish_dictionary[ word ] # it is, so response is its meaning else: # it is not, so response is a flagging of the word response = UNKNOWN_WORD_START_MARKER + word + UNKNOWN_WORD_END_MARKER # add the response to current word to the translation translation = translation + response + " " # also add a space to separate responses # print translation!! print( translation )