Skip to content

Commit f0a15e4

Browse files
committed
Update Translate samples, with some minor tweaks to Language samples.
1 parent 9280b68 commit f0a15e4

File tree

12 files changed

+272
-373
lines changed

12 files changed

+272
-373
lines changed

language/README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,30 @@ Learning API.
3131

3232
View the [documentation][analyze_docs] or the [source code][analyze_code].
3333

34-
__Usage:__ `node analyze --help`
34+
__Usage:__ `node analyze.js --help`
3535

3636
```
3737
Commands:
38-
sentimentOfText <text> Detect the sentiment of a block of text.
39-
sentimentInFile <bucket> <filename> Detect the sentiment of text in a GCS file.
40-
entitiesOfText <text> Detect the entities of a block of text.
41-
entitiesInFile <bucket> <filename> Detect the entities of text in a GCS file.
42-
syntaxOfText <text> Detect the syntax of a block of text.
43-
syntaxInFile <bucket> <filename> Detect the syntax of text in a GCS file.
38+
sentiment-text <text> Detects sentiment of a string.
39+
sentiment-file <bucket> <filename> Detects sentiment in a file in Google Cloud Storage.
40+
entities-text <text> Detects entities in a string.
41+
entities-file <bucket> <filename> Detects entities in a file in Google Cloud Storage.
42+
syntax-text <text> Detects syntax of a string.
43+
syntax-file <bucket> <filename> Detects syntax in a file in Google Cloud Storage.
4444
4545
Options:
4646
--help Show help [boolean]
4747
4848
Examples:
49-
node analyze sentimentOfText "President Obama is speaking at
49+
node analyze.js sentiment-text "President Obama is speaking
50+
at the White House."
51+
node analyze.js sentiment-file my-bucket file.txt Detects sentiment in gs://my-bucket/file.txt
52+
node analyze.js entities-text "President Obama is speaking
53+
at the White House."
54+
node analyze.js entities-file my-bucket file.txt Detects entities in gs://my-bucket/file.txt
55+
node analyze.js syntax-text "President Obama is speaking at
5056
the White House."
51-
node analyze sentimentInFile my-bucket file.txt
52-
node analyze entitiesOfText "President Obama is speaking at
53-
the White House."
54-
node analyze entitiesInFile my-bucket file.txt
55-
node analyze syntaxOfText "President Obama is speaking at
56-
the White House."
57-
node analyze syntaxInFile my-bucket file.txt
57+
node analyze.js syntax-file my-bucket file.txt Detects syntax in gs://my-bucket/file.txt
5858
5959
For more information, see https://cloud.google.com/natural-language/docs
6060
```

language/analyze.js

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ function analyzeSentimentOfText (text) {
3333
return document.detectSentiment()
3434
.then((results) => {
3535
const sentiment = results[0];
36+
3637
console.log(`Sentiment: ${sentiment >= 0 ? 'positive' : 'negative'}.`);
38+
3739
return sentiment;
3840
});
3941
}
@@ -60,7 +62,9 @@ function analyzeSentimentInFile (bucketName, fileName) {
6062
return document.detectSentiment()
6163
.then((results) => {
6264
const sentiment = results[0];
65+
6366
console.log(`Sentiment: ${sentiment >= 0 ? 'positive' : 'negative'}.`);
67+
6468
return sentiment;
6569
});
6670
}
@@ -81,10 +85,12 @@ function analyzeEntitiesOfText (text) {
8185
return document.detectEntities()
8286
.then((results) => {
8387
const entities = results[0];
88+
8489
console.log('Entities:');
8590
for (let type in entities) {
8691
console.log(`${type}:`, entities[type]);
8792
}
93+
8894
return entities;
8995
});
9096
}
@@ -111,10 +117,12 @@ function analyzeEntitiesInFile (bucketName, fileName) {
111117
return document.detectEntities()
112118
.then((results) => {
113119
const entities = results[0];
120+
114121
console.log('Entities:');
115122
for (let type in entities) {
116123
console.log(`${type}:`, entities[type]);
117124
}
125+
118126
return entities;
119127
});
120128
}
@@ -135,8 +143,10 @@ function analyzeSyntaxOfText (text) {
135143
return document.detectSyntax()
136144
.then((results) => {
137145
const syntax = results[0];
146+
138147
console.log('Tags:');
139148
syntax.forEach((part) => console.log(part.tag));
149+
140150
return syntax;
141151
});
142152
}
@@ -163,59 +173,62 @@ function analyzeSyntaxInFile (bucketName, fileName) {
163173
return document.detectSyntax()
164174
.then((results) => {
165175
const syntax = results[0];
176+
166177
console.log('Tags:');
167178
syntax.forEach((part) => console.log(part.tag));
179+
168180
return syntax;
169181
});
170182
}
171183
// [END language_syntax_file]
172184

