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

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

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

PutLexicon与 AWS SDK 或 CLI 配合使用

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

.NET
AWS SDK for .NET
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整实例,了解如何进行设置和运行。

using System; using System.Threading.Tasks; using Amazon.Polly; using Amazon.Polly.Model; /// <summary> /// Creates a new Amazon Polly lexicon using the AWS SDK for .NET. /// </summary> public class PutLexicon { public static async Task Main() { string lexiconContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<lexicon version=\"1.0\" xmlns=\"http://www.w3.org/2005/01/pronunciation-lexicon\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xsi:schemaLocation=\"http://www.w3.org/2005/01/pronunciation-lexicon http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd\" " + "alphabet=\"ipa\" xml:lang=\"en-US\">" + "<lexeme><grapheme>test1</grapheme><alias>test2</alias></lexeme>" + "</lexicon>"; string lexiconName = "SampleLexicon"; var client = new AmazonPollyClient(); var putLexiconRequest = new PutLexiconRequest() { Name = lexiconName, Content = lexiconContent, }; try { var response = await client.PutLexiconAsync(putLexiconRequest); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { Console.WriteLine($"Successfully created Lexicon: {lexiconName}."); } else { Console.WriteLine($"Could not create Lexicon: {lexiconName}."); } } catch (Exception ex) { Console.WriteLine("Exception caught: " + ex.Message); } } }
  • 有关 API 的详细信息,请参阅 AWS SDK for .NET API 参考PutLexicon中的。

CLI
AWS CLI

存储词典

以下 put-lexicon 示例存储指定的发音词典。example.pls 文件指定符合 W3C PLS 标准的词典。

aws polly put-lexicon \ --name w3c \ --content file://example.pls

example.pls 的内容

{ <?xml version="1.0" encoding="UTF-8"?> <lexicon version="1.0" xmlns="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2005/01/pronunciation-lexicon http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd" alphabet="ipa" xml:lang="en-US"> <lexeme> <grapheme>W3C</grapheme> <alias>World Wide Web Consortium</alias> </lexeme> </lexicon> }

此命令不生成任何输出。

有关更多信息,请参阅 Amazon Polly 开发者指南中的使用 PutLexicon 操作

  • 有关 API 的详细信息,请参阅AWS CLI 命令参考PutLexicon中的。

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 create_lexicon(self, name, content): """ Creates a lexicon with the specified content. A lexicon contains custom pronunciations. :param name: The name of the lexicon. :param content: The content of the lexicon. """ try: self.polly_client.put_lexicon(Name=name, Content=content) logger.info("Created lexicon %s.", name) except ClientError: logger.exception("Couldn't create lexicon %s.") raise
  • 有关 API 的详细信息,请参阅适用PutLexiconPython 的AWS SDK (Boto3) API 参考

Rust
适用于 Rust 的 SDK
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整实例,了解如何进行设置和运行。

async fn make_lexicon(client: &Client, name: &str, from: &str, to: &str) -> Result<(), Error> { let content = format!("<?xml version=\"1.0\" encoding=\"UTF-8\"?> <lexicon version=\"1.0\" xmlns=\"http://www.w3.org/2005/01/pronunciation-lexicon\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.w3.org/2005/01/pronunciation-lexicon http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd\" alphabet=\"ipa\" xml:lang=\"en-US\"> <lexeme><grapheme>{}</grapheme><alias>{}</alias></lexeme> </lexicon>", from, to); client .put_lexicon() .name(name) .content(content) .send() .await?; println!("Added lexicon"); Ok(()) }
  • 有关 API 的详细信息,请参阅适用PutLexiconRust 的AWS SDK API 参考