• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java Translation类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中com.google.cloud.translate.Translation的典型用法代码示例。如果您正苦于以下问题:Java Translation类的具体用法?Java Translation怎么用?Java Translation使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



Translation类属于com.google.cloud.translate包,在下文中一共展示了Translation类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: add

import com.google.cloud.translate.Translation; //导入依赖的package包/类
@Override
public void add(Page page) {
    reader.setPage(page);

    try {
        while (reader.nextRecord()) {
            List<String> sourceTexts = Lists.newArrayList();
            for (Column keyNameColumn : keyNameColumns) {
                String text = reader.isNull(keyNameColumn) ? "" : reader.getString(keyNameColumn);
                logger.debug(text);
                sourceTexts.add(text);
            }
            List<Translation> translations = translate(sourceTexts);
            for (int i = 0; i < keyNameColumns.size(); i++) {
                builder.setString(outputSchema.lookupColumn(keyNameColumns.get(i).getName() + task.getOutKeyNameSuffix()), translations.get(i).getTranslatedText());
            }
            setValue(builder);
            builder.addRecord();
            Thread.sleep(task.getSleep().get());
        }
    } catch (Exception  e) {
        throw new RuntimeException(e);
    } 
}
 
开发者ID:toyama0919,项目名称:embulk-filter-google_translate_api,代码行数:25,代码来源:GoogleTranslateApiPageOutput.java


示例2: canTranslateHTML

import com.google.cloud.translate.Translation; //导入依赖的package包/类
@Test
public void canTranslateHTML() {
    List<String> source = Lists.newArrayList("hello");
    List<Translation> expectedTrans = Lists.newArrayList(translation);

    when(translate.translate(source,
            Translate.TranslateOption.targetLanguage("zh"),
            Translate.TranslateOption.format("html")))
                    .thenReturn(expectedTrans);
    List<AugmentedTranslation> translations = backend.translate(source,
            new GoogleLocaleCode("en"), new GoogleLocaleCode("zh"),
            MediaType.TEXT_HTML_TYPE, Optional.empty());

    assertThat(translations).hasSize(expectedTrans.size());
    AugmentedTranslation augmentedTranslation = translations.get(0);
    assertThat(augmentedTranslation.getPlainTranslation()).isEqualTo(translation.getTranslatedText());
}
 
开发者ID:zanata,项目名称:zanata-mt,代码行数:18,代码来源:GoogleTranslatorBackendTest.java


示例3: canTranslateText

import com.google.cloud.translate.Translation; //导入依赖的package包/类
@Test
public void canTranslateText() {
    List<String> source = Lists.newArrayList("hello");
    List<Translation> expectedTrans = Lists.newArrayList(translation);

    when(translate.translate(source,
            Translate.TranslateOption.targetLanguage("zh"),
            Translate.TranslateOption.format("text")))
            .thenReturn(expectedTrans);
    List<AugmentedTranslation> translations = backend.translate(source,
            new GoogleLocaleCode("en"), new GoogleLocaleCode("zh"),
            MediaType.TEXT_PLAIN_TYPE, Optional.empty());

    assertThat(translations).hasSize(expectedTrans.size());
    AugmentedTranslation augmentedTranslation = translations.get(0);
    assertThat(augmentedTranslation.getPlainTranslation()).isEqualTo(translation.getTranslatedText());
}
 
开发者ID:zanata,项目名称:zanata-mt,代码行数:18,代码来源:GoogleTranslatorBackendTest.java


示例4: translateText

import com.google.cloud.translate.Translation; //导入依赖的package包/类
/**
 * Translate the source text from source to target language.
 *
 * @param sourceText source text to be translated
 * @param sourceLang source language of the text
 * @param targetLang target language of translated text
 * @return source text translated into target language.
 */
public static String translateText(
    String sourceText,
    String sourceLang,
    String targetLang) {
  if (Strings.isNullOrEmpty(sourceLang)
      || Strings.isNullOrEmpty(targetLang)
      || sourceLang.equals(targetLang)) {
    return sourceText;
  }
  com.google.cloud.translate.Translate translate = createTranslateService();
  TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
  TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang);

  Translation translation = translate.translate(sourceText, srcLang, tgtLang);
  return translation.getTranslatedText();
}
 
开发者ID:GoogleCloudPlatform,项目名称:getting-started-java,代码行数:25,代码来源:Translate.java


示例5: main

