[英]Python: Using TextBlob NLTK to read a text file and detect the language
[英]TextBlob translator cannot detect the different language in dataframe
我使用 TextBlob 运行语言翻译器。 它可以从字符串翻译。 但是,我尝试循环文本块翻译器以获取 dataframe 中的数据,在 dataframe 中可能混合了不同的语言(en 和 es)。
我使用的代码是:
for content in data:
blob = TextBlob(content)
for i in data:
blob = TextBlob(i)
blob.translate(from_lang = 'en', to = 'es')
错误是:
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.
因为没有必要在每种情况下'en'和'es'必须不同。 'es' 和 'en' 有很多相同的文本。 因此,在两者相同的情况下会引发错误。 使用 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.