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\")[0] }}'"
)

範本

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,
)

更多資訊

請參閱:基本 (V2) Google Cloud 翻譯文件。進階 (V3) Google Cloud 翻譯 (進階) 文件。資料集:傳統與原生資料集比較

參考資料

如需更多資訊,請參閱

此條目是否有幫助?