173-
// The command-line program
174-
const cli = require(`yargs`);
175-
176-
const program = module.exports = {
177-
analyzeSentimentOfText,
178-
analyzeSentimentInFile,
179-
analyzeEntitiesOfText,
180-
analyzeEntitiesInFile,
181-
analyzeSyntaxOfText,
182-
analyzeSyntaxInFile,
183-
main: (args) => {
184-
// Run the command-line program
185-
cli.help().strict().parse(args).argv;
186-
}
187-
};
188-
189-
cli
185+
require(`yargs`)
190186
.demand(1)
191-
.command(`sentimentOfText <text>`, `Detect the sentiment of a block of text.`, {}, (opts) => {
192-
program.analyzeSentimentOfText(opts.text);
193-
})
194-
.command(`sentimentInFile <bucket> <filename>`, `Detect the sentiment of text in a GCS file.`, {}, (opts) => {
195-
program.analyzeSentimentInFile(opts.bucket, opts.filename);
196-
})
197-
.command(`entitiesOfText <text>`, `Detect the entities of a block of text.`, {}, (opts) => {
198-
program.analyzeEntitiesOfText(opts.text);
199-
})
200-
.command(`entitiesInFile <bucket> <filename>`, `Detect the entities of text in a GCS file.`, {}, (opts) => {
201-
program.analyzeEntitiesInFile(opts.bucket, opts.filename);
202-
})
203-
.command(`syntaxOfText <text>`, `Detect the syntax of a block of text.`, {}, (opts) => {
204-
program.analyzeSyntaxOfText(opts.text);
205-
})
206-
.command(`syntaxInFile <bucket> <filename>`, `Detect the syntax of text in a GCS file.`, {}, (opts) => {
207-
program.analyzeSyntaxInFile(opts.bucket, opts.filename);
208-
})
209-
.example(`node $0 sentimentOfText "President Obama is speaking at the White House."`, ``)
210-
.example(`node $0 sentimentInFile my-bucket file.txt`, ``)
211-
.example(`node $0 entitiesOfText "President Obama is speaking at the White House."`, ``)
212-
.example(`node $0 entitiesInFile my-bucket file.txt`, ``)
213-
.example(`node $0 syntaxOfText "President Obama is speaking at the White House."`, ``)
214-
.example(`node $0 syntaxInFile my-bucket file.txt`, ``)
187+
.command(
188+
`sentiment-text <text>`,
189+
`Detects sentiment of a string.`,
190+
{},
191+
(opts) => analyzeSentimentOfText(opts.text)
192+
)
193+
.command(
194+
`sentiment-file <bucket> <filename>`,
195+
`Detects sentiment in a file in Google Cloud Storage.`,
196+
{},
197+
(opts) => analyzeSentimentInFile(opts.bucket, opts.filename)
198+
)
199+
.command(
200+
`entities-text <text>`,
201+
`Detects entities in a string.`,
202+
{},
203+
(opts) => analyzeEntitiesOfText(opts.text)
204+
)
205+
.command(
206+
`entities-file <bucket> <filename>`,
207+
`Detects entities in a file in Google Cloud Storage.`,
208+
{},
209+
(opts) => analyzeEntitiesInFile(opts.bucket, opts.filename)
210+
)
211+
.command(
212+
`syntax-text <text>`,
213+
`Detects syntax of a string.`,
214+
{},
215+
(opts) => analyzeSyntaxOfText(opts.text)
216+
)
217+
.command(
218+
`syntax-file <bucket> <filename>`,
219+
`Detects syntax in a file in Google Cloud Storage.`,
220+
{},
221+
(opts) => analyzeSyntaxInFile(opts.bucket, opts.filename)
222+
)
223+
.example(`node $0 sentiment-text "President Obama is speaking at the White House."`)
224+
.example(`node $0 sentiment-file my-bucket file.txt`, `Detects sentiment in gs://my-bucket/file.txt`)
225+
.example(`node $0 entities-text "President Obama is speaking at the White House."`)
226+
.example(`node $0 entities-file my-bucket file.txt`, `Detects entities in gs://my-bucket/file.txt`)
227+
.example(`node $0 syntax-text "President Obama is speaking at the White House."`)
228+
.example(`node $0 syntax-file my-bucket file.txt`, `Detects syntax in gs://my-bucket/file.txt`)
215229
.wrap(120)
216230
.recommendCommands()
217-
.epilogue(`For more information, see https://cloud.google.com/natural-language/docs`);
218-
219-
if (module === require.main) {
220-
program.main(process.argv.slice(2));
221-
}
231+
.epilogue(`For more information, see https://cloud.google.com/natural-language/docs`)
232+
.help()
233+
.strict()
234+
.argv;

