我已经成功地为云创建了一个术语表,我可以成功地列出它,但是当尝试使用它来测试翻译出来时,我会得到一个错误:
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应该是正确的论点,所以我根本不理解错误。我很感谢你的帮助
发布于 2020-08-17 08:15:59
让我从另一个帮助我部分解决问题的问题中复制用户帕迪·奥顿的回答:
也遇到过这种情况。并非所有的文档都已更新,但它们已经发布了一份迁移指南:
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],
}
)现在,你很可能会问:“我怎么知道把什么放在那本请求字典里呢?”似乎库附带了适合每种方法的字典的类型注释:v3/types.html es.html。
例如,我在您的评论中看到了另一个答复,您在detect_language方法上遇到了问题。方法签名表明,如果使用关键字参数,content应该是有效的,所以我不知道为什么失败--可能是个bug。
但是,如果您使用的是request字典,则应该是像这样。您将看到,这些键似乎与方法签名关键字不完全对应(尽管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(如您所见,返回类型有点复杂)
现在,在像这样更改代码之后:
response = client.translate_text(
request={
"contents": [text],
"source_language_code": "en",
"target_language_code": "hu",
"parent": parent,
"glossary_config": glossary_config,
}
)我没有得到glossary_config错误,代码返回一个转换。我现在唯一的问题是,我得到的结果翻译似乎没有使用我提供的术语表,尽管它说它使用。但这可能是另一条线索。
发布于 2020-08-13 10:45:46
我已经用我的实验数据检查了您的代码,修改了一些glossary_config父和案文()参数定义--从文档中引用示例的整个案文()方法-- 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)请记住,在代码示例或问题描述中共享个人可识别信息,例如唯一帐户标识符,可能会大大增加有害事件损害客户计算资源的风险。
https://stackoverflow.com/questions/63370098
复制相似问题