【发布时间】:2021-02-25 14:33:27
【问题描述】:
我的库 googletrans 有问题。所以我正在翻译数据框。我只是从 Google Cloud Storage 存储桶下载数据并将其存储在 DataFrames 中 -> 我选择一列进行翻译,然后就可以了。我用这个来翻译:
from googletrans import Translator, constants
def translator(text, language):
"""
Function that receives the text we want to translate and the chosen language specified by the user and returns
the translation
Parameters:
-----------
text:
- The string of text we want to translate
language:
- Specified by the user, into which language they want to translate
"""
translate = Translator(raise_exception=True)
# They are stored such as 'en':'english'
def dest_language(val):
for key, value in constants.LANGUAGES.items():
# all values in lower case. If not we get ValueError: invalid destination language
if value == val.lower():
return key
return 'Key Not Found'
return translate.translate(text=text, dest=dest_language(language)).text
当我尝试翻译大量样本时,问题就来了。我有 9749 个样本(DataFrame 行)要翻译,每个样本或多或少有 5k 个字符,当我尝试翻译 2000is 样本时出现错误。这是我做的代码(阈值是因为我翻译不了超过5k,不知道为什么):
# Data = All DataFrame
# tr = DataFrame with the selected column
tr = Data.loc[:, [column_translation]]
# Make a threshold to divide the text
threshold = 5000
tr.reset_index(inplace=True, drop=True)
l = []
for i in range(len(tr)):
if len(tr.loc[i,column_translation]) < threshold:
l.append(TR(text=tr.loc[i,column_translation], language="spanish"))
print(f"{i+1} out of {len(tr)}")
这是我收到 2000 个样本时遇到的错误:
异常:来自 ('translate.google.com',) 的意外状态代码“429” 和
我不明白我怎么会得到 429 错误,然后是 200 响应……你知道为什么会这样吗?我的意思是 HTTP 代码告诉我,我的 IP 因如此多的请求而被阻止,但我不明白为什么。
另外,使用googletrans和google-cloud-translate有什么区别?
【问题讨论】:
标签: google-cloud-platform google-translate google-translation-api