【发布时间】:2021-10-03 11:39:42
【问题描述】:
我正在尝试使用 Google Cloud Translation API 翻译包含多种语言文本的 excel(或 csv)文档,而我的目标语言是英语。
我想使用“批量翻译文本(仅限高级版)”代码示例(此处链接:https://cloud.google.com/translate/docs/samples/translate-v3-batch-translate-text)但代码示例中有一行定义源语言,因此只能有一种源语言.
但我需要先检测文档中的语言,然后将文本翻译成英文。有用于检测文本“检测语言(高级)”(链接:https://cloud.google.com/translate/docs/advanced/detecting-language-v3)的简单字符串中的语言的代码示例,但我需要将第一个翻译文档的代码示例(但只定义了一种源语言)与检测语言而不是定义一种源语言的能力。
资源中有这种类型的代码示例吗?怎么解决?
这里是有问题的示例代码:
from google.cloud import translate
def batch_translate_text(
input_uri="gs://YOUR_BUCKET_ID/path/to/your/file.txt",
output_uri="gs://YOUR_BUCKET_ID/path/to/save/results/",
project_id="YOUR_PROJECT_ID",
timeout=180,
):
"""Translates a batch of texts on GCS and stores the result in a GCS location."""
client = translate.TranslationServiceClient()
location = "us-central1"
# Supported file types: https://cloud.google.com/translate/docs/supported-formats
gcs_source = {"input_uri": input_uri}
input_configs_element = {
"gcs_source": gcs_source,
"mime_type": "text/plain", # Can be "text/plain" or "text/html".
}
gcs_destination = {"output_uri_prefix": output_uri}
output_config = {"gcs_destination": gcs_destination}
parent = f"projects/{project_id}/locations/{location}"
# Supported language codes: https://cloud.google.com/translate/docs/language
operation = client.batch_translate_text(
request={
"parent": parent,
"source_language_code": "en",
"target_language_codes": ["ja"], # Up to 10 language codes here.
"input_configs": [input_configs_element],
"output_config": output_config,
}
)
print("Waiting for operation to complete...")
response = operation.result(timeout)
print("Total Characters: {}".format(response.total_characters))
print("Translated Characters: {}".format(response.translated_characters))
【问题讨论】:
标签: python google-cloud-platform translation google-translate google-translation-api