How can I easily machine translate something with python?

googletrans and NLTK are great libraries to do any translation of language processing

from nltk import sent_tokenize

from googletrans import Translator

translator = Translator()

data = "All work and no play makes jack dull boy. All work and no play 
makes jack a dull boy."

token = sent_tokenize(data)

for tt in token:
    translatedText = translator.translate(tt, dest="ko")
    print(translatedText.text)

Result:

모든 일과 놀이는 잭 둔한 소년을 만든다.

모든 일과 놀이는 잭을 둔한 소년으로 만든다.


Goslate is a good library for this that uses Google Translate: http://pythonhosted.org/goslate/

Here's the example from the docs:

>>> import goslate
>>> gs = goslate.Goslate()
>>> print(gs.translate('hello world', 'de'))
hallo welt

In order to go from "carpe diem" to "seize the day":

>>> print(gs.translate('carpe diem', 'en', 'la'))
seize the day

So it's essentially the same as the Babelfish API used to be, but the order of the target and source languages is switched. And one more thing -- if you need to figure out the short code, gs.get_languages() will give you a dictionary of all the short codes for each supported language: {...'la':'Latin'...}

Tags:

Python

Nltk