【问题标题】:Can Microsoft Translator API translate text with special characters?Microsoft Translator API 可以翻译带有特殊字符的文本吗?
【发布时间】:2021-08-03 22:19:28
【问题描述】:

我正在尝试使用 Microsoft Translator API 将文本从波兰语翻译成任何其他语言。在波兰语中,有几个特殊字符,如“ą”、“ś”、“ż”等。当我发送没有特殊字符的 HTTP 请求时:

POST /translate?api-version=3.0&from=pl&to=en HTTP/1.1
Ocp-Apim-Subscription-Key: ********
Ocp-Apim-Subscription-Region: ******
Content-Length: 21
Host: api.cognitive.microsofttranslator.com
Connection: close
User-Agent: Apache-HttpClient/4.5.10 (Java/15.0.2)
Accept-Encoding: gzip, deflate

[{"Text": "Gramatyka"}]

我收到正确的翻译:

[{"translations":[{"text":"grammar","to":"en"}]}]

但是,波兰语单词或句子很可能包含特殊字符:

POST /translate?api-version=3.0&from=pl&to=en HTTP/1.1
Ocp-Apim-Subscription-Key: ********
Ocp-Apim-Subscription-Region: ********
Content-Length: 21
Host: api.cognitive.microsofttranslator.com
Connection: close
User-Agent: Apache-HttpClient/4.5.10 (Java/15.0.2)
Accept-Encoding: gzip, deflate

[{"Text": "Roślina"}]

此请求导致错误代码 400000:

{"error":{"code":400000,"message":"One of the request inputs is not valid."}}

如果我将特殊字符更改为标准字符(例如将“ś”更改为“s”),API 不会提供正确的翻译。例如:

[{"Text": "Roslina"}]

结果:

[{"translations":[{"text":"Roslina","to":"en"}]}]

而“roślina”应该翻译成“植物”。

这个问题也适用于其他语言。例如德语:

[{"Text": "Wörterbuch"}]

也会导致 400000 错误。

有人找到解决办法了吗?

【问题讨论】:

  • 这能回答你的问题吗? How to send UTF8 to the WordPress REST-API?
  • 注意 dup 讲的是 WordPress,但问题几乎可以肯定是一样的,你需要将 ContentType 标头设置为 application/json; charset=utf-8
  • 我之前尝试过,但没有成功,但在您意识到所需的标头不是ContentType 而是Content-Type 之后。没有破折号它不起作用,令人惊讶的是它也区分大小写。谢谢!

标签: azure api microsoft-cognitive azure-cognitive-services microsoft-translator


【解决方案1】:

您是否尝试检查语言检测分数,以了解它是否将其视为波兰语。您可以尝试不使用“From”属性吗?确保放置所有标题。 curl -X POST "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=zh-Hans" -H "Ocp-Apim-Subscription-Key: " -H "Content-Type: 应用程序/json; charset=UTF-8" -d "[{'Text':'你好,你叫什么名字?'}]"

【讨论】:

  • 问题原来是请求中缺少Content-Type 标头。因此,Azure 需要正确处理特殊字符的只是 Content-Type: application/json; 标头。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 2010-11-16
相关资源
最近更新 更多