Microsoft Translator v中没有其他语言。3.0检测JSON响应

2024-10-02 18:18:56 发布

您现在位置:Python中文网/ 问答频道 /正文

根据Microsoft Translator 3.0 documentation,检测端点的JSON响应体应包含以下属性:

  • 备选方案:一系列其他可能的语言。数组的每个元素都是另一个具有上面列出的相同属性的对象:language、score、isTranslationSupported和isTranslationSupported

下面是来自Translator Quickstart web page的请求主体的示例:

[
    { "Text": "Ich würde wirklich gern Ihr Auto um den Block fahren ein paar Mal." }
]

这是一个预期的响应机构:

[
    {
        "alternatives": [
            {
                "isTranslationSupported": true,
                "isTransliterationSupported": false,
                "language": "nl",
                "score": 0.92
            },
            {
                "isTranslationSupported": true,
                "isTransliterationSupported": false,
                "language": "sk",
                "score": 0.77
            }
        ],
        "isTranslationSupported": true,
        "isTransliterationSupported": false,
        "language": "de",
        "score": 1.0
    }
]

但是,当我在语言检测端点中使用相同的请求主体时,我只得到一种分数为1.0的语言:

import requests, uuid, json

# Add your subscription key and endpoint
subscription_key = "XXXXXXXXXXXXXXXXXX"
endpoint = "https://api.cognitive.microsofttranslator.com"

# Add your location, also known as region. The default is global.
# This is required if using a Cognitive Services resource.
location = "global"

path = '/detect'
constructed_url = endpoint + path

params = {
'api-version': '3.0'
}
constructed_url = endpoint + path

headers = {
'Ocp-Apim-Subscription-Key': subscription_key,
'Ocp-Apim-Subscription-Region': location,
'Content-type': 'application/json',
'X-ClientTraceId': str(uuid.uuid4())
}

# You can pass more than one object in body.
body = [{
'text': 'Ich würde wirklich gern Ihr Auto um den Block fahren ein paar Mal.'
}]

request = requests.post(constructed_url, params=params, headers=headers, json=body)
response = request.json()

print(json.dumps(response, sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ': ')))

[
    {
        "isTranslationSupported": true,
        "isTransliterationSupported": false,
        "language": "de",
        "score": 1.0
    }
]

有人知道我在这里遗漏了什么吗


Tags: pathkey语言jsonfalsetrueurllocation
1条回答
网友
1楼 · 发布于 2024-10-02 18:18:56

经过测试,我尝试使用nodejs、officerestapi和C语言进行测试。得到同样的结果。我可以调试它,发现Alternatives总是空的

所以我确信官方文件不是最新的

现在你得到的回答是正确的。您可以在本页中submit feedback

enter image description here

相关问题 更多 >