''' Purpose: translate user text to English given a text, try top-down approach to translate it translate text --> translate line by line --> translate word by word ''' import url DEFAULT_URL = 'http://www.cs.virginia.edu/~cs1112/words/babel' DEFAULT_DICTIONARY = url.get_dictionary( DEFAULT_URL ) def passage( text, dictionary = DEFAULT_DICTIONARY ) : ''' returns a translation of text according to dictionary, where text may have embedded \n's default dictionary is the babel dictionary if not given a dictionary ''' # text is a giant string where there are multiple lines separated by "\n" # convert the text into lines so we can translate each line at a time # split on new line character ("\n") lines = text.split( "\n" ) # we want our result, which is the translated version of text, to be a giant string as well # we need to build this up (accumulation )because we don't have the capability to # translate the entire text for now result = '' # string accumulation --> every time we translate a line, we add on to this string for phrase in lines: # need to translate each phrase in lines convert = line( phrase, dictionary ) # we have a function (line 48) # that will translate a line/phhrase and return that translation as a string # we add the translated phrase to the string accumulator # since this is a line, we separate each line by a newline character ('\n') at the end result = result + convert + "\n" # we've broken down the giant text into lines --> for each of those lines, we translate each phrase at a time # breaking things down to smaller and smaller problems # we can't use phrase in a dictionary because the dictionary only contains word --> word translation # so we need another function to handle this return result def line( string, dictionary = DEFAULT_DICTIONARY ) : ''' returns a translation of string according to dictionary, where string is composed of zero or more words Translate a single line of text given (string) ''' # convert the string into a list of words # string is a phrase, each word separated by space words = string.split() # need to hand back the translated sentence after translated word by word # need to accumulate the definition of each word in words result = '' # for each word in words, we need to translate it for w in words: conversion = word( w, dictionary ) # we have another function called word() where we can pass in a single word # and it will hand back the translation for that single word # then we add the translation/conversion to our accumulator and add a space at the end # to separate each word result = result + conversion + ' ' return result def word( string, dictionary = DEFAULT_DICTIONARY) : ''' returns a translation of the string according to dictionary, where string is composed of a single word. if the translation is unknown the function return the word within <> translate a single word from our dictionary ''' # if string/word is in the dictionary, then hand back the translation if ( string in dictionary ): result = dictionary.get( string ) # get the translation of the string # otherwise, put <> around the original word else: result = '<' + string + '>' return result if ( __name__ == '__main__' ) : translation = word( 'lapiz' ) print( translation ) print() translation = line( 'como esta eso' ) print( translation ) print() translation = passage( 'tun vous savez hvor\nmi pencil ist' ) print( translation ) print() DIGIT_URL = 'http://www.cs.virginia.edu/~cs1112/words/digits' d = {'0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine'} translation = passage( '0 O 1 2 3 4 5', dictionary = d ) print( translation ) print() text = ''' dubailte dubailte kesusahan und guaio umlilo adolebitque und ketel bombolla umucu di una pantanoso neidr dans der ketel bouyi und cuire oog di tritons und kaki di rano yun di fledermoyz und lingua di chien viperae foarke und blyn cuc stik moo fotur und ovlet tis pre eng viehatys di voimakas guaio mag un inferno salda bouyi und bombolla ''' translation = passage( text ) print( translation )