Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python textblob Translation API Error

I have been using textblob in Python 2.7.10 on Windows for quite some time, and unexpectedly, it stopped working. Testing with two independent virtual machines as well as on OS X produces the same error.

Testing a simple snippet from the docs:

    from textblob import TextBlob
    en_blob = TextBlob(u'Simple is better than complex.')
    print(en_blob.translate(to='es'))

produces an error:

File "test.py", line 3, in <module> print(en_blob.translate(to='es'))

File "C:\Python27\lib\site-packages\textblob\blob.py", line 509, in translate
from_lang=from_lang, to_lang=to))

File "C:\Python27\lib\site-packages\textblob\translate.py", line 45, in translate
raise NotTranslated('Translation API returned the input string unchanged.')

textblob.exceptions.NotTranslated: Translation API returned the input string 
unchanged.

How can I debug this error?

like image 315
user145078 Avatar asked Mar 13 '23 07:03

user145078


2 Answers

As mentioned by @Gijs, the Google Translate API changed. This caused TextBlob's translation and language detection functionality to stop working.

I've submitted a PR to fix the problem.

like image 29
jschnurr Avatar answered Mar 23 '23 03:03

jschnurr


As mentioned in the docs, Textblob uses the Google Translate API for its translations.

Apparently, this (undocumented) API changed it's output format. I am able to do a succesfull request with this snippet:

import requests
url = 'http://translate.google.com/translate_a/t'
params = {
    "text": "Simple is better than complex", 
    "sl": "en", 
    "tl": "es", 
    "client": "p"
}
print(requests.get(url, params=params).content)

>> '"Simple es mejor que complejo"'

In the source code of textblob, code is indicating a json encoded approach, but apparently Google has decided here that simple is indeed better than complex.

This issue is already mentioned in https://github.com/sloria/TextBlob/issues/117.

like image 86
Gijs Avatar answered Mar 23 '23 03:03

Gijs