Amazon Translate 사용하여 웹 페이지 번역하기 - Amazon Translate

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

Amazon Translate 사용하여 웹 페이지 번역하기

Amazon Translate Translate를 사용하여 웹 페이지의 콘텐츠를 번역할 수 있습니다. 다음 Java 프로그램에서는 지정된 웹 페이지를 영어에서 스페인어로 번역하고 번역된 결과가 포함된 HTML 파일을 생성합니다. 프로그램에는 두 개의 함수가 있습니다.

  • 소스 웹 페이지에서 데이터를 읽는 함수와 HTML 요소와 데이터를 분리한 다음 요소를 번역하기 위해 두 번째 함수를 호출합니다. 설명서 마지막에 HTML 파일로 결과를 작성합니다.

  • Amazon Translate 서비스를 호출하여 HTML 요소의 콘텐츠를 번역하는 함수입니다.

이 예제는 중첩 요소 없이 간단한 HTML 페이지에서 실행됩니다.

예제를 구성하려면
  1. AWS SDK for Java를 설치하고 구성합니다. SDK for Java 설치 지침은 설정을 참조하십시오AWS SDK for Java.

  2. jsoup Java HTML 파서를 설치합니다. 지침은 jsoup를 참조하십시오.

  3. 이 예제를 실행하는 데 필요한 최소 권한이 있는지 확인합니다. 필요한 권한 정책의 경우 Amazon Translate에 사용되는 자격 증명 기반 정책을 참조하십시오.

  4. 필요한 자격 증명을 설치하여 샘플을 실행합니다. 지침은 AWS SDK for Java개발자 안내서의AWS 자격 증명 및 개발 지역 설정을 참조하십시오.

  5. Java IDE에 새 프로젝트를 생성하고 소스 코드를 복사합니다.

  6. 리전 및 엔드포인트를 Amazon Translate 작업을 실행할 리전으로 변경합니다. Amazon Translate Translate가 지원되는 지역 목록은 AWS일반 참조의 AWS 리전 및 엔드포인트를 참조하십시오.

package com.amazonaws.translateweb; import com.amazonaws.auth.AWSCredentialsProviderChain; import com.amazonaws.auth.EnvironmentVariableCredentialsProvider; import com.amazonaws.auth.SystemPropertiesCredentialsProvider; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.translate.AmazonTranslate; import com.amazonaws.services.translate.AmazonTranslateClient; import com.amazonaws.services.translate.model.TranslateTextRequest; import com.amazonaws.services.translate.model.TranslateTextResult; import com.amazonaws.AmazonServiceException; import java.io.IOException; import java.io.PrintWriter; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class TranslateWebPage { public static void main(String[] args) throws InterruptedException { // Define the URL of the HTML content to translate String url = "http://example.com/source.html"; // Create credentials using a provider chain that will evaluate in order; // a) Any Java system properties // b) Any environment variables // c) Any profile file AWSCredentialsProviderChain DefaultAWSCredentialsProviderChain = new AWSCredentialsProviderChain( new SystemPropertiesCredentialsProvider(), new EnvironmentVariableCredentialsProvider(), new ProfileCredentialsProvider() ); // Create an endpoint configuration for the Translate service AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration( "endpoint", "region"); // Create a client for the Translate service AmazonTranslate translate = AmazonTranslateClient.builder() .withCredentials(DefaultAWSCredentialsProviderChain) .withEndpointConfiguration(endpointConfiguration).build(); // Record the beginning of translating the HTML content at the url System.out.println("Translating URL: " + url); // Create an empty HTML document to store the parsed data Document doc; try { // Retrieve the HTML located at the URL doc = Jsoup.connect(url).get(); // Select all of the elements in the HTML Elements eles = doc.select("*"); // For each element for (Element ele : eles) { // Translate the element translateElement(ele, translate); // If you encounter service throttling when translating large web // pages, you can request a service limit increase. For details, // see https://aws.amazon.com/premiumsupport/knowledge-center/manage-service-limits/, // or you can throttle your requests by inserting a sleep statement. // Thread.sleep(1000); } // Configure an output file for the translated HTML String fname = "output HTML file name"; PrintWriter pw = new PrintWriter(fname, "UTF-8"); // Write our translated HTML to the output file pw.println(doc); pw.close(); // Record that the file has been saved System.out.println("Saved file "+fname); // Catch any exceptions in retrieving the HTML } catch (IOException e1) { e1.printStackTrace(); } } // This function is used to translate each individual element public static void translateElement(Element ele, AmazonTranslate translate) { // Check if the element has any text if (!ele.ownText().isEmpty()) { // Retrieve the text of the HTML element String text = ele.ownText(); // Now translate the element's text try { // Translate from English to Spanish TranslateTextRequest request = new TranslateTextRequest() .withText(text) .withSourceLanguageCode("en") .withTargetLanguageCode("es"); // Retrieve the result TranslateTextResult result = translate.translateText(request); // Record the original and translated text System.out.println("Original text: " + text + " - Translated text: "+ result.getTranslatedText()); // Update the HTML element with the translated text ele.text(result.getTranslatedText()); // Catch any translation errors } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } } else { // We have found a non-text HTML element. No action required. } } }