Search in sources :

Example 1 with DetectedLanguage

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());
        }
    }
}
Also used : TranslationServiceClient(com.google.cloud.translate.v3.TranslationServiceClient) DetectLanguageResponse(com.google.cloud.translate.v3.DetectLanguageResponse) DetectedLanguage(com.google.cloud.translate.v3.DetectedLanguage) DetectLanguageRequest(com.google.cloud.translate.v3.DetectLanguageRequest) LocationName(com.google.cloud.translate.v3.LocationName)

Aggregations

DetectLanguageRequest (com.google.cloud.translate.v3.DetectLanguageRequest)1 DetectLanguageResponse (com.google.cloud.translate.v3.DetectLanguageResponse)1 DetectedLanguage (com.google.cloud.translate.v3.DetectedLanguage)1 LocationName (com.google.cloud.translate.v3.LocationName)1 TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)1