英文:
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.
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("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;
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论