|
| 1 | +/** |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +const {assert} = require('chai'); |
| 19 | +const {TranslationServiceClient} = require('@google-cloud/translate'); |
| 20 | +const {Storage} = require('@google-cloud/storage'); |
| 21 | +const cp = require('child_process'); |
| 22 | +const uuid = require('uuid'); |
| 23 | + |
| 24 | +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); |
| 25 | + |
| 26 | +const REGION_TAG = 'translate_batch_translate_text_with_glossary_and_model'; |
| 27 | + |
| 28 | +describe(REGION_TAG, () => { |
| 29 | + const translationClient = new TranslationServiceClient(); |
| 30 | + const location = 'us-central1'; |
| 31 | + const glossaryId = 'my-fake_glossary'; |
| 32 | + const modelId = 'TRL1218052175389786112'; |
| 33 | + const bucketUuid = uuid.v4(); |
| 34 | + const bucketName = `translation-${bucketUuid}/BATCH_TRANSLATION_OUTPUT/`; |
| 35 | + const storage = new Storage(); |
| 36 | + |
| 37 | + before(async () => { |
| 38 | + const projectId = await translationClient.getProjectId(); |
| 39 | + |
| 40 | + //Create bucket if needed |
| 41 | + await storage |
| 42 | + .createBucket(projectId, { |
| 43 | + location: 'US', |
| 44 | + storageClass: 'COLDLINE', |
| 45 | + }) |
| 46 | + .catch(error => { |
| 47 | + if (error.code !== 409) { |
| 48 | + //if it's not a duplicate bucket error, let the user know |
| 49 | + console.error(error); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + // Create glossary |
| 54 | + const request = { |
| 55 | + parent: `projects/${projectId}/locations/${location}`, |
| 56 | + glossary: { |
| 57 | + languageCodesSet: { |
| 58 | + languageCodes: ['en', 'ja'], |
| 59 | + }, |
| 60 | + inputConfig: { |
| 61 | + gcsSource: { |
| 62 | + inputUri: 'gs://cloud-samples-data/translation/glossary_ja.csv', |
| 63 | + }, |
| 64 | + }, |
| 65 | + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, |
| 66 | + }, |
| 67 | + }; |
| 68 | + |
| 69 | + // Create glossary using a long-running operation. |
| 70 | + const [operation] = await translationClient.createGlossary(request); |
| 71 | + // Wait for operation to complete. |
| 72 | + await operation.promise(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should batch translate the input text with a glossary', async () => { |
| 76 | + const projectId = await translationClient.getProjectId(); |
| 77 | + const inputUri = `gs://cloud-samples-data/translation/text_with_custom_model_and_glossary.txt`; |
| 78 | + |
| 79 | + const outputUri = `gs://${projectId}/${bucketName}`; |
| 80 | + const output = execSync( |
| 81 | + `node v3/${REGION_TAG}.js ${projectId} ${location} ${inputUri} ${outputUri} ${glossaryId} ${modelId}` |
| 82 | + ); |
| 83 | + assert.match(output, /Total Characters: 25/); |
| 84 | + assert.match(output, /Translated Characters: 25/); |
| 85 | + }); |
| 86 | + |
| 87 | + // Delete the folder from GCS for cleanup |
| 88 | + after(async function() { |
| 89 | + const projectId = await translationClient.getProjectId(); |
| 90 | + const options = { |
| 91 | + prefix: `translation-${bucketUuid}`, |
| 92 | + }; |
| 93 | + |
| 94 | + const bucket = await storage.bucket(projectId); |
| 95 | + const [files] = await bucket.getFiles(options); |
| 96 | + const length = files.length; |
| 97 | + if (length > 0) { |
| 98 | + await Promise.all(files.map(file => file.delete())); |
| 99 | + } |
| 100 | + |
| 101 | + // Delete the Glossary |
| 102 | + const request = { |
| 103 | + parent: `projects/${projectId}/locations/${location}`, |
| 104 | + name: `projects/${projectId}/locations/${location}/glossaries/${glossaryId}`, |
| 105 | + }; |
| 106 | + // Delete glossary using a long-running operation. |
| 107 | + const [operation] = await translationClient.deleteGlossary(request); |
| 108 | + // Wait for operation to complete. |
| 109 | + await operation.promise(); |
| 110 | + }); |
| 111 | +}); |
0 commit comments