language/system-test/analyze.test.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
// Copyright 2016, Google, Inc.
2-
// Licensed under the Apache License, Version 2.0 (the "License");
3-
// you may not use this file except in compliance with the License.
4-
// You may obtain a copy of the License at
5-
//
6-
// http://www.apache.org/licenses/LICENSE-2.0
7-
//
8-
// Unless required by applicable law or agreed to in writing, software
9-
// distributed under the License is distributed on an "AS IS" BASIS,
10-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11-
// See the License for the specific language governing permissions and
12-
// limitations under the License.
1+
/**
2+
* Copyright 2016, Google, Inc.
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+
*/
1315

1416
'use strict';
1517

@@ -37,37 +39,37 @@ describe(`language:analyze`, () => {
3739
});
3840

3941
it(`should analyze sentiment in text`, () => {
40-
assert.equal(run(`${cmd} sentimentOfText "${text}"`, cwd), `Sentiment: positive.`);
42+
assert.equal(run(`${cmd} sentiment-text "${text}"`, cwd), `Sentiment: positive.`);
4143
});
4244

4345
it(`should analyze sentiment in a file`, () => {
44-
assert.equal(run(`${cmd} sentimentInFile ${bucketName} ${fileName}`, cwd), `Sentiment: positive.`);
46+
assert.equal(run(`${cmd} sentiment-file ${bucketName} ${fileName}`, cwd), `Sentiment: positive.`);
4547
});
4648

4749
it(`should analyze entities in text`, () => {
48-
const output = run(`${cmd} entitiesOfText "${text}"`, cwd);
50+
const output = run(`${cmd} entities-text "${text}"`, cwd);
4951
assert.equal(output.includes(`Entities:`), true);
5052
assert.equal(output.includes(`people:`), true);
5153
assert.equal(output.includes(`places:`), true);
5254
});
5355

5456
it('should analyze entities in a file', () => {
55-
const output = run(`${cmd} entitiesInFile ${bucketName} ${fileName}`, cwd);
57+
const output = run(`${cmd} entities-file ${bucketName} ${fileName}`, cwd);
5658
assert.equal(output.includes(`Entities:`), true);
5759
assert.equal(output.includes(`people:`), true);
5860
assert.equal(output.includes(`places:`), true);
5961
});
6062

6163
it(`should analyze syntax in text`, () => {
62-
const output = run(`${cmd} syntaxOfText "${text}"`, cwd);
64+
const output = run(`${cmd} syntax-text "${text}"`, cwd);
6365
assert.equal(output.includes(`Tags:`), true);
6466
assert.equal(output.includes(`NOUN`), true);
6567
assert.equal(output.includes(`VERB`), true);
6668
assert.equal(output.includes(`PUNCT`), true);
6769
});
6870

