将词汇表与 Google Cloud Translation API 集成以进行文档翻译

问题描述 投票:0回答:1

我目前正在致力于将词汇表与 Google Cloud Translation API 集成以进行文档翻译。我已经成功实现了文档翻译并单独创建了一个术语表,但我正在努力寻找一种在没有 AutoML 模型的情况下使用术语表 ID 直接翻译文档的方法。我已经浏览了 Google 提供的文档,但它没有提供明确的信息有关如何实现这一目标的说明。有人可以提供一个代码示例(最好是 Python 语言),说明如何使用词汇表 ID 和 Google Cloud Translation API 来翻译文档吗?提前感谢您的帮助!

google-cloud-platform google-api google-translate google-cloud-translate glossaries
1个回答
0
投票

如果您能提供如何创建术语表和提出文档翻译请求的代码片段,那就太好了。

假设您已经成功创建了词汇表资源,并且GCS中已经有文档。以下是使用术语表翻译文档的代码片段(您可以阅读有关此 API 的更多信息此处):

from google.cloud import translate_v3beta1 as translate

def translate_document(
    project_id: str,
    file_path: str,
    glossary_id: str  # Add your glossary ID parameter
) -> translate.TranslationServiceClient:
    """Translates a document.

    Args:
        project_id: The GCP project ID.
        file_path: The path to the file to be translated.
        glossary_id: glossary resource ID.

    Returns:
        The translated document.
    """

    client = translate.TranslationServiceClient()
    location = "us-central1"
    parent = f"projects/{project_id}/locations/{location}"
    document_input_config = {
        "content": document_content,
        "mime_type": "application/pdf",
    }
    

    # Create glossary configuration here
    glossary_config = translate.types.TranslateTextGlossaryConfig(glossary_id=glossary_id)

    response = client.translate_document(
        request={
            "parent": parent,
            "target_language_code": "fr-FR",
            "document_input_config": document_input_config,
            "glossary_config": glossary_config,  # Include glossary config
        }
    )

    return response

您不必将 AutoML 与术语表一起使用。

© www.soinside.com 2019 - 2024. All rights reserved.