Boberto
Boberto

Reputation: 57

How to make a language translator in python?

I'm making a small dictionary in python, and am now stuck on part where I want to translate a whole sentence. I have a dictionary set up with few words

english_italian = {"hey": "ciao", "my": "mio", "name": "nome"}

How do i translate a sentence like "hello my name is Mario" so the output would be like

ciao mio nome is Mario

So the words that are not in the dictionary would print out like they were written and words that can be found in the dictionary will be translated.

Upvotes: 1

Views: 1319

Answers (4)

CPUFry
CPUFry

Reputation: 576

A problem you'll have to tackle first is translating a sentence as a string, to an array of words (which could be a question by itself). Once you have this array of words (let's call it words), loop through the words and translate each word to its Italian counterpart. Ofcourse, just pasting the translated words together will form a sentence different than the original one (as you'll ignore punctuation, spaces, ...). e.g. "hey, my name is mario" will translate to "ciao mio nome is Mario" (mention the missing comma).

To solve this, you could replace each translated word in the original sentence, to its Italian counterpart, which also retains the original words not in the translation. This produces the following code:

english_italian = {"hey": "ciao", "my": "mio", "name": "nome"}
sentence = "ciao mio nome is Mario"
words = sentence_to_words(sentence) # ["ciao", "mio", "nome", "is", "Mario"]

for word in words:
    if word in english_italian:
        sentence = sentence.replace(word, english_italian[word])

An optimalization could be to first remove duplicates from the words array, so you don't translate the same word more than once.

All and all, translating like this will not work that well (think about verb conjugation, different order of parts in the sentence such as the subject and verbs, grammatical differences, etc.).

Upvotes: 1

ArK
ArK

Reputation: 17

You could try the following:

english_italian = {"hey": "ciao", "my": "mio", "name": "nome"}

sentence = "hey my name is Mario"

sentence_list = sentence.split(" ")

# We get this: ['hey', 'my', 'name', 'is', 'Mario']

translation_list = [english_italian[word] if word in english_italian.keys() else word for word in sentence_list]

trans_sentence = " ".join(translation_list)

print(trans_sentence)

Output

ciao mio nome is Mario

We take the sentence, split it up into words, translate each word if it is in the dictionary, else we keep the word. Then we join the list of translated words and print it.

Hope it helps.

Upvotes: 0

Nick Skywalker
Nick Skywalker

Reputation: 1087

If you want to leave words not in the dictionnary untranslated:

english_italian = {"hey": "ciao", "my": "mio", "name": "nome"}
s = "hello my name is Mario"

translated = ' '.join([
    english_italian[english_word] 
    if english_word in english_italian else english_word
    for english_word in s.split(' ')])

print(translated)

Output:

hello mio nome is Mario

Upvotes: 2

Dan
Dan

Reputation: 1587

english_italian = {"hey": "ciao", "my": "mio", "name": "nome"}

s = "hey my name is Mario"

# split s into a list of words, and search the dict for each. 
# if the word is not found, keep the original word
translated = " ".join(english_italian.get(word, word) for word in s.split(" "))

print(translated)

Output:

'ciao mio nome is Mario'

Upvotes: 2

Related Questions