1

I have the following example sentence

text_to_translate1=" I want to go for swimming however the 天气 (weather) is not good"

As you can see there exist two languages in the sentence (i.e, English and Chinese).

I want a translator. The result that I want is the following:

I want to go for swimming however the weather(weather) is not good

I used the deepl translator but cannot autodetect two languages in one.

Code that I follow:

import deepl

auth_key = "xxxxx"
translator = deepl.Translator(auth_key)

result = translator.translate_text(text_to_translate1, target_lang="EN-US")
print(result.text)  
print(result.detected_source_lang) 

The result is the following:

I want to go for swimming however the 天气 (weather) is not good
EN

Any ideas?

xavi
  • 80
  • 1
  • 12
  • Could you send a link to the translator you're using, or a larger snippet of code that explains how you implement it? Also, do you mean the [DeepL Translator](https://www.deepl.com//translator)? – Xiddoc Jul 19 '22 at 07:55
  • yes, @Xiddoc thank you. I make some additions – xavi Jul 19 '22 at 13:36

1 Answers1

2

I do not have access to the DeepL Translator, but according to their API you can supply a source language as a parameter:

result = translator.translate_text(text_to_translate1, source_lang="ZH", target_lang="EN-US")

If this doesn't work, my next best idea (if your target language is only English) is to send only the words that aren't English, then translate them in place. You can do this by looping over all words, checking if they are English, and if not, then translating them. You can then merge all the words together.

I would do it like so:

text_to_translate1 = "I want to go for swimming however the 天气 (weather) is not good"

new_sentence = []
# Split the sentence into words
for word in text_to_translate1.split(" "):
    # For each word, check if it is English
    if not word.isascii():
        new_sentence.append(translator.translate_text(word, target_lang="EN-US").text)
    else:
        new_sentence.append(word)

# Merge the sentence back together
translated_sentence = " ".join(new_sentence)

print(translated_sentence)
Xiddoc
  • 3,369
  • 3
  • 11
  • 37
  • thank you the second option works fine. Can you please explain how the .text works? – xavi Jul 19 '22 at 19:14
  • @xavi I took it from your code example, where you run `print(result.text)`. If my answer is valid, please mark it with the checkmark on the left :) – Xiddoc Jul 20 '22 at 11:29
  • 1
    @xavi The deepl api returns an object of ``. After the API call, the returned object has the following attributes: `['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'detected_source_lang', 'text']`. Calling `.text` on the object gets the returned translated text. – Thomas Jul 24 '22 at 16:59