【问题标题】:How to translate a long text with google cloud translate v3如何使用谷歌云翻译 v3 翻译长文本
【发布时间】:2020-10-28 12:27:04
【问题描述】:

环境: API:谷歌云翻译 V3, 文字大小:12 000 字

translateText() 方法可以翻译单词和短句,但是当我运行整个文本时,我遇到了“文本太长”的错误。

"message": "Text is too long.",
    "code": 3,
    "status": "INVALID_ARGUMENT",
    "details": [
        {
            "@type": 0,
            "data": "type.googleapis.com\/google.rpc.BadRequest"
        },
        {
            "@type": 0,
            "data": [
                {
                    "field": "contents",
                    "description": "The total codepoints in the request must be less than 30720, actual: 90005"
                }
            ]
        }
    ]
}

【问题讨论】:

    标签: google-cloud-platform google-cloud-translate


    【解决方案1】:

    U 可以将文本分成多个部分(多个请求),contents[] 内的代码点最多为 30k,如 API 中所述:https://cloud.google.com/translate/docs/reference/rest/v3/projects.locations/translateText

    // Request 1
    {
      "sourceLanguageCode": "en",
      "targetLanguageCode": "de",
      "contents": ["Text part one..."]
    }
    
    // Request 2
    {
      "sourceLanguageCode": "en",
      "targetLanguageCode": "de",
      "contents": ["...text part two..."]
    }
    
    // Request n
    {
      "sourceLanguageCode": "en",
      "targetLanguageCode": "de",
      "contents": ["...text part n."]
    }
    

    或者使用批量翻译,它以异步批处理模式翻译大量文本:https://cloud.google.com/translate/docs/reference/rest/v3/projects.locations/batchTranslateText。这个有点复杂,因为您必须将文件上传到 Google Cloud Storage。

    【讨论】:

      猜你喜欢
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 2019-09-23
      • 1970-01-01
      相关资源
      最近更新 更多