import com.google.cloud.translate.Translation; //导入依赖的package包/类
public static void main(String... args) throws Exception {
  // Instantiates a client
  Translate translate = TranslateOptions.getDefaultInstance().getService();

  // The text to translate
  String text = "Hello, world!";

  // Translates some text into Russian
  Translation translation =
      translate.translate(
          text,
          TranslateOption.sourceLanguage("en"),
          TranslateOption.targetLanguage("ru"));


  System.out.printf("Text: %s%n", text);
  System.out.printf("Translation: %s%n", translation.getTranslatedText());
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:19,代码来源:QuickstartSample.java


示例6: translateTextWithOptionsAndModel

import com.google.cloud.translate.Translation; //导入依赖的package包/类
/**
 * Translate the source text from source to target language.
 * Make sure that your project is whitelisted.
 *
 * @param sourceText source text to be translated
 * @param sourceLang source language of the text
 * @param targetLang target language of translated text
 * @param out print stream
 */
public static void translateTextWithOptionsAndModel(
    String sourceText,
    String sourceLang,
    String targetLang,
    PrintStream out) {

  Translate translate = createTranslateService();
  TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
  TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang);

  // Use translate `model` parameter with `base` and `nmt` options.
  TranslateOption model = TranslateOption.model("nmt");

  Translation translation = translate.translate(sourceText, srcLang, tgtLang, model);
  out.printf("Source Text:\n\tLang: %s, Text: %s\n", sourceLang, sourceText);
  out.printf("TranslatedText:\n\tLang: %s, Text: %s\n", targetLang,
      translation.getTranslatedText());
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:28,代码来源:TranslateText.java


示例7: translateTextWithOptions

import com.google.cloud.translate.Translation; //导入依赖的package包/类
/**
 * Translate the source text from source to target language.
 *
 * @param sourceText source text to be translated
 * @param sourceLang source language of the text
 * @param targetLang target language of translated text
 * @param out print stream
 */
public static void translateTextWithOptions(
    String sourceText,
    String sourceLang,
    String targetLang,
    PrintStream out) {

  Translate translate = createTranslateService();
  TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang);
  TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang);

  Translation translation = translate.translate(sourceText, srcLang, tgtLang);
  out.printf("Source Text:\n\tLang: %s, Text: %s\n", sourceLang, sourceText);
  out.printf("TranslatedText:\n\tLang: %s, Text: %s\n", targetLang,
      translation.getTranslatedText());
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:24,代码来源:TranslateText.java


示例8: translate

import com.google.cloud.translate.Translation; //导入依赖的package包/类
/**
 * @param texts
 * @return
 */
private List<Translation> translate(List<String> texts) {
    TranslateOption[] translateOptions = new TranslateOption[] {};
    if (srcLang != null) {
        translateOptions = ArrayUtils.add(translateOptions, srcLang);
    }
    if (model != null) {
        translateOptions = ArrayUtils.add(translateOptions, model);
    }
    return translate.translate(texts, translateOptions);
}
 
开发者ID:toyama0919,项目名称:embulk-filter-google_translate_api,代码行数:15,代码来源:GoogleTranslateApiPageOutput.java


示例9: translate

import com.google.cloud.translate.Translation; //导入依赖的package包/类
@Override
    public List<AugmentedTranslation> translate(List<String> contents,
            BackendLocaleCode srcLocale, BackendLocaleCode targetLocale,
            MediaType mediaType, Optional<String> category) throws MTException {
        String format = MediaType.TEXT_HTML_TYPE.isCompatible(mediaType) ? "html" : "text";
        List<Translate.TranslateOption> options = Lists.newLinkedList();
        options.add(Translate.TranslateOption
                .targetLanguage(
                        targetLocale.getLocaleCode()));
        options.add(Translate.TranslateOption.format(format));
        if (!googleCredential.exists()) {
            throw new BadRequestException("Google Default Credential file is not setup");
        }
        // google can detect source locale if omitted
        // TODO we should probably retrieve and cache a google supported language list and check if the given locale code is supported or not
//        srcLocale.ifPresent(l -> options.add(
//                Translate.TranslateOption.sourceLanguage(l.getLocaleCode())));
        try {
            List<Translation> translations =
                    translate.translate(
                            contents,
                            options.toArray(new Translate.TranslateOption[options.size()]));
            return translations.stream()
                    .map(translation -> new AugmentedTranslation(
                            translation.getTranslatedText(),
                            dtoUtil.toJSON(translation))).collect(
                            Collectors.toList());
        } catch (Exception e) {
            throw new MTException(
                    "Unable to get translations from Google API", e);
        }
    }
 
开发者ID:zanata,项目名称:zanata-mt,代码行数:33,代码来源:GoogleTranslatorBackend.java


示例10: translateText

import com.google.cloud.translate.Translation; //导入依赖的package包/类
/**
 * Translates the source text in any language to English.
 *
 * @param sourceText source text to be translated
 * @param out print stream
 */
public static void translateText(String sourceText, PrintStream out) {
  Translate translate = createTranslateService();
  Translation translation = translate.translate(sourceText);
  out.printf("Source Text:\n\t%s\n", sourceText);
  out.printf("Translated Text:\n\t%s\n", translation.getTranslatedText());
}
 
开发者ID:GoogleCloudPlatform,项目名称:java-docs-samples,代码行数:13,代码来源:TranslateText.java


示例11: translate

import com.google.cloud.translate.Translation; //导入依赖的package包/类
public String translate(String text, String languageFrom,
		String languageTo) {
	Translation translation = translate.translate(text,
			TranslateOption.sourceLanguage(languageFrom),
			TranslateOption.targetLanguage(languageTo));

	String translatedText = translation.getTranslatedText();
	log.trace("Translating {} [{}] to [{}] ... result={}", text,
			languageFrom, languageTo, translatedText);
	return translatedText;
}
 
开发者ID:bonigarcia,项目名称:dualsub,代码行数:12,代码来源:Translator.java


示例12: translateText

import com.google.cloud.translate.Translation; //导入依赖的package包/类
/**
 * Translates the source text in any language to English.
 *
 * @param sourceText source text to be translated
 * @return translated text
 */
public static String translateText(String sourceText) {
    Translate translate = createTranslateService();
    Translation translation = translate.translate(sourceText);

    return translation.getTranslatedText();


}
 
开发者ID:ikhamlic,项目名称:esct-messenger-bot,代码行数:15,代码来源:TranslateText.java



注:本文中的com.google.cloud.translate.Translation类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java PStack类代码示例发布时间:2022-05-23
下一篇:
Java HotDeployMessageListener类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap