简体   繁体   English

TextBlob 翻译器无法检测到 dataframe 中的不同语言

[英]TextBlob translator cannot detect the different language in dataframe

I run the language translator using TextBlob.我使用 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).但是,我尝试循环文本块翻译器以获取 dataframe 中的数据,在 dataframe 中可能混合了不同的语言(en 和 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.因为没有必要在每种情况下'en'和'es'必须不同。 There can be many cases in which 'es' and 'en' have the same text. 'es' 和 'en' 有很多相同的文本。 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.使用 try 和 catch 语句将解决所有具有相同文本的情况,最终使您的代码正常工作。

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


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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