Search in sources :

Example 6 with Detection

use of com.google.cloud.translate.Detection in project google-cloud-java by GoogleCloudPlatform.

the class ITTranslateSnippets method testDetectLanguageOfTextList.

@Test
public void testDetectLanguageOfTextList() {
    // [START translate_detect_language]
    // TODO(developer): Uncomment these lines.
    // import com.google.cloud.translate.*;
    // Translate translate = TranslateOptions.getDefaultInstance().getService();
    List<String> texts = new LinkedList<>();
    texts.add("Hello, World!");
    texts.add("¡Hola Mundo!");
    List<Detection> detections = translate.detect(texts);
    System.out.println("Language(s) detected:");
    for (Detection detection : detections) {
        System.out.printf("\t%s\n", detection);
    }
    // [END translate_detect_language]
    Detection detection = detections.get(0);
    assertEquals("en", detection.getLanguage());
    detection = detections.get(1);
    assertEquals("es", detection.getLanguage());
}
Also used : Detection(com.google.cloud.translate.Detection) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 7 with Detection

use of com.google.cloud.translate.Detection in project google-cloud-java by GoogleCloudPlatform.

the class DetectLanguageAndTranslate method main.

public static void main(String... args) {
    // Create a service object
    // 
    // If no explicit credentials or API key are set, requests are authenticated using Application
    // Default Credentials if available; otherwise, using an API key from the GOOGLE_API_KEY
    // environment variable
    Translate translate = TranslateOptions.getDefaultInstance().getService();
    // Text of an "unknown" language to detect and then translate into English
    final String mysteriousText = "Hola Mundo";
    // Detect the language of the mysterious text
    Detection detection = translate.detect(mysteriousText);
    String detectedLanguage = detection.getLanguage();
    // Translate the mysterious text to English
    Translation translation = translate.translate(mysteriousText, TranslateOption.sourceLanguage(detectedLanguage), TranslateOption.targetLanguage("en"));
    System.out.println(translation.getTranslatedText());
}
Also used : Translation(com.google.cloud.translate.Translation) Detection(com.google.cloud.translate.Detection) Translate(com.google.cloud.translate.Translate)

Example 8 with Detection

use of com.google.cloud.translate.Detection in project google-cloud-java by GoogleCloudPlatform.

the class ITTranslateSnippets method testDetectLanguageOfText.

@Test
public void testDetectLanguageOfText() {
    // SNIPPET translate_detect_language_string
    Detection detection = translate.detect("Hello, World!");
    // SNIPPET translate_detect_language_string
    assertEquals("en", detection.getLanguage());
}
Also used : Detection(com.google.cloud.translate.Detection) Test(org.junit.Test)

Aggregations

Detection (com.google.cloud.translate.Detection)8 Test (org.junit.Test)6 Translate (com.google.cloud.translate.Translate)2 Translation (com.google.cloud.translate.Translation)1 LinkedList (java.util.LinkedList)1