Azure AI Translator not returning Alignment data

ThioJoe 95 Reputation points
2024-01-20T21:39:51.02+00:00

I'm trying to set up basic translation functionality with the Azure Translate API. I've got it working but I can't seem to get it to give back the alignment, despite using the 'includeAlignment'='true' parameter. Below is my code. At first I thought maybe my test sentences were too generic, so I tried using absurd random sentences, but nothing seems to work. Am I missing something?

AZURE_TRANSLATE_KEY = cloudConfig['azure_translate_key']
AZURE_TRANSLATE_REGION = cloudConfig['azure_translate_region']
endpoint = "https://api.cognitive.microsofttranslator.com/"
path = '/translate'
constructed_url = endpoint + path

params = {
    'api-version': '3.0',
    'from': 'en',
    'to': 'de',
    'textType': 'plain',
    'includeAlignment': 'true',
    'includeSentenceLength': 'true',
}

headers = {
    'Ocp-Apim-Subscription-Key': AZURE_TRANSLATE_KEY,
    'Ocp-Apim-Subscription-Region': AZURE_TRANSLATE_REGION,
    'Content-type': 'application/json; charset=UTF-8'
}

# You can pass more than one object in body.
texts_to_translate = [
    "My favorite activity is running around in the middle of traffic! But it's probably not the safest thing to do. Would you suggest an alternative?",
]

body = [{'text': text} for text in texts_to_translate]
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=(',', ': ')))

And here's the response:

[
    {
        "translations": [
            {
                "sentLen": {
                    "srcSentLen": [
                        65,
                        46,
                        61
                    ],
                    "transSentLen": [
                        70,
                        66,
                        69
                    ]
                },
                "text": "Meine Lieblingsbeschäftigung ist es, mitten im Verkehr herumzulaufen! Aber es ist wahrscheinlich nicht das Sicherste, was man tun kann. Würden Sie eine Alternative vorschlagen, wie z.B. die Basketballjagd?",
                "to": "de"
            }
        ]
    }
]
Azure Translator in Foundry Tools

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.