我正在尝试通过textblob将非英语文本翻译成英语。我阅读了文档并尝试处理可能的异常,如下所示:
txt=" "
for word in text.split():
try:
w=TextBlob(word)
w=w.translate(to='en')
except TranslatorError(TextBlobError):
word=" " #replace word with space
txt=txt+word
except NotTranslated(TextBlobError):
txt=txt+word+" "
else:
txt=txt+w+" "
print(txt)
我遇到以下错误:
except TranslatorError(TextBlobError):
NameError: name 'TranslatorError' is not defined
raise NotTranslated('Translation API returned the input string unchanged.')
textblob.exceptions.NotTranslated: Translation API returned the input string unchanged.
我提到了以下链接: https://textblob.readthedocs.io/en/dev/api_reference.html#textblob.exceptions.TextBlobError
我无法解决这些错误。请帮忙!
答案 0 :(得分:0)
这对我有用:
en_blob = TextBlob(“简单比复杂要好。”) 打印(en_blob.translate(from_lang ='en',to ='es'))
明确说明“ from_lang”。
答案 1 :(得分:0)
这只是因为两种语言中的某些单词相同。如果我们查看TextBlob的文档,它将如何引发异常:
首先它翻译单词,所以您调用 translate 方法,它看起来像:
def translate(self, source, from_lang='auto', to_lang='en', host=None, type_=None):
"""Translate the source text from one language to another."""
if PY2:
source = source.encode('utf-8')
data = {"q": source}
url = u'{url}&sl={from_lang}&tl={to_lang}&hl={to_lang}&tk={tk}'.format(
url=self.url,
from_lang=from_lang,
to_lang=to_lang,
tk=_calculate_tk(source),
)
response = self._request(url, host=host, type_=type_, data=data)
result = json.loads(response)
if isinstance(result, list):
try:
result = result[0] # ignore detected language
except IndexError:
pass
self._validate_translation(source, result)
return result
当您看到第二行时,它会尝试验证翻译。如果翻译后的和来源相同,则抛出异常
def _validate_translation(self, source, result):
"""Validate API returned expected schema, and that the translated text
is different than the original string.
"""
if not result:
raise NotTranslated('Translation API returned and empty response.')
if PY2:
result = result.encode('utf-8')
if result.strip() == source.strip():
raise NotTranslated('Translation API returned the input string unchanged.')
例如,如果您尝试将英语中的“ Plauen ”翻译成德语,这是因为 Plauen 这个词在英语和德语中是相同的。
您可以简单地使用Python中的try和except块来逃避此类异常。
我希望这会有所帮助。
答案 2 :(得分:0)
错误处理的方式不对,我使用了下面的代码并且它有效,希望这有帮助。
import textblob.exceptions
txt=" "
for word in text.split():
try:
w=TextBlob(word)
w=w.translate(to='en')
except textblob.exceptions.TranslatorError:
word=" " #replace word with space
txt=txt+word
except textblob.exceptions.NotTranslated:
txt=txt+word+" "
else:
txt=txt+w+" "
print(txt)