''' 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 ) # {k:v, k:v} (each line in the babel file ^^ in DEFAULT_URL in that link is now a k:v pair) def glot( text, dictionary = DEFAULT_DICTIONARY) : ''' returns a translation of text according to dictionary, where text may have embedded \n's ''' print( dictionary ) # text is one string of multiple lines of text lines = text.split('\n') # Split on the newline character so that you split the giant string # of multiple lines text_translation = '' # We want to build up a string of the translation of the text for each_line in lines: # Go through each line (a string of words) line_translation = line( each_line, dictionary ) # call the line() function on each_line # Use the right dictionary! # DEFAULT or the argument! # Add each translated line into our text_translation followed by a newline character # to go to the next line after for the next line to translate! text_translation = text_translation + line_translation + '\n' return text_translation # return the result of the accumulator (string we built up of the # translation) def line( string, dictionary = DEFAULT_DICTIONARY ) : ''' returns a translation of string according to dictionary, where string is composed of zero or more words ''' print( dictionary ) # string is now a line of text (string of words) words = string.split() # get a list of words (similar to how you split any other string) line_translation = '' # Build up a string of the translated line! for each_word in words: # Loop through each word in our words list word_translation = word( each_word , dictionary ) # Call the word() function on each_word line_translation = line_translation + word_translation + ' ' # Add each translated word # to our line_translation # each word separated by a space return line_translation # We want to start small scale by translating each word in function word(), # translating each line in function line(), # and then translating multiple lines in function glot()! # glot() uses line() to translate each line, line() uses word() to translate each word in # the line, and word() just translates 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 <> ''' print( dictionary ) # string is a single word if ( string in dictionary ): # If the word is in the dictionary word_translate = dictionary.get( string ) # or dictionary[string] else: # if we can't translate it we want to have the translation be word_translate = '<' + string + '>' return word_translate # Hands back the right translation of the word # Testing everything in the same file! if ( __name__ == '__main__' ) : print( word( 'lapiz' ) ) print() print( line( 'como esta eso' ) ) print() # Previously by default glot used the babel dictionary (the word translations one) print( glot( 'tun vous savez hvor\nmi pencil ist' ) ) print() DIGIT_URL = 'http://www.cs.virginia.edu/~cs1112/words/digits' d = url.get_dictionary( DIGIT_URL ) # Now we use a different dictionary! # Url.py help: # Make sure url.py is in the same folder as this file! # Website / Modules / url / link "here" is where the most recent version of url.py is! print( glot( '0 O 1 2 3 4 5', dictionary = d ) , ) # The dictionary online is integer to strings so if you want this to work # just change the line in get_dictionary() in url.py that calls get_dataset to # get_raw_dataset