aws python sdk批量上传_使用 AWS SDK for Python (Boto) 翻译文本 - Amazon Translate

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 AWS SDK for Python (Boto) 翻译文本

以下示例演示了如何在 Python 中使用 TranslateText 操作。要运行它,您必须先通过 AWS CLI 安装 Amazon Translate。有关说明,请参阅步骤 2:设置 AWS Command Line Interface (AWS CLI)。import boto3

translate = boto3.client(service_name='translate', region_name='region', use_ssl=True)

result = translate.translate_text(Text="Hello, World",

SourceLanguageCode="en", TargetLanguageCode="de")

print('TranslatedText: ' + result.get('TranslatedText'))

print('SourceLanguageCode: ' + result.get('SourceLanguageCode'))

print('TargetLanguageCode: ' + result.get('TargetLanguageCode'))

有关支持的语言代码的列表,请参阅支持的语言和语言代码

自定义术语

另一个示例,此示例演示如何在 Python 中使用自定义术语操作:#!/usr/bin/env python

# -*- coding: utf-8 -*-

import boto3

translate = boto3.client(service_name='translate')

# The terminology file 'my-first-terminology.csv' has the following contents:

'''

en,fr

Amazon Family,Amazon Famille

'''

# Read the terminology from a local file

with open('/tmp/my-first-terminology.csv', 'rb') as f:

data = f.read()

file_data = bytearray(data)

print("Importing the terminology into Amazon Translate...")

response = translate.import_terminology(Name='my-first-terminology', MergeStrategy='OVERWRITE', TerminologyData={"File": file_data, "Format": 'CSV'})

print("Terminology imported: "),

print(response.get('TerminologyProperties'))

print("\n")

print("Getting the imported terminology...")

response = translate.get_terminology(Name='my-first-terminology', TerminologyDataFormat='CSV')

print("Received terminology: "),

print(response.get('TerminologyProperties'))

print("The terminology data file can be downloaded here: " + response.get('TerminologyDataLocation').get('Location'))

print("\n")

print("Listing the first 10 terminologies for the account...")

response = translate.list_terminologies(MaxResults=10)

print("Received terminologies: "),

print(response.get('TerminologyPropertiesList'))

print("\n")

print("Translating 'Amazon Family' from English to French with no terminology...")

response = translate.translate_text(Text="Amazon Family", SourceLanguageCode="en", TargetLanguageCode="fr")

print("Translated text: " + response.get('TranslatedText'))

print("\n")

print("Translating 'Amazon Family' from English to French with the 'my-first-terminology' terminology...")

response = translate.translate_text(Text="Amazon Family", TerminologyNames=["my-first-terminology"], SourceLanguageCode="en", TargetLanguageCode="fr")

print("Translated text: " + response.get('TranslatedText'))

print("\n")

# The terminology file 'my-updated-terminology.csv' has the following contents:

'''

en,fr

Amazon Family,Amazon Famille

Prime Video, Prime Video

'''

# Read the terminology from a local file

with open('/tmp/my-updated-terminology.csv', 'rb') as f:

data = f.read()

file_data = bytearray(data)

print("Updating the imported terminology in Amazon Translate...")

response = translate.import_terminology(Name='my-first-terminology', MergeStrategy='OVERWRITE', TerminologyData={"File": file_data, "Format": 'CSV'})

print("Terminology updated: "),

print(response.get('TerminologyProperties'))

print("\n")

print("Translating 'Prime Video' from English to French with no terminology...")

response = translate.translate_text(Text="Prime Video", SourceLanguageCode="en", TargetLanguageCode="fr")

print("Translated text: " + response.get('TranslatedText'))

print("\n")

print("Translating 'Prime Video' from English to French with the 'my-first-terminology' terminology...")

response = translate.translate_text(Text="Prime Video", TerminologyNames=["my-first-terminology"], SourceLanguageCode="en", TargetLanguageCode="fr")

print("Translated text: " + response.get('TranslatedText'))

print("\n")

print("Cleaning up by deleting 'my-first-terminology'...")

translate.delete_terminology(Name="my-first-terminology")

print("Terminology deleted.")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值