问题描述
我已经成功创建了一个可以成功列出的云词汇表,但是当尝试使用它来测试翻译时,出现错误:
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应该是正确的参数,因此我根本不理解该错误。我将不胜感激
解决方法
我已经用实验数据检查了您的代码,并修改了整个translate_text()方法的 parent 和 glossary_config 参数定义,并引用了{{3} },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],}
)
现在,您可能会问:“我怎么知道该请求字典中要放什么?”。好像该库为每种方法的字典提供了类型注释: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
(返回类型有些复杂,如您所见)
现在像这样更改代码后:
response = client.translate_text(
request={
"contents": [text],"source_language_code": "en","target_language_code": "hu","parent": parent,"glossary_config": glossary_config,}
)
我没有lossary_config错误,并且代码返回了翻译。我唯一的问题是,即使我得到的结果翻译说确实使用了我提供的词汇表,但它似乎并未使用我提供的词汇表。但这可能应该是另一个线程。
,相同的错误
Traceback (most recent call last):
File "translate_google.py",line 37,in <module>
translate_text_with_glossary("mozha","191450529352","myceph-en-zh")
File "translate_google.py",line 30,in translate_text_with_glossary
glossary_config=glossary_config,TypeError: translate_text() got an unexpected keyword argument 'glossary_config'
[root@lab101 3-google-translate]# pip-3 list|grep google-cloud-translate
google-cloud-translate (3.0.1)