简体   繁体   中英

TextBlob translator cannot detect the different language in dataframe

I run the language translator using TextBlob. It can translate from a string. However, I tried to loop the textblob translator for the data in a dataframe which in dataframe might have a mixed of different languages (en and es).

The code I used is:

for content in data:
  blob = TextBlob(content)

for i in data:
  blob = TextBlob(i)

blob.translate(from_lang = 'en', to = 'es')

The error is:

    83             result = result.encode('utf-8')
    84         if result.strip() == source.strip():
---> 85             raise NotTranslated('Translation API returned the input string unchanged.')
    86 
    87     def _request(self, url, host=None, type_=None, data=None):

NotTranslated: Translation API returned the input string unchanged.

As it is not necessary in each case that 'en' and 'es' must be different. There can be many cases in which 'es' and 'en' have the same text. So the error is being raised in the case where both of them are same. Using the try and catch statement will tackle all the cases having same texts eventually making your code to work.

for content in data:
  blob = TextBlob(content)

for i in data:
  blob = TextBlob(i)
  try:
     print (blob.translate(from_lang = 'en', to = 'es'))
  except:
     print ("Same translation so skipping")


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM