Ad

Dictionary Looping

- 1 answer

As you will understand from my question, I am new to programming and as most people who are new to this, I am having issues comprehending some of the code. I came across the following code in Python which translates some words from German to English and returns a string of the translation:

deu2eng = {
'ich':'I',
'gehe':'go',
'nach':'to',
'die':'the',
'kirche':'church'}

def trans(eng2deu):
    translation = ''
    for word in eng2deu.split():
        if word in deu2eng:
            translation+= deu2eng[word] + ' '
        else:
            translation += word + ' '
    return translation

When I run the following it works: trans('ich gehe nach die kirche')

However there are some issues. I can't get to understand how I can get it to be case insensitive (e.g. if I type Kirche instead of kirche, it will return Kirche instead of church). I tried using the lower() method to no avail. But most importantly, I do not understand the following line and why it works:

translation+= deu2eng[word] + ' '

From what you can infer, this is quite basic, but I would like to fully comprehend what is happening with this code as I am stuck in the 2 aforementioned things.

Ad

Answer

It always helps to put in comments to read through new code specially if you're new to programming.

# Initializes your basic "translation" for each word, i.e. ich = I
# Notice that ich is in all lower case letters, which affects your matching in your trans function
deu2eng = {
'ich':'I',
'gehe':'go',
'nach':'to',
'die':'the',
'kirche':'church'}

def trans(eng2deu):
    translation = ''
    # Start of your for loop, this does 2 things:
    # (1) Splits up your sentence using the space character, i.e. ich gehe nach die kirche => [ich gehe, nach, die, kirche]
    # (2) Loops through your split up sentence word per word
    for word in eng2deu.split():
        # Now we check if the word is in deu2eng,
        # i.e. is ich a key in deu2eng (a key is case sensitive)
        if word in deu2eng:
            # If we find the key, then we get the corresponding value of our word, i.e. ich = I, and then append it to our translation variable.
            translation += deu2eng[word] + ' '
        else:
            # If not, then we use the original word
            translation += word + ' '
    return translation

Now notice your if statement if word in deu2eng which we mention checks if a specific key to a value in deu2eng, we also mention that the check itself is case sensitive which if it fails defaults to your else case. So to fix your issues with it not matching correctly, we simply change the word to its lower case value using lower():

key = word.lower()
if key in deu2eng:
    translation+= deu2eng[key] + ' '
else:
    translation += word + ' '

Although you could simplify that by just using:

translations += deu2eng.get(word.lower(), word) + ' '

Always good to have a read through the python documentation as well, here are some references to what you're looking for:

dict

split

Ad
source: stackoverflow.com
Ad