【问题标题】:How to get translation groups from Microsoft Translator?如何从 Microsoft Translator 获取翻译组?
【发布时间】:2020-09-14 03:30:58
【问题描述】:

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

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

现在我有以下内容,但它只返回主要翻译,在本例中为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;
}

【问题讨论】:

    标签: java azure microsoft-translator


    【解决方案1】:

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

    它返回 posTag 属性中的词性。然后可以按 postTag 进行分组,实现类似的分组。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 2021-08-03
      相关资源
      最近更新 更多