4

textblobWindows で Python 2.7.10 をかなり長い間使用してきましたが、予期せず動作しなくなりました。2 つの独立した仮想マシンと 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 のソース コードでは、code はjsonエンコードされたアプローチを示していますが、どうやら Google はここで単純な方が複雑なよりも優れていると判断したようです。

この問題は、 https://github.com/sloria/TextBlob/issues/117で既に言及されています。

于 2016-02-15T23:38:44.340 に答える
3

@Gijs が述べたように、Google Translate 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

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

于 2020-10-11T10:30:30.453 に答える