Google Cloud Translate 算子¶
前提任务¶
要使用这些算子,您必须完成以下事项
使用 Cloud Console 选择或创建一个 Cloud Platform 项目。
为您的项目启用结算功能,具体请参阅 Google Cloud 文档。
启用 API,具体请参阅 Cloud Console 文档。
通过 pip 安装 API 库。
pip install 'apache-airflow[google]'有关详细信息,请参阅 安装。
CloudTranslateTextOperator¶
翻译一个字符串或字符串列表。
有关参数定义,请参阅 CloudTranslateTextOperator
使用算子¶
算子的基本用法
tests/system/google/cloud/translate/example_translate.py
product_set_create = CloudTranslateTextOperator(
task_id="translate",
values=["zażółć gęślą jaźń"],
target_language="en",
format_="text",
source_language=None,
model="base",
)
翻译结果可作为字典或字典数组访问,通过 Airflow 常用的 XCom 机制获取
tests/system/google/cloud/translate/example_translate.py
translation_access = BashOperator(
task_id="access", bash_command="echo '{{ task_instance.xcom_pull(\"translate\") }}'"
)
模板¶
template_fields: Sequence[str] = (
"values",
"target_language",
"format_",
"source_language",
"model",
"gcp_conn_id",
"impersonation_chain",
)
TranslateTextOperator¶
翻译一个或多个文本(或 HTML)项的数组。适用于适量文本数据,对于大量数据请使用 TranslateTextBatchOperator
有关参数定义,请参阅 TranslateTextOperator
使用算子¶
算子的基本用法
tests/system/google/cloud/translate/example_translate_text.py
translate_text = TranslateTextOperator(
task_id="translate_v3_op",
contents=["Ciao mondo!", "Mi puoi prendere una tazza di caffè, per favore?"],
source_language_code="it",
target_language_code="en",
)
TranslateTextBatchOperator¶
在单次运行中将大量文本数据翻译成最多 10 种目标语言。文件列表和其他选项由输入配置提供。
有关参数定义,请参阅 TranslateTextBatchOperator
TranslateCreateDatasetOperator¶
使用 Cloud Translate API (Advanced V3) 创建本地翻译数据集。
有关参数定义,请参阅 TranslateCreateDatasetOperator
使用算子¶
算子的基本用法
tests/system/google/cloud/translate/example_translate_dataset.py
create_dataset_op = TranslateCreateDatasetOperator(
task_id="translate_v3_ds_create",
dataset=DATASET,
project_id=PROJECT_ID,
location=REGION,
)
TranslateImportDataOperator¶
使用 Cloud Translate API (Advanced V3) 将数据导入现有本地数据集。
有关参数定义,请参阅 TranslateImportDataOperator
使用算子¶
算子的基本用法
tests/system/google/cloud/translate/example_translate_dataset.py
import_ds_data_op = TranslateImportDataOperator(
task_id="translate_v3_ds_import_data",
dataset_id=create_dataset_op.output["dataset_id"],
input_config={
"input_files": [{"usage": "UNASSIGNED", "gcs_source": {"input_uri": DATASET_DATA_PATH}}]
},
project_id=PROJECT_ID,
location=REGION,
)
TranslateDatasetsListOperator¶
使用 Cloud Translate API (Advanced V3) 获取翻译数据集列表。
有关参数定义,请参阅 TranslateDatasetsListOperator
使用算子¶
算子的基本用法
tests/system/google/cloud/translate/example_translate_dataset.py
list_datasets_op = TranslateDatasetsListOperator(
task_id="translate_v3_list_ds",
project_id=PROJECT_ID,
location=REGION,
)
TranslateDeleteDatasetOperator¶
使用 Cloud Translate API (Advanced V3) 删除本地翻译数据集。
有关参数定义,请参阅 TranslateDeleteDatasetOperator
使用算子¶
算子的基本用法
tests/system/google/cloud/translate/example_translate_dataset.py
delete_ds_op = TranslateDeleteDatasetOperator(
task_id="translate_v3_ds_delete",
dataset_id=create_dataset_op.output["dataset_id"],
project_id=PROJECT_ID,
location=REGION,
)
TranslateCreateModelOperator¶
使用 Cloud Translate API (Advanced V3) 创建本地翻译模型。
有关参数定义,请参阅 TranslateCreateModelOperator
使用算子¶
算子的基本用法
tests/system/google/cloud/translate/example_translate_model.py
create_model = TranslateCreateModelOperator(
task_id="translate_v3_model_create",
display_name=f"native_model_{ENV_ID}"[:32].replace("-", "_"),
dataset_id=create_dataset_op.output["dataset_id"],
project_id=PROJECT_ID,
location=REGION,
)
TranslateModelsListOperator¶
使用 Cloud Translate API (Advanced V3) 获取本地翻译模型列表。
有关参数定义,请参阅 TranslateModelsListOperator
使用算子¶
算子的基本用法
tests/system/google/cloud/translate/example_translate_model.py
list_models = TranslateModelsListOperator(
task_id="translate_v3_list_models",
project_id=PROJECT_ID,
location=REGION,
)
TranslateDeleteModelOperator¶
使用 Cloud Translate API (Advanced V3) 删除本地翻译模型。
有关参数定义,请参阅 TranslateDeleteModelOperator
使用算子¶
算子的基本用法
tests/system/google/cloud/translate/example_translate_model.py
delete_model = TranslateDeleteModelOperator(
task_id="translate_v3_automl_delete_model",
model_id=model_id,
project_id=PROJECT_ID,
location=REGION,
)
TranslateDocumentOperator¶
使用 Cloud Translate API (Advanced V3) 翻译文档。
有关参数定义,请参阅 TranslateDocumentOperator
使用算子¶
算子的基本用法
tests/system/google/cloud/translate/example_translate_document.py
translate_document = TranslateDocumentOperator(
task_id="translate_document_op",
project_id=PROJECT_ID,
location=REGION,
source_language_code="en",
target_language_code="uk",
document_input_config=DOC_TRANSLATE_INPUT,
document_output_config=GCS_OUTPUT_DST,
)
TranslateDocumentBatchOperator¶
根据给定的输入配置,使用 Cloud Translate API (Advanced V3) 翻译文档。
有关参数定义,请参阅 TranslateDocumentBatchOperator
使用算子¶
算子的基本用法
tests/system/google/cloud/translate/example_translate_document.py
translate_document_batch = TranslateDocumentBatchOperator(
task_id="batch_translate_document_op",
project_id=PROJECT_ID,
location=REGION,
source_language_code="en",
target_language_codes=["uk", "fr"],
input_configs=[BATCH_DOC_INPUT_ITEM_1, BATCH_DOC_INPUT_ITEM_2],
output_config=BATCH_OUTPUT_CONFIG,
)
TranslateCreateGlossaryOperator¶
使用 Cloud Translate API (Advanced V3) 创建翻译词汇表。
有关参数定义,请参阅 TranslateCreateGlossaryOperator
使用算子¶
算子的基本用法
tests/system/google/cloud/translate/example_translate_glossary.py
create_glossary = TranslateCreateGlossaryOperator(
task_id="glossary_create",
project_id=PROJECT_ID,
location=REGION,
input_config=GLOSSARY_FILE_INPUT,
glossary_id=f"glossary_new_{PROJECT_ID}",
language_pair={"source_language_code": "en", "target_language_code": "es"},
)
TranslateUpdateGlossaryOperator¶
使用 Cloud Translate API (Advanced V3) 更新翻译词汇表。只有 display_name
和 input_config
字段可供更新。通过更新 input_config - 词汇表字典也会更新。
有关参数定义,请参阅 TranslateUpdateGlossaryOperator
使用算子¶
算子的基本用法
tests/system/google/cloud/translate/example_translate_glossary.py
glossary_id = create_glossary.output["glossary_id"]
update_glossary = TranslateUpdateGlossaryOperator(
task_id="glossary_update",
project_id=PROJECT_ID,
location=REGION,
new_input_config=UPDATE_GLOSSARY_FILE_INPUT,
new_display_name=f"gl_{PROJECT_ID}_updated",
glossary_id=glossary_id,
)
TranslateListGlossariesOperator¶
列出项目中所有可用的翻译词汇表。
有关参数定义,请参阅 TranslateListGlossariesOperator
使用算子¶
算子的基本用法
tests/system/google/cloud/translate/example_translate_glossary.py
list_glossaries = TranslateListGlossariesOperator(
task_id="list_glossaries",
page_size=100,
project_id=PROJECT_ID,
location=REGION,
)
TranslateDeleteGlossaryOperator¶
删除翻译词汇表资源。
有关参数定义,请参阅 TranslateDeleteGlossaryOperator
使用算子¶
算子的基本用法
tests/system/google/cloud/translate/example_translate_glossary.py
delete_glossary = TranslateDeleteGlossaryOperator(
task_id="delete_glossary",
glossary_id=glossary_id,
project_id=PROJECT_ID,
location=REGION,
)
更多信息¶
请参阅:基础 (V2) Google Cloud Translate 文档。高级 (V3) Google Cloud Translate (Advanced) 文档。数据集 传统数据集与本地数据集比较。
参考¶
有关进一步信息,请参阅