glossary_config 尝试使用词汇表翻译文本时出错

glossary_config error when trying to use a glossary to translate text

我已经成功地创建了一个可以成功列出的云词汇表,但是当我试图用它来测试翻译时,我得到了一个错误:

Traceback (most recent call last):
  File "C:\py\gtranstest2.py", line 57, in <module>
    translate_text_with_glossary(eng,pid,gid)
  File "C:\py\gtranstest2.py", line 45, in translate_text_with_glossary
    response = client.translate_text(
TypeError: translate_text() got an unexpected keyword argument 'glossary_config'

我的代码(基于此处提供的示例代码:https://cloud.google.com/translate/docs/advanced/glossary#v3):

from google.cloud import translate_v3


eng = "H1 High beam, H1 Low beam (included)"
pid = "[HIDDEN]"




def translate_text_with_glossary(
    text,
    project_id,
):
    """Translates a given text using a glossary."""

    client = translate_v3.TranslationServiceClient()
    parent = 'projects/[HIDDEN]/locations/us-central1'


    glossary_config = translate_v3.types.TranslateTextGlossaryConfig(glossary="projects/[HIDDEN]/locations/us-central1/glossaries/kittglossaryv2")

    # Supported language codes: https://cloud.google.com/translate/docs/languages
    response = client.translate_text(
        contents=[text],
        target_language_code="en",
        source_language_code="hu",
        parent=parent,
        glossary_config=glossary_config,
    )
    print("Translated text: \n")
    for translation in response.glossary_translations:
        # print(u"\t {}".format(translation.translated_text))
        return translation.translated_text

translate_text_with_glossary(eng,pid)

glossary_config 应该是正确的参数,所以我根本不明白这个错误。我将不胜感激

我用我的实验数据检查了你的代码,修改了一点 parentglossary_config 参数定义整个 translate_text() method referring to the example 从文档中,translate_text_with_glossary() 函数按预期工作。

client = translate_v3.TranslationServiceClient()

parent = client.location_path("Project_ID", "us-central1")
glossary = client.glossary_path("Project_ID", "us-central1", "Glossary-ID")

glossary_config = translate_v3.types.TranslateTextGlossaryConfig(glossary=glossary)

请记住,在代码示例或问题描述中共享个人身份信息(例如唯一帐户标识符)可能会显着增加危害客户计算资源的有害事件的风险。

让我从另一个帮助我部分解决问题的问题中复制用户 Paddy Alton 的回答:


也遇到了这个。尚未更新所有文档,但他们已发布迁移指南:

https://googleapis.dev/python/translation/latest/UPGRADING.html

您可以将 parent 替换为 "projects/<PROJECT_ID>/locations/<LOCATION>"

或定义

def location_path(project_id, location):
    # might as well use an f-string, the new library supports python >=3.6
    return f"projects/{project_id}/locations/{location}"

并将 client.location_path 更改为 location_path 如果这是您在许多地方使用的东西。

还有更多彻底的变化。他们现在更喜欢你将一个名为 request 的字典传递给 API 方法,尽管旧方法仍然被接受。因此,您的代码可能如下所示:

from google.cloud import translate_v3 as translate

client = translate.TranslationServiceClient(credentials=credentials)

response = client.translate_text(
    request={
        "parent": "projects/my-location/locations/global",
        "target_language_code": target_language_code,
        "contents": [text],
    }
)

现在,您可能会问 'how will I know what to put in that request dictionary?'。看起来该库带有适用于每种方法的字典的类型注释:https://googleapis.dev/python/translation/latest/translate_v3/types.html

例如,我在您对另一个答案的评论中读到您在使用 detect_language 方法时遇到了问题。方法签名表明,如果您使用关键字参数,content 应该是有效的,所以我不知道为什么会失败 - 也许这是一个错误。

但是,如果您改用 request 字典,那应该看起来 like this。您会看到键似乎与方法签名关键字不完全对应(尽管 content 是其中之一)。

此代码有效:

response = client.detect_language({
    "parent": "projects/my-location/locations/global",
    "content": "Tá Gaeilge Agam, ach tá mé i mo chonai i Sasana",
})

lang = response.languages[0].language_code

(return 类型有点复杂,如您所见)


现在像这样更改代码后:

response = client.translate_text(
        request={
            "contents": [text],
            "source_language_code": "en",
            "target_language_code": "hu",            
            "parent": parent,
            "glossary_config": glossary_config,
           }
    )

我没有收到 glossary_config 错误,代码 return 是一个翻译。我现在唯一的问题是,我得到的结果翻译似乎没有使用我提供的词汇表,即使它说是。但这可能应该是另一个线程。