Úselo ListLexicons con un AWS SDK o CLI - AWS Ejemplos de código de SDK

Hay más ejemplos de AWS SDK disponibles en el GitHub repositorio de ejemplos de AWS Doc SDK.

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

Úselo ListLexicons con un AWS SDK o CLI

En los siguientes ejemplos de código, se muestra cómo utilizar ListLexicons.

.NET
AWS SDK for .NET
nota

Hay más en marcha GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

using System; using System.Threading.Tasks; using Amazon.Polly; using Amazon.Polly.Model; /// <summary> /// Lists the Amazon Polly lexicons that have been defined. By default, /// lists the lexicons that are defined in the same AWS Region as the default /// user. To view Amazon Polly lexicons that are defined in a different AWS /// Region, supply it as a parameter to the Amazon Polly constructor. /// </summary> public class ListLexicons { public static async Task Main() { var client = new AmazonPollyClient(); var request = new ListLexiconsRequest(); try { Console.WriteLine("All voices: "); do { var response = await client.ListLexiconsAsync(request); request.NextToken = response.NextToken; response.Lexicons.ForEach(lexicon => { var attributes = lexicon.Attributes; Console.WriteLine($"Name: {lexicon.Name}"); Console.WriteLine($"\tAlphabet: {attributes.Alphabet}"); Console.WriteLine($"\tLanguageCode: {attributes.LanguageCode}"); Console.WriteLine($"\tLastModified: {attributes.LastModified}"); Console.WriteLine($"\tLexemesCount: {attributes.LexemesCount}"); Console.WriteLine($"\tLexiconArn: {attributes.LexiconArn}"); Console.WriteLine($"\tSize: {attributes.Size}"); }); } while (request.NextToken is not null); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } }
  • Para obtener más información sobre la API, consulta ListLexiconsla Referencia AWS SDK for .NET de la API.

CLI
AWS CLI

Creación de una lista de léxicos

En el siguiente ejemplo de list-lexicons se enumeran sus léxicos de pronunciación.

aws polly list-lexicons

Salida:

{ "Lexicons": [ { "Name": "w3c", "Attributes": { "Alphabet": "ipa", "LanguageCode": "en-US", "LastModified": 1603908910.99, "LexiconArn": "arn:aws:polly:us-east-2:123456789012:lexicon/w3c", "LexemesCount": 1, "Size": 492 } } ] }

Para obtener más información, consulte Uso de la ListLexicons operación en la Guía para desarrolladores de Amazon Polly.

  • Para obtener más información sobre la API, consulte ListLexiconsla Referencia de AWS CLI comandos.

Java
SDK para Java 2.x
nota

Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.polly.PollyClient; import software.amazon.awssdk.services.polly.model.ListLexiconsResponse; import software.amazon.awssdk.services.polly.model.ListLexiconsRequest; import software.amazon.awssdk.services.polly.model.LexiconDescription; import software.amazon.awssdk.services.polly.model.PollyException; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListLexicons { public static void main(String args[]) { PollyClient polly = PollyClient.builder() .region(Region.US_WEST_2) .build(); listLexicons(polly); polly.close(); } public static void listLexicons(PollyClient client) { try { ListLexiconsRequest listLexiconsRequest = ListLexiconsRequest.builder() .build(); ListLexiconsResponse listLexiconsResult = client.listLexicons(listLexiconsRequest); List<LexiconDescription> lexiconDescription = listLexiconsResult.lexicons(); for (LexiconDescription lexDescription : lexiconDescription) { System.out.println("The name of the Lexicon is " + lexDescription.name()); } } catch (PollyException e) { System.err.println("Exception caught: " + e); System.exit(1); } } }
  • Para obtener más información sobre la API, consulta ListLexiconsla Referencia AWS SDK for Java 2.x de la API.

Python
SDK para Python (Boto3)
nota

Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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 list_lexicons(self): """ Lists lexicons in the current account. :return: The list of lexicons. """ try: response = self.polly_client.list_lexicons() lexicons = response["Lexicons"] logger.info("Got %s lexicons.", len(lexicons)) except ClientError: logger.exception( "Couldn't get %s.", ) raise else: return lexicons
  • Para obtener más información sobre la API, consulta ListLexiconsla AWS Referencia de API de SDK for Python (Boto3).

Ruby
SDK para Ruby
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

require "aws-sdk-polly" # In v2: require 'aws-sdk' begin # Create an Amazon Polly client using # credentials from the shared credentials file ~/.aws/credentials # and the configuration (region) from the shared configuration file ~/.aws/config polly = Aws::Polly::Client.new resp = polly.list_lexicons resp.lexicons.each do |l| puts l.name puts " Alphabet:" + l.attributes.alphabet puts " Language:" + l.attributes.language puts end rescue StandardError => ex puts "Could not get lexicons" puts "Error message:" puts ex.message end
  • Para obtener más información sobre la API, consulta ListLexiconsla Referencia AWS SDK for Ruby de la API.

Rust
SDK para Rust
nota

Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

async fn show_lexicons(client: &Client) -> Result<(), Error> { let resp = client.list_lexicons().send().await?; println!("Lexicons:"); let lexicons = resp.lexicons(); for lexicon in lexicons { println!(" Name: {}", lexicon.name().unwrap_or_default()); println!( " Language: {:?}\n", lexicon .attributes() .as_ref() .map(|attrib| attrib .language_code .as_ref() .expect("languages must have language codes")) .expect("languages must have attributes") ); } println!(); println!("Found {} lexicons.", lexicons.len()); println!(); Ok(()) }
  • Para obtener más información sobre la API, consulta ListLexiconsla referencia sobre la API de AWS SDK para Rust.