use of com.google.cloud.translate.v3.DetectedLanguage in project java-translate by googleapis.
the class DetectLanguage method detectLanguage.
// Detecting the language of a text string
public static void detectLanguage(String projectId, String text) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (TranslationServiceClient client = TranslationServiceClient.create()) {
// Supported Locations: `global`, [glossary location], or [model location]
// Glossaries must be hosted in `us-central1`
// Custom Models must use the same location as your model. (us-central1)
LocationName parent = LocationName.of(projectId, "global");
// Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
DetectLanguageRequest request = DetectLanguageRequest.newBuilder().setParent(parent.toString()).setMimeType("text/plain").setContent(text).build();
DetectLanguageResponse response = client.detectLanguage(request);
// The most probable language is first.
for (DetectedLanguage language : response.getLanguagesList()) {
// The language detected
System.out.printf("Language code: %s\n", language.getLanguageCode());
// Confidence of detection result for this language
System.out.printf("Confidence: %s\n", language.getConfidence());
}
}
}
Aggregations