-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): Translate with automl model sample (#238)
- Loading branch information
Showing
3 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
translate/test/v3beta1/translate_translate_text_with_model_beta.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright 2019, Google, Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const {assert} = require('chai'); | ||
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1; | ||
const cp = require('child_process'); | ||
|
||
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); | ||
const REGION_TAG = 'translate_translate_text_with_model_beta'; | ||
|
||
describe.skip(REGION_TAG, () => { | ||
const translationClient = new TranslationServiceClient(); | ||
const location = 'us-central1'; | ||
const modelId = 'TRL123456789'; //TODO: Create model that can be used for testing | ||
const input = 'Tell me how this ends'; | ||
|
||
it('should translate text with an automl model in project', async () => { | ||
const projectId = await translationClient.getProjectId(); | ||
const output = await execSync( | ||
`node v3beta1/${REGION_TAG}.js ${projectId} ${location} ${modelId} ${input}` | ||
); | ||
assert.match(output, /Translated Content: これがどのように終わるか教えて/); | ||
}); | ||
}); |
62 changes: 62 additions & 0 deletions
62
translate/v3beta1/translate_translate_text_with_model_beta.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* Copyright 2019, Google, Inc. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
function main( | ||
projectId = 'YOUR_PROJECT_ID', | ||
location = 'us-central1', | ||
modelId = 'model-id', | ||
text = 'text to translate' | ||
) { | ||
// [START translate_text_with_model_beta] | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// const projectId = 'YOUR_PROJECT_ID'; | ||
// const location = 'global'; | ||
// const modelId = 'YOUR_MODEL_ID'; | ||
|
||
// Imports the Google Cloud Translation library | ||
const {TranslationServiceClient} = require('@google-cloud/translate').v3beta1; | ||
const automl = require('@google-cloud/automl'); | ||
|
||
// Instantiates a client | ||
const translationClient = new TranslationServiceClient(); | ||
const autoMLClient = new automl.AutoMlClient(); | ||
async function translateTextWithModel() { | ||
const model = autoMLClient.modelPath(projectId, location, modelId); | ||
// Construct request | ||
const request = { | ||
parent: translationClient.locationPath(projectId, location), | ||
contents: [text], | ||
mimeType: 'text/plain', // mime types: text/plain, text/html | ||
sourceLanguageCode: 'en-US', | ||
targetLanguageCode: 'ja', | ||
model: model, | ||
}; | ||
|
||
// Run request | ||
const [response] = await translationClient.translateText(request); | ||
|
||
for (const translation of response.translations) { | ||
console.log(`Translated Content: ${translation.translatedText}`); | ||
} | ||
} | ||
|
||
translateTextWithModel(); | ||
// [END translate_text_with_model_beta] | ||
} | ||
|
||
main(...process.argv.slice(2)); |