PutLexicon - Amazon Polly
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

PutLexicon

以下代码示例显示如何使用基于 Python (boto3) 的应用程序将发音词典存储在 Amazon 区域中。

有关此操作的更多信息,请参阅 PutLexicon API 参考。

请注意以下几点:

  • 您需要通过提供本地词典文件名和存储词典名更新该代码。

  • 该示例假定您拥有在名为 pls 的子目录中创建的词典文件。您需要视情况新路径。

以下代码示例使用存储在 Amazon 软件开发工具包配置文件中的默认凭证。有关创建配置文件的信息,请参阅 步骤 2.1:设置 Amazon CLI

有关此操作的更多信息,请参阅 PutLexicon API 参考。

from argparse import ArgumentParser from boto3 import Session from botocore.exceptions import BotoCoreError, ClientError # Define and parse the command line arguments cli = ArgumentParser(description="PutLexicon example") cli.add_argument("path", type=str, metavar="FILE_PATH") cli.add_argument("-n", "--name", type=str, required=True, metavar="LEXICON_NAME", dest="name") arguments = cli.parse_args() # Create a client using the credentials and region defined in the adminuser # section of the Amazon credentials and configuration files session = Session(profile_name="adminuser") polly = session.client("polly") # Open the PLS lexicon file for reading try: with open(arguments.path, "r") as lexicon_file: # Read the pls file contents lexicon_data = lexicon_file.read() # Store the PLS lexicon on the service. # If a lexicon with that name already exists, # its contents will be updated response = polly.put_lexicon(Name=arguments.name, Content=lexicon_data) except (IOError, BotoCoreError, ClientError) as error: # Could not open/read the file or the service returned an error, # exit gracefully cli.error(error) print(u"The \"{0}\" lexicon is now available for use.".format(arguments.name))