GetLexicon 搭配 a AWS SDK 或 CLI 使用 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

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

GetLexicon 搭配 a AWS SDK 或 CLI 使用

下列程式碼範例示範如何使用 GetLexicon

.NET
AWS SDK for .NET
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

using System; using System.Threading.Tasks; using Amazon.Polly; using Amazon.Polly.Model; /// <summary> /// Retrieves information about a specific Amazon Polly lexicon. /// </summary> public class GetLexicon { public static async Task Main(string[] args) { string lexiconName = "SampleLexicon"; var client = new AmazonPollyClient(); await GetPollyLexiconAsync(client, lexiconName); } public static async Task GetPollyLexiconAsync(AmazonPollyClient client, string lexiconName) { var getLexiconRequest = new GetLexiconRequest() { Name = lexiconName, }; try { var response = await client.GetLexiconAsync(getLexiconRequest); Console.WriteLine($"Lexicon:\n Name: {response.Lexicon.Name}"); Console.WriteLine($"Content: {response.Lexicon.Content}"); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } } }
  • 如需 API 詳細資訊,請參閱 GetLexicon AWS SDK for .NET 參考中的 API

CLI
AWS CLI

擷取詞庫的內容

下列get-lexicon範例會擷取指定發音詞庫的內容。

aws polly get-lexicon \ --name w3c

輸出:

{ "Lexicon": { "Content": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<lexicon version=\"1.0\" \n xmlns= \"http://www.w3.org/2005/01/pronunciation-lexicon\"\n xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n xsi:schemaLocation=\"http://www.w3.org/2005/01/pronunciation-lexicon \n http://www.w3.org/TR/2007/CR-pronunciation- lexicon-20071212/pls.xsd\"\n alphabet=\"ipa\" \n xml:lang=\"en-US\">\n <lexeme>\n <grapheme>W3C</grapheme>\n <alias>World Wide Web Consortium</alias>\n </lexeme>\n</lexicon>\n", "Name": "w3c" }, "LexiconAttributes": { "Alphabet": "ipa", "LanguageCode": "en-US", "LastModified": 1603908910.99, "LexiconArn": "arn:aws:polly:us-west-2:880185128111:lexicon/w3c", "LexemesCount": 1, "Size": 492 } }

如需詳細資訊,請參閱 Amazon Polly 開發人員指南中的使用 GetLexicon 操作

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetLexicon

Python
SDK for Python (Boto3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

class PollyWrapper: """Encapsulates Amazon Polly functions.""" def __init__(self, polly_client, s3_resource): """ :param polly_client: A Boto3 Amazon Polly client. :param s3_resource: A Boto3 Amazon Simple Storage Service (Amazon S3) resource. """ self.polly_client = polly_client self.s3_resource = s3_resource self.voice_metadata = None def get_lexicon(self, name): """ Gets metadata and contents of an existing lexicon. :param name: The name of the lexicon to retrieve. :return: The retrieved lexicon. """ try: response = self.polly_client.get_lexicon(Name=name) logger.info("Got lexicon %s.", name) except ClientError: logger.exception("Couldn't get lexicon %s.", name) raise else: return response
  • 如需 API 詳細資訊,請參閱 GetLexicon AWS SDK for Python (Boto3) Word 參考中的 API