루비에 사용하는 SDK Amazon Polly 예제 - AWS SDK코드 예제

AWS 문서 AWS SDK SDK 예제 GitHub 리포지토리에 더 많은 예제가 있습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

루비에 사용하는 SDK Amazon Polly 예제

다음 코드 예제는 Amazon Polly와 AWS SDK for Ruby 함께 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.

작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 호출하는 방법을 보여 주며 관련 시나리오와 교차 서비스 예시에서 컨텍스트에 맞는 작업을 볼 수 있습니다.

시나리오는 동일한 서비스 내에서 여러 함수를 호출하여 특정 태스크를 수행하는 방법을 보여주는 코드 예시입니다.

각 예제에는 컨텍스트에서 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있는 링크가 포함되어 있습니다. GitHub

주제

작업

다음 코드 예시에서는 DescribeVoices을 사용하는 방법을 보여 줍니다.

SDKRuby의 경우
참고

더 많은 정보가 있습니다. 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 # Get US English voices resp = polly.describe_voices(language_code: "en-US") resp.voices.each do |v| puts v.name puts " " + v.gender puts end rescue StandardError => ex puts "Could not get voices" puts "Error message:" puts ex.message end

다음 코드 예시에서는 ListLexicons을 사용하는 방법을 보여 줍니다.

SDK루비의 경우
참고

더 많은 정보가 있습니다. 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

다음 코드 예시에서는 SynthesizeSpeech을 사용하는 방법을 보여 줍니다.

SDK루비의 경우
참고

더 많은 정보가 있습니다. GitHub AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

require "aws-sdk-polly" # In v2: require 'aws-sdk' begin # Get the filename from the command line if ARGV.empty? puts "You must supply a filename" exit 1 end filename = ARGV[0] # Open file and get the contents as a string if File.exist?(filename) contents = IO.read(filename) else puts "No such file: " + filename exit 1 end # 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.synthesize_speech({ output_format: "mp3", text: contents, voice_id: "Joanna", }) # Save output # Get just the file name # abc/xyz.txt -> xyx.txt name = File.basename(filename) # Split up name so we get just the xyz part parts = name.split(".") first_part = parts[0] mp3_file = first_part + ".mp3" IO.copy_stream(resp.audio_stream, mp3_file) puts "Wrote MP3 content to: " + mp3_file rescue StandardError => ex puts "Got error:" puts "Error message:" puts ex.message end