[英]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.