4

textblob在 Windows 上使用 Python 2.7.10 已经有一段时间了,出乎意料的是,它停止了工作。使用两个独立的虚拟机以及在 OS X 上进行测试会产生相同的错误。

测试文档中的一个简单片段:

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

产生错误:

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.

如何调试此错误?

4

4 回答 4

4

如文档中所述,Textblob 使用Google Translate API进行翻译。

显然,这个(未记录的)API 改变了它的输出格式。我可以使用此代码段成功请求:

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"'

在 textblob 的源代码中,代码表示一种json编码方法,但显然谷歌在这里决定简单确实比复杂好。

这个问题已经在https://github.com/sloria/TextBlob/issues/117中提到过。

于 2016-02-15T23:38:44.340 回答
3

正如@Gijs 所提到的,谷歌翻译 API 发生了变化。这导致 TextBlob 的翻译和语言检测功能停止工作。

我已经提交了PR来解决这个问题。

于 2016-02-16T06:22:35.480 回答
0

您只需要设置from_lang参数,告诉您正在翻译哪种语言:

en_blob = TextBlob(u'Simple is better than complex.')
print(en_blob.translate(from_lang='en', to='es'))
于 2018-10-08T16:07:53.443 回答
0

根据我的经验,引入from_lang参数并不能解决问题。我已经修复了它从另一个方面调用 Google 的翻译 API,而不是通过 textblob。 https://github.com/ssut/py-googletrans

于 2020-10-11T10:30:30.453 回答