搭ListLexicons配 AWS 開發套件或 CLI 使用 - AWS SDK 程式碼範例

AWS 文件 AWS SDK 範例 GitHub 存放庫中提供了更多 SDK 範例

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

ListLexicons配 AWS 開發套件或 CLI 使用

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

.NET
AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在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}"); } } }
  • 如需 API 詳細資訊,請參閱 AWS SDK for .NET API 參考ListLexicons中的。

CLI
AWS CLI

列出您的辭典

下列list-lexicons範例會列出您的發音詞彙。

aws polly list-lexicons

輸出:

{ "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 } } ] }

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

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

Java
適用於 Java 2.x 的 SDK
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在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); } } }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考ListLexicons中的。

Python
適用於 Python (Boto3) 的 SDK
注意

還有更多關於 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 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
  • 如需 API 的詳細資訊,請參閱AWS 開發套件ListLexicons中的 Python (博托 3) API 參考。

Ruby
適用於 Ruby 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在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
  • 如需 API 詳細資訊,請參閱 AWS SDK for Ruby API 參考ListLexicons中的。

Rust
適用於 Rust 的 SDK
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在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(()) }
  • 如需 API 的詳細資訊,請參閱 AWS SDK ListLexicons中的 Rust API 參考資料。