Skip to content

Instantly share code, notes, and snippets.

@AmitMY
Last active July 13, 2019 10:45
Show Gist options
  • Save AmitMY/38e6b3bfdd2b57113129e3a76daddb68 to your computer and use it in GitHub Desktop.
Save AmitMY/38e6b3bfdd2b57113129e3a76daddb68 to your computer and use it in GitHub Desktop.
from googletrans import Translator
def translate(sentence: str, speaker: str, dest: str):
sentence = ("he" if speaker == "M" else "she") + " said: " + sentence # Preprocess = Add prefix
out = Translator().translate(sentence, dest=dest).text # Translate with prefix
return ":".join(out.split(":")[1:]) # Postprocess = Remove prefix
print(translate("I love you", speaker="M", dest="he")) # Hebrew: אני אוהב אותך
print(translate("I love you", speaker="F", dest="he")) # Hebrew: אני אוהבת אותך
print(translate("I'm smart", speaker="M", dest="fr")) # French: Je suis intelligent
print(translate("I'm smart", speaker="F", dest="fr")) # French: Je suis intelligente
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment