''' 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 urllib.request # need access to Python provided URL support stream = urllib.request.urlopen( BABEL_FISH_URL ) # connect to babel fish url page = stream.read() # read stream to get encoded contents text = page.decode() # decode page to get contents as text text = text.strip() # clean up text # get contents as a list of lines lines = text.split( "\n" ) # split on newline character # get the babel fish dictionary out of the lines babelfish_dictionary = {} # initialize the dictionary # build dictionary out of the lines for line in lines : # process lines one by one # line in CSV format, need to split it on comma word, meaning = line.split( "," ) # line consists of a word and its translation word, meaning = word.strip(), meaning.strip() # clean up word and meaning babelfish_dictionary[ word ] = meaning # add another entry to dictionary` # 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 )