Skip to content

Commit 003ed6e

Browse files
authored
Bring ML APIs up to standard. (#346)
1 parent 0c11ad0 commit 003ed6e

File tree

4 files changed

+141
-288
lines changed

4 files changed

+141
-288
lines changed

translate/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"dependencies": {
1111
"@google-cloud/translate": "0.8.0",
12-
"yargs": "6.6.0"
12+
"yargs": "7.0.2"
1313
},
1414
"engines": {
1515
"node": ">=4.3.2"

translate/quickstart.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,8 @@ translateClient.translate(text, target)
3939

4040
console.log(`Text: ${text}`);
4141
console.log(`Translation: ${translation}`);
42+
})
43+
.catch((err) => {
44+
console.error('ERROR:', err);
4245
});
4346
// [END translate_quickstart]

translate/translate.js

Lines changed: 79 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,125 +15,133 @@
1515

1616
'use strict';
1717

18-
const Translate = require('@google-cloud/translate');
19-
20-
// [START translate_detect_language]
21-
function detectLanguage (input) {
22-
// The text for which to detect language, e.g.:
23-
// input = 'Hello, world';
18+
function detectLanguage (text) {
19+
// [START translate_detect_language]
20+
// Imports the Google Cloud client library
21+
const Translate = require('@google-cloud/translate');
2422

2523
// Instantiates a client
2624
const translate = Translate();
2725

28-
// Detects the language. "input" can be a string for detecting the language of
26+
// The text for which to detect language, e.g. "Hello, world!"
27+
// const text = 'Hello, world!';
28+
29+
// Detects the language. "text" can be a string for detecting the language of
2930
// a single piece of text, or an array of strings for detecting the languages
3031
// of multiple texts.
31-
return translate.detect(input)
32+
translate.detect(text)
3233
.then((results) => {
3334
let detections = results[0];
34-
35-
if (!Array.isArray(detections)) {
36-
detections = [detections];
37-
}
35+
detections = Array.isArray(detections) ? detections : [detections];
3836

3937
console.log('Detections:');
4038
detections.forEach((detection) => {
4139
console.log(`${detection.input} => ${detection.language}`);
4240
});
43-
44-
return detections;
41+
})
42+
.catch((err) => {
43+
console.error('ERROR:', err);
4544
});
45+
// [END translate_detect_language]
4646
}
47-
// [END translate_detect_language]
4847

49-
// [START translate_list_codes]
5048
function listLanguages () {
49+
// [START translate_list_codes]
50+
// Imports the Google Cloud client library
51+
const Translate = require('@google-cloud/translate');
52+
5153
// Instantiates a client
5254
const translate = Translate();
5355

5456
// Lists available translation language with their names in English (the default).
55-
return translate.getLanguages()
57+
translate.getLanguages()
5658
.then((results) => {
5759
const languages = results[0];
5860

5961
console.log('Languages:');
6062
languages.forEach((language) => console.log(language));
61-
62-
return languages;
63+
})
64+
.catch((err) => {
65+
console.error('ERROR:', err);
6366
});
67+
// [END translate_list_codes]
6468
}
65-
// [END translate_list_codes]
6669

67-
// [START translate_list_language_names]
6870
function listLanguagesWithTarget (target) {
69-
// The target language for language names, e.g.:
70-
// target = 'ru';
71+
// [START translate_list_language_names]
72+
// Imports the Google Cloud client library
73+
const Translate = require('@google-cloud/translate');
7174

7275
// Instantiates a client
7376
const translate = Translate();
7477

75-
// Lists available translation language with their names in a target language,
76-
// e.g. "ru"
77-
return translate.getLanguages(target)
78+
// The target language for language names, e.g. "ru"
79+
// const target = 'ru';
80+
81+
// Lists available translation language with their names in a target language
82+
translate.getLanguages(target)
7883
.then((results) => {
7984
const languages = results[0];
8085

8186
console.log('Languages:');
8287
languages.forEach((language) => console.log(language));
83-
84-
return languages;
88+
})
89+
.catch((err) => {
90+
console.error('ERROR:', err);
8591
});
92+
// [END translate_list_language_names]
8693
}
87-
// [END translate_list_language_names]
8894

89-
// [START translate_translate_text]
90-
function translateText (input, target) {
91-
// The text to translate, e.g.:
92-
// input = 'Hello, world';
93-
// The target language, e.g.:
94-
// target = 'ru';
95-
96-
if (!Array.isArray(input)) {
97-
input = [input];
98-
}
95+
function translateText (text, target) {
96+
// [START translate_translate_text]
97+
// Imports the Google Cloud client library
98+
const Translate = require('@google-cloud/translate');
9999

100100
// Instantiates a client
101101
const translate = Translate();
102102

103-
// Translates the text into the target language. "input" can be a string for
103+
// The text to translate, e.g. "Hello, world!"
104+
// const text = 'Hello, world!';
105+
106+
// The target language, e.g. "ru"
107+
// const target = 'ru';
108+
109+
// Translates the text into the target language. "text" can be a string for
104110
// translating a single piece of text, or an array of strings for translating
105111
// multiple texts.
106-
return translate.translate(input, target)
112+
translate.translate(text, target)
107113
.then((results) => {
108114
let translations = results[0];
109115
translations = Array.isArray(translations) ? translations : [translations];
110116

111117
console.log('Translations:');
112118
translations.forEach((translation, i) => {
113-
console.log(`${input[i]} => (${target}) ${translation}`);
119+
console.log(`${text[i]} => (${target}) ${translation}`);
114120
});
115-
116-
return translations;
121+
})
122+
.catch((err) => {
123+
console.error('ERROR:', err);
117124
});
125+
// [END translate_translate_text]
118126
}
119-
// [END translate_translate_text]
120-
121-
// [START translate_text_with_model]
122-
function translateTextWithModel (input, target, model) {
123-
// The text to translate, e.g.:
124-
// input = 'Hello, world';
125-
// The target language, e.g.:
126-
// target = 'ru';
127-
// The model to use, e.g.:
128-
// model = 'nmt';
129127

130-
if (!Array.isArray(input)) {
131-
input = [input];
132-
}
128+
function translateTextWithModel (text, target, model) {
129+
// [START translate_text_with_model]
130+
// Imports the Google Cloud client library
131+
const Translate = require('@google-cloud/translate');
133132

134133
// Instantiates a client
135134
const translate = Translate();
136135

136+
// The text to translate, e.g. "Hello, world!"
137+
// const text = 'Hello, world!';
138+
139+
// The target language, e.g. "ru"
140+
// const target = 'ru';
141+
142+
// The model to use, e.g. "nmt"
143+
// const model = 'nmt';
144+
137145
const options = {
138146
// The target language, e.g. "ru"
139147
to: target,
@@ -142,35 +150,36 @@ function translateTextWithModel (input, target, model) {
142150
model: model
143151
};
144152

145-
// Translates the text into the target language. "input" can be a string for
153+
// Translates the text into the target language. "text" can be a string for
146154
// translating a single piece of text, or an array of strings for translating
147155
// multiple texts.
148-
return translate.translate(input, options)
156+
translate.translate(text, options)
149157
.then((results) => {
150158
let translations = results[0];
151159
translations = Array.isArray(translations) ? translations : [translations];
152160

153161
console.log('Translations:');
154162
translations.forEach((translation, i) => {
155-
console.log(`${input[i]} => (${target}) ${translation}`);
163+
console.log(`${text[i]} => (${target}) ${translation}`);
156164
});
157-
158-
return translations;
165+
})
166+
.catch((err) => {
167+
console.error('ERROR:', err);
159168
});
169+
// [END translate_text_with_model]
160170
}
161-
// [END translate_text_with_model]
162171

163172
require(`yargs`)
164173
.demand(1)
165174
.command(
166-
`detect <input..>`,
175+
`detect <text..>`,
167176
`Detects the language of one or more strings.`,
168177
{},
169-
(opts) => detectLanguage(opts.input)
178+
(opts) => detectLanguage(opts.text)
170179
)
171180
.command(
172181
`list [target]`,
173-
`Lists available translation languages. To return language names in a language other than English, specify a target language.`,
182+
`Lists available translation languages. To language names in a language other than English, specify a target language.`,
174183
{},
175184
(opts) => {
176185
if (opts.target) {
@@ -181,16 +190,16 @@ require(`yargs`)
181190
}
182191
)
183192
.command(
184-
`translate <toLang> <input..>`,
193+
`translate <toLang> <text..>`,
185194
`Translates one or more strings into the target language.`,
186195
{},
187-
(opts) => translateText(opts.input, opts.toLang)
196+
(opts) => translateText(opts.text, opts.toLang)
188197
)
189198
.command(
190-
`translate-with-model <toLang> <model> <input..>`,
199+
`translate-with-model <toLang> <model> <text..>`,
191200
`Translates one or more strings into the target language using the specified model.`,
192201
{},
193-
(opts) => translateTextWithModel(opts.input, opts.toLang, opts.model)
202+
(opts) => translateTextWithModel(opts.text, opts.toLang, opts.model)
194203
)
195204
.example(`node $0 detect "Hello world!"`, `Detects the language of a string.`)
196205
.example(`node $0 detect "Hello world!" "Goodbye"`, `Detects the languages of multiple strings.`)

0 commit comments

Comments
 (0)