All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.cloud.translate.spi.DefaultTranslateRpc Maven / Gradle / Ivy

There is a newer version: 2.56.0
Show newest version
/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.cloud.translate.spi;

import static com.google.cloud.translate.spi.TranslateRpc.Option.MODEL;
import static com.google.cloud.translate.spi.TranslateRpc.Option.SOURCE_LANGUAGE;
import static com.google.cloud.translate.spi.TranslateRpc.Option.TARGET_LANGUAGE;
import static com.google.common.base.MoreObjects.firstNonNull;

import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.json.JsonHttpContent;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.translate.Translate;
import com.google.api.services.translate.model.DetectionsListResponse;
import com.google.api.services.translate.model.DetectionsResourceItems;
import com.google.api.services.translate.model.LanguagesListResponse;
import com.google.api.services.translate.model.LanguagesResource;
import com.google.api.services.translate.model.TranslationsResource;
import com.google.cloud.translate.TranslateException;
import com.google.cloud.translate.TranslateOptions;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class DefaultTranslateRpc implements TranslateRpc {

  private final TranslateOptions options;
  private final Translate translate;

  public DefaultTranslateRpc(TranslateOptions options) {
    HttpTransport transport = options.getHttpTransportFactory().create();
    HttpRequestInitializer initializer = options.getHttpRequestInitializer();
    this.options = options;
    translate = new Translate.Builder(transport, new JacksonFactory(), initializer)
        .setRootUrl(options.getHost())
        .setApplicationName(options.getApplicationName())
        .build();
  }

  private static TranslateException translate(IOException exception) {
    return new TranslateException(exception);
  }

  private GenericUrl buildTargetUrl(String path) {
    GenericUrl genericUrl = new GenericUrl(translate.getBaseUrl() + "v2/" + path);
    if (options.getApiKey() != null) {
      genericUrl.put("key", options.getApiKey());
    }
    return genericUrl;
  }

  @Override
  public List> detect(List texts) {
    try {
      Map content = ImmutableMap.of("q", texts);
      HttpRequest httpRequest = translate.getRequestFactory()
          .buildPostRequest(buildTargetUrl("detect"),
              new JsonHttpContent(translate.getJsonFactory(), content))
          .setParser(translate.getObjectParser());
      List> detections =
          httpRequest.execute().parseAs(DetectionsListResponse.class).getDetections();
      // TODO use REST apiary as soon as it supports POST
      // List> detections =
      //    translate.detections().list(texts).setKey(options.getApiKey()).execute().getDetections();
      return detections != null ? detections : ImmutableList.>of();
    } catch (IOException ex) {
      throw translate(ex);
    }
  }

  @Override
  public List listSupportedLanguages(Map optionMap) {
    try {
      Map content = ImmutableMap.of("target",
          firstNonNull(TARGET_LANGUAGE.getString(optionMap), options.getTargetLanguage()));
      HttpRequest httpRequest = translate.getRequestFactory()
          .buildPostRequest(buildTargetUrl("languages"),
              new JsonHttpContent(translate.getJsonFactory(), content))
          .setParser(translate.getObjectParser());
      List languages =
          httpRequest.execute().parseAs(LanguagesListResponse.class).getLanguages();
      // TODO use REST apiary as soon as it supports POST
      // List languages = translate.languages()
      //     .list()
      //     .setKey(options.getApiKey())
      //     .setTarget(
      //         firstNonNull(TARGET_LANGUAGE.getString(optionMap), options.getTargetLanguage()))
      //     .execute().getLanguages();
      return languages != null ? languages : ImmutableList.of();
    } catch (IOException ex) {
      throw translate(ex);
    }
  }

  @Override
  public List translate(List texts, Map optionMap) {
    try {
      // TODO use POST as soon as usage of "model" correctly reports an error in non-whitelisted
      // projects
      String targetLanguage =
          firstNonNull(TARGET_LANGUAGE.getString(optionMap), options.getTargetLanguage());
      final String sourceLanguage = SOURCE_LANGUAGE.getString(optionMap);
      List translations =
          translate.translations()
              .list(texts, targetLanguage)
              .setSource(sourceLanguage)
              .setKey(options.getApiKey())
              .set("model", MODEL.getString(optionMap))
              .execute()
              .getTranslations();
      return Lists.transform(
          translations != null ? translations : ImmutableList.of(),
          new Function() {
            @Override
            public TranslationsResource apply(TranslationsResource translationsResource) {
              if (translationsResource.getDetectedSourceLanguage() == null) {
                translationsResource.setDetectedSourceLanguage(sourceLanguage);
              }
              return translationsResource;
            }
          });
    } catch (IOException ex) {
      throw translate(ex);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy