问题
I am trying to translate non-english texts into english via textblob. I read documentation and trying to handle possible exceptions as below:
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)
I am getting the following errors:
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.
I referred to the following link: https://textblob.readthedocs.io/en/dev/api_reference.html#textblob.exceptions.TextBlobError
I am not able to resolve these errors. Please help!
回答1:
This works for me:
en_blob = TextBlob(u'Simple is better than complex.') print(en_blob.translate(from_lang='en', to='es'))
Explicitly stating "from_lang".
回答2:
It is just because some words in both languages are the same. If we look at the documentation of TextBlob how it raises the exception:
The first it translates the word, so you call translate method, it looks like:
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
when as you can see the last second line, it tries to validates the translation. if the both translated and source are the same, then exception is thrown
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.')
As Example, if you try to translate a word of "Plauen" from English to German, this is because the word Plauen are the same in English and German.
You can escape such exception simply with try and except block in Python.
I hope this helps.
来源:https://stackoverflow.com/questions/53900541/textblob-exceptions-nottranslated-translation-api-returned-the-input-string-unc