4

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?

saladi
  • 3,103
  • 6
  • 36
  • 61
user145078
  • 41
  • 1
  • 4

4 Answers4

4

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.

Gijs
  • 10,346
  • 5
  • 27
  • 38
  • 1
    I am facing the same problem with but with Language Detection not translation. Can you please help me in understanding how can I do the same with the above format. – Harsh Patni Feb 16 '16 at 08:46
3

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.

jschnurr
  • 1,181
  • 6
  • 8
0

You just need to set the from_lang parameter telling from which language you are translating:

en_blob = TextBlob(u'Simple is better than complex.')
print(en_blob.translate(from_lang='en', to='es'))
pushkin
  • 9,575
  • 15
  • 51
  • 95
0

Introducing the from_lang parameter does not solve the issue, in my experience. I have fixed it calling Google's translation API from another front, not through textblob's. https://github.com/ssut/py-googletrans

RiGonz
  • 461
  • 4
  • 8