Use PutLexicon with an AWS SDK or CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use PutLexicon with an AWS SDK or CLI

The following code examples show how to use PutLexicon.

.NET
AWS SDK for .NET
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

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); } } }
  • For API details, see PutLexicon in AWS SDK for .NET API Reference.

CLI
AWS CLI

To store a lexicon

The following put-lexicon example stores the specified pronunciation lexicon. The example.pls file specifies a W3C PLS-compliant lexicon.

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

Contents of 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> }

This command produces no output.

For more information, see Using the PutLexicon operation in the Amazon Polly Developer Guide.

  • For API details, see PutLexicon in AWS CLI Command Reference.

Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

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
  • For API details, see PutLexicon in AWS SDK for Python (Boto3) API Reference.

Rust
SDK for Rust
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

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(()) }
  • For API details, see PutLexicon in AWS SDK for Rust API reference.