16

You used to be able to use nltk.misc.babelfish to translate things, but the Yahoo Babelfish API went down. Is there an easy way I can, say, do this?

>>> import translate
>>> translate('carpe diem', 'latin', 'english')

'seize the day' 
Delimitry
  • 2,987
  • 4
  • 30
  • 39
Jonathan
  • 10,571
  • 13
  • 67
  • 103

3 Answers3

15

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'...}

G Brown
  • 754
  • 8
  • 15
  • Awesome, thanks. Although it looks like it worked the first two times I tried it, and now only gives "HTTPError: HTTP Error 503: Service Unavailable." Did I get banned or something? – Jonathan Dec 20 '15 at 16:28
  • 1
    Likely that Google is throttling...pretty low limits for the translate API. See this answer for a possible solution: http://stackoverflow.com/a/33448911/5224214 – G Brown Dec 20 '15 at 16:31
  • 1
    Goslate no longer works. From the library homepage: "Google has updated its translation service recently with a ticket mechanism to prevent simple crawler program like goslate from accessing. Though a more sophisticated crawler may still work technically, however it would have crossed the fine line between using the service and breaking the service. goslate will not be updated to break google’s ticket mechanism. Free lunch is over. Thanks for using." – malfroid May 26 '20 at 13:18
4

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:

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

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

2

You can use the python translate library. Install with pip:

$ pip install translate

Code example:

In [1]: from translate import Translator
In [2]: translator= Translator(to_lang="zh")
In [3]: translation = translator.translate("This is a pen.")
Out [3]: 这是一支笔
malfroid
  • 139
  • 2
  • 9