Translation Using Dictionaries (Python)
Question: Alright so my teacher gave me the assignment below, and we have only been
working with dictionaries for about a day:

1) Create a dictionary to translate a sentence from one language to another
(such as Spanish to English). 2) The program should then write a sentence in
the first language, then in the second language.

So far, my code is this:

    
    
    def userinput():
        inputsentence = input("What sentence would you like to translate?(Hint! Make the sentence I speak _ more than my friends")
        spanoreng = input("Is this sentence Spanish or English?(Please enter span or eng")
        return(inputsentence,spanoreng)
    
    def spantoeng(spantoengtras,inputsentence):
        inputsentence.lower()
        inputsentence.split()
        print(inputsentence)
    def engtospan(engtospantrans,inputsentence):
        inputsentence.lower()
        inputsentence.split()
        print(inputsentence)
    def main():
        spantoengtrans = {'yo' : 'I', 'hablo' : 'speak', 'espanol' : 'spanish', 'ingles' : 'english', 'mas' : 'more', 'de' : 'than','mis' : 'my', 'amigos' : 'friends'}
        engtospantrans = {'I' : 'yo', 'speak' : 'hablo', 'spanish' : 'espanol', 'english' : 'ingles', 'more' : 'mas', 'than' : 'de','my' : 'mis', 'friends' : 'amigos'}
        (inputsentence,spanoreng) = userinput()
        if spanoreng == 'span':
            spantoeng(spantoengtrans,inputsentence)
        elif spanoreng == 'eng':
            engtospan(engtospantrans,inputsentence)
        else:
            print("please type span or eng")
            (inputsentence,spanoreng) = userinput()
    
    
    main()
    

All I'm having trouble with is changing the list using the dictionary. Also,
my .lower() seems to not be doing anything. help?

EDIT: realized my mistake. thanks.


Answer: I don't see **any** list -- I only see strings, which are immutable, and no
use of the dictionary.

So the core function could be:

    
    
    def translate(sentence, transdict):
        words = sentence.split()
        trans = [transdict.get(w.lower(),w) for w in words]
        print(' '.join(trans))
    

`split` makes the sentence into a list of whitespace-separated words; then the
list comprehension makes it into a list of translated words (leaving words
alone if they're not in the dictionary); finally we join the latter list back
into a space-separated sentence.

Of course this leaves a lot to be desired, but it's hard to do better without
regular expressions -- and if you've been using dictionaries for just a day
regular expressions may be well beyond your studies so far. In case they're
not:

    
    
    import re
    
    def maketrans(somedict):
        def trans(mo):
            word = mo.group()
            return somedict.get(word.lower(), word)
        return trans
    

and then

    
    
    translated = re.sub(r'\w+', maketrans(right_dict), sentence)
    print (translated)
    

will preserve punctuation and spacing. But what between RE's and higher order
functions, I suspect you'd better ignore this one until later in your Python
studies:-).

