PutLexicon - Amazon Polly

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

PutLexicon

下列程式碼範例示範如何使用 Python (boto3) 型應用程式在 AWS 區域中儲存發音詞庫。

如需此操作的詳細資訊,請參閱 PutLexicon 的參考API。

注意下列事項:

  • 您需要提供本機語彙檔名和儲存的語彙名稱,以更新程式碼。

  • 此範例假設您在 pls 子目錄中建立語彙檔案。您需要適時更新路徑。

下列程式碼範例使用儲存在 AWS SDK組態檔案中的預設憑證。如需建立組態檔的資訊,請參閱「設定 AWS 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 AWS 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))