6971
it('should analyze syntax in a file', () => {
70-
const output = run(`${cmd} syntaxInFile ${bucketName} ${fileName}`, cwd);
72+
const output = run(`${cmd} syntax-file ${bucketName} ${fileName}`, cwd);
7173
assert.equal(output.includes(`Tags:`), true);
7274
assert.equal(output.includes(`NOUN`), true);
7375
assert.equal(output.includes(`VERB`), true);

language/system-test/quickstart.test.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ const proxyquire = require(`proxyquire`).noPreserveCache();
1919
const language = proxyquire(`@google-cloud/language`, {})();
2020

2121
describe(`language:quickstart`, () => {
22-
let languageMock, LanguageMock;
23-
2422
it(`should detect sentiment`, (done) => {
2523
const expectedText = `Hello, world!`;
26-
27-
languageMock = {
24+
const languageMock = {
2825
detectSentiment: (_text) => {
2926
assert.equal(_text, expectedText);
3027

@@ -34,20 +31,19 @@ describe(`language:quickstart`, () => {
3431
assert.equal(typeof sentiment, `number`);
3532

3633
setTimeout(() => {
37-
assert.equal(console.log.calledTwice, true);
38-
assert.deepEqual(console.log.firstCall.args, [`Text: ${expectedText}`]);
39-
assert.deepEqual(console.log.secondCall.args, [`Sentiment: ${sentiment}`]);
34+
assert.equal(console.log.callCount, 2);
35+
assert.deepEqual(console.log.getCall(0).args, [`Text: ${expectedText}`]);
36+
assert.deepEqual(console.log.getCall(1).args, [`Sentiment: ${sentiment}`]);
4037
done();
4138
}, 200);
4239

4340
return results;
4441
});
4542
}
4643
};
47-
LanguageMock = sinon.stub().returns(languageMock);
4844

4945
proxyquire(`../quickstart`, {
50-
'@google-cloud/language': LanguageMock
46+
'@google-cloud/language': sinon.stub().returns(languageMock)
5147
});
5248
});
5349
});

translate/README.md

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,29 @@ text between thousands of language pairs.
2929

3030
View the [documentation][translate_docs] or the [source code][translate_code].
3131

32-
__Usage:__ `node translate --help`
32+
__Usage:__ `node translate.js --help`
3333

3434
```
3535
Commands:
36-
detect <input..> Detect the language of the provided text or texts
37-
list [target] List available translation languages. To return language names in a language other than
38-
English, specify a target language.
39-
translate <toLang> <input..> Translate the provided text or texts to the target language, optionally specifying the
40-
source language.
36+
detect <input..> Detects the language of one or more strings.
37+
list [target] Lists available translation languages. To return
38+
language names in a language other thanEnglish,
39+
specify a target language.
40+
translate <toLang> <input..> Translates one or more strings into the target
41+
language.
4142
4243
Options:
43-
--apiKey, -k Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment variable. [string]
44-
--help Show help [boolean]
44+
--help Show help [boolean]
4545
4646
Examples:
47-
node translate detect "Hello world!" Detect the language of "Hello world!".
48-
node translate detect -k your-api-key "Hello world!" Detect the language of "Hello world!" and "Goodbye",
49-
"Goodbye" supplying the API key inline..
50-
node translate list -k your-api-key List available translation languages with names in
51-
English, supplying the API key inline..
52-
node translate list es List available translation languages with names in
47+
node translate.js detect "Hello world!" Detects the language of a string.
48+
node translate.js detect "Hello world!" "Goodbye" Detects the languages of multiple strings.
49+
node translate.js list Lists available translation languages with names in
50+
English.
51+
node translate.js list es Lists available translation languages with names in
5352
Spanish.
54-
node translate translate ru "Good morning!" Translate "Good morning!" to Russian, auto-detecting the
55-
source language.
56-
node translate translate ru "Good morning!" -f en -k Translate "Good morning!" to Russian from English,
57-
your-api-key supplying the API key inline.
53+
node translate.js translate ru "Good morning!" Translates a string into Russian.
54+
node translate.js translate ru "Good morning!" "Good night!" Translates multiple strings into Russian.
5855
5956
For more information, see https://cloud.google.com/translate/docs
6057
```

translate/package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55
"license": "Apache Version 2.0",
66
"author": "Google Inc.",
77
"scripts": {
8-
"test": "mocha -R spec --require intelli-espower-loader ../test/_setup.js test/*.test.js",
9-
"system-test": "mocha -R spec --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
8+
"test": "cd ..; npm run st -- translate/system-test/*.test.js"
109
},
1110
"dependencies": {
12-
"@google-cloud/translate": "^0.4.0",
11+
"@google-cloud/translate": "^0.5.0",
1312
"yargs": "^6.4.0"
1413
},
15-
"devDependencies": {
16-
"mocha": "^3.1.2"
17-
},
1814
"engines": {
1915
"node": ">=4.3.2"
2016
}

0 commit comments

Comments
 (0)