JavaJawa
JavaJawa

Reputation: 23

Python translator: I want to translate multiple words in sentence

So I've begun working on this little translator program that translates English to German with an input. However, when I enter more than one word I get the words I've entered, followed by the correct translation.

This is what I have so far:

data = [input()]

dictionary = {'i':'ich', 'am':'bin', 'a':'ein', 'student':'schueler', 'of 
the':'der', 'german':'deutschen', 'language': 'sprache'}


from itertools import takewhile
def find_suffix(s):
    return ''.join(takewhile(str.isalpha, s[::-1]))[::-1]

for d in data:
    sfx = find_suffix(d)
    print (d.replace(sfx, dictionary.get(sfx, sfx)))

I'm trying to get the following output:

"i am a student of the german sprache" 

as opposed to:

"ich bin ein schueler der deutschen spracher"

I'm quite new to python so any help would be greatly appreciated

Upvotes: 1

Views: 2774

Answers (3)

Mohd
Mohd

Reputation: 5613

I have noticed two things in your input. First thing is that you can have two words to be translated into one (two word key in dictionary) and the other thing is that input can have german words that shouldn't be translated. Having those two conditions I think the best approach is to split() the input and loop through to check the words. Follow the comments in the code below:

dictionary = {'i': 'ich', 'am': 'bin', 'a': 'ein', 'student': 'schueler', 'of the': 'der', 'german': 'deutschen', 'language': 'sprache'}
data = "i am a student of the german sprache"
lst = data.split()
result = ''
i = 0
while i < len(lst):
    # try/except to see if the key is one word or two words
    try:
        if lst[i] in dictionary.values():  # Check if the word is german
            result += lst[i] + ' '
            i += 1
        else:
            result += dictionary[lst[i]] + ' '  # get the word from the dictionary
            i += 1
    except KeyError:
        result += dictionary[lst[i] + ' ' + lst[i+1]] + ' '  # if the word is not german and not in dictionary, add the 2nd word and get from dictionary
        i += 2
print result

output:

ich bin ein schueler der deutschen sprache 

This will also fail if you have a 3 word key for example, but if you only have two words max then it should be fine.

Upvotes: 0

themistoklik
themistoklik

Reputation: 880

Changing your code to this should provide a first step to what you're looking for.

data = raw_input()

dictionary = {'i':'ich', 'am':'bin', 'a':'ein', 'student':'schueler', 'of':'der', 'german':'deutschen', 'language': 'sprache'}



from itertools import takewhile
def find_suffix(s):
    return ''.join(takewhile(str.isalpha, s[::-1]))[::-1]


for d in data.split():
    sfx = find_suffix(d)
    print (d.replace(sfx, dictionary.get(sfx,''))),

What you have right now does not take every separate word into consideration as data is not a list of words as you intended but a list holding one string, the input you provided. Try print-debugging your snippet to see what I am talking about.

Notice that with such logic corner cases in your project appear. Taking each word and translating it with its German counterpart prohibits dictionary entries longer than 1 word, such as 'of the':'der'. For demo purposes I chose to keep a dictionary with keys of length 1, so the above key:value pair becomes 'of':'der' which is not correct, as German grammar is a little more complicated than that.

You now have more problems than what you started with, which is what toy projects are for. If I was you, I'd look into how open source projects deal with such cases and try to see what fits. Good luck with your project.

Upvotes: 2

Saurabh kukade
Saurabh kukade

Reputation: 1634

data = [input()]

dictionary = {'i':'ich', 'am':'bin', 'a':'ein', 'student':'schueler', 'of the':'der', 'german':'deutschen', 'language': 'sprache'}

for word in data:
    if word in dictionary:
        print dictionary[word],

Explanation:

for every word in your input if that word in present in your dictionary It will print the value associated with that word and comma (,) is to skip newline character.

Upvotes: 1

Related Questions