如何从 Microsoft Translator 获取翻译组?

How to get translation groups from Microsoft Translator?

如果您转到 https://www.bing.com/translator,(它使用 MS/Azure 翻译器 api)并输入 word 从英语到瑞典语的意思,除了右侧的“主要”翻译,还有一个包含“其他表达方式”的部分,按动词、名词和形容词分组。

我想知道如何从响应中获取此组列表。

现在我有以下内容,但它只是 returns 主要翻译,在这种情况下 Menar

import com.fasterxml.jackson.databind.ObjectMapper;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Protocol;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

protected String doInBackground(String... params) {

    String word = params[0];
    String translationType = params[1];

    MediaType mediaType = MediaType.parse("application/json");
    RequestBody body = RequestBody.create(mediaType,
            "[{\n\t\"Text\": \"" + word + "\"\n}]");
    Request request = new Request.Builder()
            .url(BASE_URL + translationType)
            .post(body)
            .addHeader("Ocp-Apim-Subscription-Key", SUBSCRIPTION_KEY)
            .addHeader("Ocp-Apim-Subscription-Region", SUBSCRIPTION_REGION)
            .addHeader("Content-type", "application/json")
            .build();

    Response response = okHttpClient.newCall(request)
            .execute();
    if (!response.isSuccessful()) {
        throw new AzureTranslateException("Failed to get translations from Azure Translator API, due to: "
                + response.message());
    }
    String json = response.body().string();
    // remove the first and last characters, which are brackets, for ObjectMapper
    json = json.substring(1, json.length() - 1);

    // this will only have ONE translation
    AzureTranslateResponse r = new ObjectMapper().readValue(json, AzureTranslateResponse.class);

    return r.getTranslations().get(0).getText();
}

AzureTranslatorResponse

@Data
public class AzureTranslateResponse {

    private DetectedLanguage detectedLanguage;
    private List<Translation> translations;
}

检测到的语言

@Data
public class DetectedLanguage {

    private String language;
    private double score;
}

检测到的语言

@Data
public class DetectedLanguage {

    private String language;
    private double score;
}

您可以使用词典查找资源检索替代翻译。 https://docs.microsoft.com/azure/cognitive-services/translator/reference/v3-0-dictionary-lookup

它returns posTag 属性中的词性。然后你可以通过 posTag 分组来实现类似的分组。

词典示例资源 returns 您在 Bing 翻译网站上看到的例句。 https://docs.microsoft.com/azure/cognitive-services/translator/reference/v3-0-dictionary-examples