Если вы перейдете на страницу 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;
}
DetectedLanguage
@Data
public class DetectedLanguage {
private String language;
private double score;
}
DetectedLanguage
@Data
public class DetectedLanguage {
private String language;
private double score;
}