本文整理汇总了Python中google.cloud.translate.Client方法的典型用法代码示例。如果您正苦于以下问题:Python translate.Client方法的具体用法?Python translate.Client怎么用?Python translate.Client使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.cloud.translate
的用法示例。
在下文中一共展示了translate.Client方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: translate
# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def translate(in_str):
if not in_str:
return
trans_client = google_trans.Client()
lang = trans_client.detect_language(in_str)['language']
if lang == 'en':
trans = trans_client.translate(
in_str, target_language='zh')
return trans['translatedText']
else:
trans = trans_client.translate(
in_str, target_language='en')
return trans['translatedText']
示例2: google_translate
# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def google_translate(sk_text):
""" CAUTION! This is a paid service! """
from google.cloud import translate
translate_client = translate.Client()
result = translate_client.translate(sk_text, source_language='sk', target_language='en')
result = result['translatedText']
print("Google Translate: {} -> {}".format(sk_text, result))
return result
示例3: __init__
# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def __init__(self):
super(GoogleTranslateFramework, self).__init__()
credentials = os.getenv('GOOGLE_APPLICATION_CREDENTIALS')
if credentials is None:
raise ValueError("missing credentials")
if credentials.startswith('{'):
credential_path = os.path.join(self._tmp_dir, 'Gateway-Translate-API.json')
with open(credential_path, 'w') as f:
f.write(credentials)
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
self._client = translate.Client()
self.max_retry = 5
self._GOOGLE_LIMIT_TIME = 100
示例4: get_labels
# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def get_labels(photo_file):
vision_client = vision.Client()
image = vision_client.image(
source_uri = 'gs://%s/%s' % (bucket_name, photo_file))
return image.detect_labels(limit=3)
示例5: translate_text
# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def translate_text(text):
if tag_language == 'en':
return text
translate_client = translate.Client()
result = translate_client.translate(text, target_language=tag_language)
return result['translatedText']
示例6: supported_locale_codes
# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def supported_locale_codes():
return [l[u'language'] for l in translate.Client().get_languages()]
#https://github.com/fastlane/fastlane/blob/master/fastlane_core/lib/fastlane_core/languages.rb
示例7: _translate
# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def _translate(dest_l, src_text_lines):
lines = [t[u'translatedText']+'\n' for t in translate.Client(target_language=dest_l).translate(src_text_lines)]
return map(lambda line: proc_after_translate(line), lines)
示例8: supported_locale_codes
# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def supported_locale_codes():
return [l[u'language'] for l in translate.Client().get_languages()]
示例9: translate_strs
# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def translate_strs(strs, to):
__trans = translate.Client(target_language=to)
assert len(strs) or isinstance(strs[0], str), "Input variables should be string list"
pre_items = [item for item in __preprocessing_translate_strs(strs, to)]
translated_items = __trans.translate([item.trans_input_text for item in pre_items])
assert len(translated_items) == len(pre_items), "the numbers of input items and translated items must be same."
# map results
for i, t in enumerate(translated_items):
_result = t[u'translatedText']
_pre_trans_item = pre_items[i]
_literal_replacement_exist = bool(len(_pre_trans_item.matched_literal_items))
_literal_replacement_contains_all = bool(
len(filter(lambda s: s in _result, [mitem.replacement for mitem in _pre_trans_item.matched_literal_items])))
# if literal replacement did not contain from translator, use original text.
if _literal_replacement_exist and not _literal_replacement_contains_all:
_pre_trans_item.trans_output_text = _pre_trans_item.original_text
# else, it means all replacing processes are completed, use/commit translated text.
else:
_pre_trans_item.trans_output_text = _result
# print pre_items[i].trans_input_text, '->' , pre_items[i].trans_output_text
return __PostprocessingTransItem(pre_items).finalize_strs()
示例10: get_g_translate
# 需要导入模块: from google.cloud import translate [as 别名]
# 或者: from google.cloud.translate import Client [as 别名]
def get_g_translate(sent: str, lang: str) -> Optional[str]:
try:
client = translate.Client()
t = client.translate(sent, target_language=lang)['translatedText']
return t
except Exception as e:
print(e)
return ''