용어집과 모델을 사용하여 대량의 텍스트를 번역합니다.
이 코드 샘플이 포함된 문서 페이지
컨텍스트에서 사용된 코드 샘플을 보려면 다음 문서를 참조하세요.
코드 샘플
Go
이 샘플을 사용해 보기 전에 Translation 빠른 시작: 클라이언트 라이브러리 사용의 Go 설정 안내를 따르세요. 자세한 내용은 Translation Go API 참조 문서를 확인하세요.
import (
"context"
"fmt"
"io"
translate "cloud.google.com/go/translate/apiv3"
translatepb "google.golang.org/genproto/googleapis/cloud/translate/v3"
)
// batchTranslateTextWithGlossaryAndModel translates a large volume of text in asynchronous batch mode.
func batchTranslateTextWithGlossaryAndModel(w io.Writer, projectID string, location string, inputURI string, outputURI string, sourceLang string, targetLang string, glossaryID string, modelID string) error {
// projectID := "my-project-id"
// location := "us-central1"
// inputURI := "gs://cloud-samples-data/text.txt"
// outputURI := "gs://YOUR_BUCKET_ID/path_to_store_results/"
// sourceLang := "en"
// targetLang := "ja"
// glossaryID := "your-glossary-id"
// modelID := "your-model-id"
ctx := context.Background()
client, err := translate.NewTranslationClient(ctx)
if err != nil {
return fmt.Errorf("NewTranslationClient: %v", err)
}
defer client.Close()
req := &translatepb.BatchTranslateTextRequest{
Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, location),
SourceLanguageCode: sourceLang,
TargetLanguageCodes: []string{targetLang},
InputConfigs: []*translatepb.InputConfig{
{
Source: &translatepb.InputConfig_GcsSource{
GcsSource: &translatepb.GcsSource{InputUri: inputURI},
},
// Optional. Can be "text/plain" or "text/html".
MimeType: "text/plain",
},
},
Glossaries: map[string]*translatepb.TranslateTextGlossaryConfig{
targetLang: {
Glossary: fmt.Sprintf("projects/%s/locations/%s/glossaries/%s", projectID, location, glossaryID),
},
},
OutputConfig: &translatepb.OutputConfig{
Destination: &translatepb.OutputConfig_GcsDestination{
GcsDestination: &translatepb.GcsDestination{
OutputUriPrefix: outputURI,
},
},
},
Models: map[string]string{
targetLang: fmt.Sprintf("projects/%s/locations/%s/models/%s", projectID, location, modelID),
},
}
// The BatchTranslateText operation is async.
op, err := client.BatchTranslateText(ctx, req)
if err != nil {
return fmt.Errorf("BatchTranslateText: %v", err)
}
fmt.Fprintf(w, "Processing operation name: %q\n", op.Name())
resp, err := op.Wait(ctx)
if err != nil {
return fmt.Errorf("Wait: %v", err)
}
fmt.Fprintf(w, "Total characters: %v\n", resp.GetTotalCharacters())
fmt.Fprintf(w, "Translated characters: %v\n", resp.GetTranslatedCharacters())
return nil
}
자바
이 샘플을 사용해 보기 전에 Translation 빠른 시작: 클라이언트 라이브러리 사용의 자바 설정 안내를 따르세요. 자세한 내용은 Translation Java API 참조 문서를 확인하세요.
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.translate.v3.BatchTranslateMetadata;
import com.google.cloud.translate.v3.BatchTranslateResponse;
import com.google.cloud.translate.v3.BatchTranslateTextRequest;
import com.google.cloud.translate.v3.GcsDestination;
import com.google.cloud.translate.v3.GcsSource;
import com.google.cloud.translate.v3.GlossaryName;
import com.google.cloud.translate.v3.InputConfig;
import com.google.cloud.translate.v3.LocationName;
import com.google.cloud.translate.v3.OutputConfig;
import com.google.cloud.translate.v3.TranslateTextGlossaryConfig;
import com.google.cloud.translate.v3.TranslationServiceClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class BatchTranslateTextWithGlossaryAndModel {
public static void batchTranslateTextWithGlossaryAndModel()
throws InterruptedException, ExecutionException, IOException, TimeoutException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "YOUR-PROJECT-ID";
// Supported Languages: https://cloud.google.com/translate/docs/languages
String sourceLanguage = "your-source-language";
String targetLanguage = "your-target-language";
String inputUri = "gs://your-gcs-bucket/path/to/input/file.txt";
String outputUri = "gs://your-gcs-bucket/path/to/results/";
String glossaryId = "your-glossary-display-name";
String modelId = "YOUR-MODEL-ID";
batchTranslateTextWithGlossaryAndModel(
projectId, sourceLanguage, targetLanguage, inputUri, outputUri, glossaryId, modelId);
}
// Batch translate text with Model and Glossary
public static void batchTranslateTextWithGlossaryAndModel(
String projectId,
String sourceLanguage,
String targetLanguage,
String inputUri,
String outputUri,
String glossaryId,
String modelId)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (TranslationServiceClient client = TranslationServiceClient.create()) {
// Supported Locations: `global`, [glossary location], or [model location]
// Glossaries must be hosted in `us-central1`
// Custom Models must use the same location as your model. (us-central1)
String location = "us-central1";
LocationName parent = LocationName.of(projectId, location);
// Configure the source of the file from a GCS bucket
GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build();
// Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats
InputConfig inputConfig =
InputConfig.newBuilder().setGcsSource(gcsSource).setMimeType("text/plain").build();
// Configure where to store the output in a GCS bucket
GcsDestination gcsDestination =
GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
OutputConfig outputConfig =
OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
// Configure the glossary used in the request
GlossaryName glossaryName = GlossaryName.of(projectId, location, glossaryId);
TranslateTextGlossaryConfig glossaryConfig =
TranslateTextGlossaryConfig.newBuilder().setGlossary(glossaryName.toString()).build();
// Configure the model used in the request
String modelPath =
String.format("projects/%s/locations/%s/models/%s", projectId, location, modelId);
// Build the request that will be sent to the API
BatchTranslateTextRequest request =
BatchTranslateTextRequest.newBuilder()
.setParent(parent.toString())
.setSourceLanguageCode(sourceLanguage)
.addTargetLanguageCodes(targetLanguage)
.addInputConfigs(inputConfig)
.setOutputConfig(outputConfig)
.putGlossaries(targetLanguage, glossaryConfig)
.putModels(targetLanguage, modelPath)
.build();
// Start an asynchronous request
OperationFuture<BatchTranslateResponse, BatchTranslateMetadata> future =
client.batchTranslateTextAsync(request);
System.out.println("Waiting for operation to complete...");
// random number between 300 - 450 (maximum allowed seconds)
long randomNumber = ThreadLocalRandom.current().nextInt(300, 450);
BatchTranslateResponse response = future.get(randomNumber, TimeUnit.SECONDS);
// Display the translation for each input text provided
System.out.printf("Total Characters: %s\n", response.getTotalCharacters());
System.out.printf("Translated Characters: %s\n", response.getTranslatedCharacters());
}
}
}
Node.js
이 샘플을 사용해 보기 전에 Translation 빠른 시작: 클라이언트 라이브러리 사용의 Node.js 설정 안내를 따르세요. 자세한 내용은 Translation Node.js API 참조 문서를 확인하세요.
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const inputUri = 'gs://cloud-samples-data/text.txt';
// const outputUri = 'gs://YOUR_BUCKET_ID/path_to_store_results/';
// const glossaryId = 'YOUR_GLOSSARY_ID';
// const modelId = 'YOUR_MODEL_ID';
// Imports the Google Cloud Translation library
const {TranslationServiceClient} = require('@google-cloud/translate');
// Instantiates a client
const client = new TranslationServiceClient();
async function batchTranslateTextWithGlossaryAndModel() {
// Construct request
const request = {
parent: `projects/${projectId}/locations/${location}`,
sourceLanguageCode: 'en',
targetLanguageCodes: ['ja'],
inputConfigs: [
{
mimeType: 'text/plain', // mime types: text/plain, text/html
gcsSource: {
inputUri: inputUri,
},
},
],
outputConfig: {
gcsDestination: {
outputUriPrefix: outputUri,
},
},
glossaries: {
ja: {
glossary: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`,
},
},
models: {
ja: `projects/${projectId}/locations/${location}/models/${modelId}`,
},
};
try {
const options = {timeout: 180000};
// Create a job using a long-running operation
const [operation] = await client.batchTranslateText(request, options);
// Wait for operation to complete
const [response] = await operation.promise();
// Display the translation for each input text provided
console.log(`Total Characters: ${response.totalCharacters}`);
console.log(`Translated Characters: ${response.translatedCharacters}`);
} catch (error) {
console.error(error.details);
}
}
batchTranslateTextWithGlossaryAndModel();
PHP
이 샘플을 사용해 보기 전에 Translation 빠른 시작: 클라이언트 라이브러리 사용의 PHP 설정 안내를 따르세요. 자세한 내용은 Translation PHP API 참조 문서를 확인하세요.
use Google\Cloud\Translate\V3\GcsDestination;
use Google\Cloud\Translate\V3\GcsSource;
use Google\Cloud\Translate\V3\InputConfig;
use Google\Cloud\Translate\V3\OutputConfig;
use Google\Cloud\Translate\V3\TranslateTextGlossaryConfig;
use Google\Cloud\Translate\V3\TranslationServiceClient;
$translationServiceClient = new TranslationServiceClient();
/** Uncomment and populate these variables in your code */
// $inputUri = 'gs://cloud-samples-data/text.txt';
// $outputUri = 'gs://YOUR_BUCKET_ID/path_to_store_results/';
// $projectId = '[Google Cloud Project ID]';
// $location = 'us-central1';
// $targetLanguage = 'en';
// $sourceLanguage = 'de';
// $modelId = '{your-model-id}';
// $glossaryId = '[YOUR_GLOSSARY_ID]';
$glossaryPath = $translationServiceClient->glossaryName(
$projectId,
$location,
$glossaryId
);
$modelPath = sprintf(
'projects/%s/locations/%s/models/%s',
$projectId,
$location,
$modelId
);
$targetLanguageCodes = [$targetLanguage];
$gcsSource = (new GcsSource())
->setInputUri($inputUri);
// Optional. Can be "text/plain" or "text/html".
$mimeType = 'text/plain';
$inputConfigsElement = (new InputConfig())
->setGcsSource($gcsSource)
->setMimeType($mimeType);
$inputConfigs = [$inputConfigsElement];
$gcsDestination = (new GcsDestination())
->setOutputUriPrefix($outputUri);
$outputConfig = (new OutputConfig())
->setGcsDestination($gcsDestination);
$formattedParent = $translationServiceClient->locationName($projectId, $location);
$models = ['ja' => $modelPath];
$glossariesItem = (new TranslateTextGlossaryConfig())
->setGlossary($glossaryPath);
$glossaries = ['ja' => $glossariesItem];
try {
$operationResponse = $translationServiceClient->batchTranslateText(
$formattedParent,
$sourceLanguage,
$targetLanguageCodes,
$inputConfigs,
$outputConfig,
[
'models' => $models,
'glossaries' => $glossaries
]
);
$operationResponse->pollUntilComplete();
if ($operationResponse->operationSucceeded()) {
$response = $operationResponse->getResult();
// Display the translation for each input text provided
printf('Total Characters: %s' . PHP_EOL, $response->getTotalCharacters());
printf('Translated Characters: %s' . PHP_EOL, $response->getTranslatedCharacters());
} else {
$error = $operationResponse->getError();
print($error->getMessage());
}
} finally {
$translationServiceClient->close();
}
Python
이 샘플을 사용해 보기 전에 Translation 빠른 시작: 클라이언트 라이브러리 사용의 Python 설정 안내를 따르세요. 자세한 내용은 Translation Python API 참조 문서를 확인하세요.
from google.cloud import translate
def batch_translate_text_with_glossary_and_model(
input_uri="gs://YOUR_BUCKET_ID/path/to/your/file.txt",
output_uri="gs://YOUR_BUCKET_ID/path/to/save/results/",
project_id="YOUR_PROJECT_ID",
model_id="YOUR_MODEL_ID",
glossary_id="YOUR_GLOSSARY_ID",
):
"""
Batch translate text with Glossary and Translation model
"""
client = translate.TranslationServiceClient()
# Supported language codes: https://cloud.google.com/translate/docs/languages
location = "us-central1"
target_language_codes = ["ja"]
gcs_source = {"input_uri": input_uri}
# Optional. Can be "text/plain" or "text/html".
mime_type = "text/plain"
input_configs_element = {"gcs_source": gcs_source, "mime_type": mime_type}
input_configs = [input_configs_element]
gcs_destination = {"output_uri_prefix": output_uri}
output_config = {"gcs_destination": gcs_destination}
parent = f"projects/{project_id}/locations/{location}"
model_path = "projects/{}/locations/{}/models/{}".format(
project_id, "us-central1", model_id
)
models = {"ja": model_path}
glossary_path = client.glossary_path(
project_id, "us-central1", glossary_id # The location of the glossary
)
glossary_config = translate.TranslateTextGlossaryConfig(glossary=glossary_path)
glossaries = {"ja": glossary_config} # target lang as key
operation = client.batch_translate_text(
request={
"parent": parent,
"source_language_code": "en",
"target_language_codes": target_language_codes,
"input_configs": input_configs,
"output_config": output_config,
"models": models,
"glossaries": glossaries,
}
)
print("Waiting for operation to complete...")
response = operation.result()
# Display the translation for each input text provided
print("Total Characters: {}".format(response.total_characters))
print("Translated Characters: {}".format(response.translated_characters))
Ruby
이 샘플을 사용해 보기 전에 Translation 빠른 시작: 클라이언트 라이브러리 사용의 Ruby 설정 안내를 따르세요. 자세한 내용은 Translation Ruby API 참조 문서를 확인하세요.
require "google/cloud/translate"
# input_uri = "gs://cloud-samples-data/text.txt"
# output_uri = "gs://YOUR_BUCKET_ID/path_to_store_results/"
# project_id = "[Google Cloud Project ID]"
# location_id = "[LOCATION ID]"
# model_id = "[MODEL ID]"
# glossary_id = "[YOUR_GLOSSARY_ID]"
source_lang = "en"
target_lang = "ja"
# Optional. Can be "text/plain" or "text/html".
mime_type = "text/plain"
client = Google::Cloud::Translate.translation_service
parent = client.location_path project: project_id, location: location_id
glossary_path = client.glossary_path project: project_id,
location: location_id,
glossary: glossary_id
model = "projects/#{project_id}/locations/#{location_id}/models/#{model_id}"
models = { target_lang => model }
glossaries = {
target_lang => { glossary: glossary_path }
}
input_config = {
mime_type: mime_type,
gcs_source: { input_uri: input_uri }
}
output_config = {
gcs_destination: { output_uri_prefix: output_uri }
}
operation = client.batch_translate_text(
parent: parent,
source_language_code: source_lang,
target_language_codes: [target_lang],
input_configs: [input_config],
output_config: output_config,
models: models,
glossaries: glossaries
)
# Wait until the long running operation is done
operation.wait_until_done!
response = operation.response
puts "Total Characters: #{response.total_characters}"
puts "Translated Characters: #{response.translated_characters}"