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) 文件。資料集 傳統資料集與本地資料集比較。
參考¶
有關進一步資訊,請參閱