尝试使用词汇表翻译文本时出现lossary_config错误

如何解决尝试使用词汇表翻译文本时出现lossary_config错误

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

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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-