''' Purpose: translate user text to English ''' import url DEFAULT_URL = 'http://www.cs.virginia.edu/~cs1112/words/babel' DEFAULT_DICTIONARY = url.get_dictionary( DEFAULT_URL ) # text is a string of many lines def passage( text, dictionary = DEFAULT_DICTIONARY) : ''' returns a translation of text according to dictionary, where text may have embedded \n's ''' list_lines = text.split('\n') # Split on newline character to break up the passage into a list of lines # Translate each line in the passage result = '' # We will add the translation of each line to this result for each_line in list_lines: line_translation = line( each_line, dictionary ) # Translate each line using our line() function # and the dictionary passed in (that way babel # is not always used by default if we want to use # a different dictionary) result = result + line_translation + '\n' # '\n' is a newline character (goes to next line - like # hitting enter key) return result # string is a string of a bunch of words def line( string, dictionary = DEFAULT_DICTIONARY ) : ''' returns a translation of string according to dictionary, where string is composed of zero or more words ''' list_words = string.split() # Translate each word in list of words result = '' # string accumulator where we add the translation for each word into this translation of the line for w in list_words: # w is each word in our list_words word_translation = word( w, dictionary ) # get the translation for a single word (function below) # using the dictionary passed in (otherwise it will use # babel by default and we don't always want that! as seen # in the dictionary d example below in the tests) result = result + word_translation + ' ' # add each translated word into the string result accumulator return result # return the string accumulator of translated words (translated the entire line of words) # For a normal program, Python goes from top to bottom. # However, for a module, Python will see which functions are defined and can use the # function definitions to run programs / code that utilizes these functions. # So the bottom part if __name__ == '__main__' part is essentially the "runnable" part # that we're worried about in terms of running it top to bottom. # Python scans and sees all the functions! # Uses optional parameter dictionary (DEFAULT_DICTIONARY is used if we don't provide # an argument for an alternate dictionary) # string is a single word 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 <> ''' # If the word is in the dictionary, we can get the translation of the word if ( string in dictionary ): # if string is a key in our dictionary result = dictionary.get( string ) # Gets the value for the key string (the word we want to translate) # If the word is not in the dictionary, we can't get the translation so we do else: result = '<' + string + '>' # We return the word encased in <> (unknown word) return result if ( __name__ == '__main__' ) : translation = word( 'lapiz' ) # Translates a single word using our babel dictionary print( translation ) print() # 'pencil' (value for the key 'lapiz' in our babel dictionary) translation = line( 'como esta eso' ) # Translates a line of words using the word() function on each word print( translation ) # in the line print() # 'how is that' translation = passage( 'tun vous savez hvor\nmi pencil ist' ) print( translation ) print() DIGIT_URL = 'http://www.cs.virginia.edu/~cs1112/words/digits' # 'O' is not a key in our dictionary so that cannot be translated but everything else can! :) d = {'0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine'} # Pass in an argument to optional parameter dictionary so now we specify that we are # not using the babel dictionary but rather the dictionary d defined on line 81. translation = line( '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 )