如何从Microsoft Translator获取翻译组?

huangapple go评论70阅读模式
英文:

How to get translation groups from Microsoft Translator?

问题

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

protected List<Group> fetchTranslationGroups(String word, String translationType) {
    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);

    AzureTranslateResponse azureTranslateResponse = new ObjectMapper().readValue(json, AzureTranslateResponse.class);

    return azureTranslateResponse.getTranslations().get(0).getOtherForms();
}

@Data
public class AzureTranslateResponse {
    private DetectedLanguage detectedLanguage;
    private List<Translation> translations;
}

@Data
public class DetectedLanguage {
    private String language;
    private double score;
}

@Data
public class Translation {
    private String text;
    private List<Group> others;
}

@Data
public class Group {
    private String type;
    private List<String> translations;
}

Note: In the provided code, I've made modifications to allow fetching the "Other ways to say" groups. Please ensure that you have the necessary dependencies and imports for the classes used in the code.

英文:

If you go to https://www.bing.com/translator, (which uses the MS/Azure Translator api) and type in the word mean from English to Swedish, in addition to the "main" translation you get on the right, you also have a section that has "Other ways to say", which are grouped by Verb, Noun, and Adjective.

如何从Microsoft Translator获取翻译组?

I would like to know how I can fetch this list of groups from the response.

Right now I have the following, but it only returns the main translation, in this case 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(&quot;application/json&quot;);
RequestBody body = RequestBody.create(mediaType,
&quot;[{\n\t\&quot;Text\&quot;: \&quot;&quot; + word + &quot;\&quot;\n}]&quot;);
Request request = new Request.Builder()
.url(BASE_URL + translationType)
.post(body)
.addHeader(&quot;Ocp-Apim-Subscription-Key&quot;, SUBSCRIPTION_KEY)
.addHeader(&quot;Ocp-Apim-Subscription-Region&quot;, SUBSCRIPTION_REGION)
.addHeader(&quot;Content-type&quot;, &quot;application/json&quot;)
.build();
Response response = okHttpClient.newCall(request)
.execute();
if (!response.isSuccessful()) {
throw new AzureTranslateException(&quot;Failed to get translations from Azure Translator API, due to: &quot;
+ 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&lt;Translation&gt; translations;
}

DetectedLanguage

@Data
public class DetectedLanguage {
private String language;
private double score;
}

DetectedLanguage

@Data
public class DetectedLanguage {
private String language;
private double score;
}

答案1

得分: 1

你可以使用词典查询资源来获取替代翻译。\nhttps://learn.microsoft.com/azure/cognitive-services/translator/reference/v3-0-dictionary-lookup\n\n它会在posTag属性中返回词性。然后,您可以按posTag分组,以实现类似的分组。\n\n词典示例资源会返回您在必应翻译器网站上看到的示例句子。\nhttps://learn.microsoft.com/azure/cognitive-services/translator/reference/v3-0-dictionary-examples

英文:

You can retrieve alternative translations using the Dictionary Lookup resource.
https://learn.microsoft.com/azure/cognitive-services/translator/reference/v3-0-dictionary-lookup

It returns the part of speech in the posTag attribute. You can then group by posTag to achieve a similar grouping.

The Dictionary Examples resource returns the example sentences that you see on the Bing Translator site as well.
https://learn.microsoft.com/azure/cognitive-services/translator/reference/v3-0-dictionary-examples

huangapple
  • 本文由 发表于 2020年9月13日 22:28:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63871900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定