GetLexicon与 AWS SDK 或 CLI 配合使用 - AWS SDK 代码示例

文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例

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

GetLexicon与 AWS SDK 或 CLI 配合使用

以下代码示例演示如何使用 GetLexicon

.NET
AWS SDK for .NET
注意

还有更多相关信息 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 的详细信息,请参阅 AWS SDK for .NET API 参考GetLexicon中的。

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)
注意

还有更多相关信息 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 的详细信息,请参阅适用GetLexiconPython 的AWS SDK (Boto3) API 参考