Skip to content

Commit

Permalink
Update Translate samples, with some minor tweaks to Language samples. (
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry authored and NimJay committed Nov 10, 2022
1 parent 08de734 commit 4547c40
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 73 deletions.
105 changes: 59 additions & 46 deletions cloud-language/snippets/analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ function analyzeSentimentOfText (text) {
return document.detectSentiment()
.then((results) => {
const sentiment = results[0];

console.log(`Sentiment: ${sentiment >= 0 ? 'positive' : 'negative'}.`);

return sentiment;
});
}
Expand All @@ -60,7 +62,9 @@ function analyzeSentimentInFile (bucketName, fileName) {
return document.detectSentiment()
.then((results) => {
const sentiment = results[0];

console.log(`Sentiment: ${sentiment >= 0 ? 'positive' : 'negative'}.`);

return sentiment;
});
}
Expand All @@ -81,10 +85,12 @@ function analyzeEntitiesOfText (text) {
return document.detectEntities()
.then((results) => {
const entities = results[0];

console.log('Entities:');
for (let type in entities) {
console.log(`${type}:`, entities[type]);
}

return entities;
});
}
Expand All @@ -111,10 +117,12 @@ function analyzeEntitiesInFile (bucketName, fileName) {
return document.detectEntities()
.then((results) => {
const entities = results[0];

console.log('Entities:');
for (let type in entities) {
console.log(`${type}:`, entities[type]);
}

return entities;
});
}
Expand All @@ -135,8 +143,10 @@ function analyzeSyntaxOfText (text) {
return document.detectSyntax()
.then((results) => {
const syntax = results[0];

console.log('Tags:');
syntax.forEach((part) => console.log(part.tag));

return syntax;
});
}
Expand All @@ -163,59 +173,62 @@ function analyzeSyntaxInFile (bucketName, fileName) {
return document.detectSyntax()
.then((results) => {
const syntax = results[0];

console.log('Tags:');
syntax.forEach((part) => console.log(part.tag));

return syntax;
});
}
// [END language_syntax_file]

// The command-line program
const cli = require(`yargs`);

const program = module.exports = {
analyzeSentimentOfText,
analyzeSentimentInFile,
analyzeEntitiesOfText,
analyzeEntitiesInFile,
analyzeSyntaxOfText,
analyzeSyntaxInFile,
main: (args) => {
// Run the command-line program
cli.help().strict().parse(args).argv;
}
};

cli
require(`yargs`)
.demand(1)
.command(`sentimentOfText <text>`, `Detect the sentiment of a block of text.`, {}, (opts) => {
program.analyzeSentimentOfText(opts.text);
})
.command(`sentimentInFile <bucket> <filename>`, `Detect the sentiment of text in a GCS file.`, {}, (opts) => {
program.analyzeSentimentInFile(opts.bucket, opts.filename);
})
.command(`entitiesOfText <text>`, `Detect the entities of a block of text.`, {}, (opts) => {
program.analyzeEntitiesOfText(opts.text);
})
.command(`entitiesInFile <bucket> <filename>`, `Detect the entities of text in a GCS file.`, {}, (opts) => {
program.analyzeEntitiesInFile(opts.bucket, opts.filename);
})
.command(`syntaxOfText <text>`, `Detect the syntax of a block of text.`, {}, (opts) => {
program.analyzeSyntaxOfText(opts.text);
})
.command(`syntaxInFile <bucket> <filename>`, `Detect the syntax of text in a GCS file.`, {}, (opts) => {
program.analyzeSyntaxInFile(opts.bucket, opts.filename);
})
.example(`node $0 sentimentOfText "President Obama is speaking at the White House."`, ``)
.example(`node $0 sentimentInFile my-bucket file.txt`, ``)
.example(`node $0 entitiesOfText "President Obama is speaking at the White House."`, ``)
.example(`node $0 entitiesInFile my-bucket file.txt`, ``)
.example(`node $0 syntaxOfText "President Obama is speaking at the White House."`, ``)
.example(`node $0 syntaxInFile my-bucket file.txt`, ``)
.command(
`sentiment-text <text>`,
`Detects sentiment of a string.`,
{},
(opts) => analyzeSentimentOfText(opts.text)
)
.command(
`sentiment-file <bucket> <filename>`,
`Detects sentiment in a file in Google Cloud Storage.`,
{},
(opts) => analyzeSentimentInFile(opts.bucket, opts.filename)
)
.command(
`entities-text <text>`,
`Detects entities in a string.`,
{},
(opts) => analyzeEntitiesOfText(opts.text)
)
.command(
`entities-file <bucket> <filename>`,
`Detects entities in a file in Google Cloud Storage.`,
{},
(opts) => analyzeEntitiesInFile(opts.bucket, opts.filename)
)
.command(
`syntax-text <text>`,
`Detects syntax of a string.`,
{},
(opts) => analyzeSyntaxOfText(opts.text)
)
.command(
`syntax-file <bucket> <filename>`,
`Detects syntax in a file in Google Cloud Storage.`,
{},
(opts) => analyzeSyntaxInFile(opts.bucket, opts.filename)
)
.example(`node $0 sentiment-text "President Obama is speaking at the White House."`)
.example(`node $0 sentiment-file my-bucket file.txt`, `Detects sentiment in gs://my-bucket/file.txt`)
.example(`node $0 entities-text "President Obama is speaking at the White House."`)
.example(`node $0 entities-file my-bucket file.txt`, `Detects entities in gs://my-bucket/file.txt`)
.example(`node $0 syntax-text "President Obama is speaking at the White House."`)
.example(`node $0 syntax-file my-bucket file.txt`, `Detects syntax in gs://my-bucket/file.txt`)
.wrap(120)
.recommendCommands()
.epilogue(`For more information, see https://cloud.google.com/natural-language/docs`);

if (module === require.main) {
program.main(process.argv.slice(2));
}
.epilogue(`For more information, see https://cloud.google.com/natural-language/docs`)
.help()
.strict()
.argv;
38 changes: 20 additions & 18 deletions cloud-language/snippets/system-test/analyze.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// Copyright 2016, 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.
/**
* Copyright 2016, 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';

Expand Down Expand Up @@ -37,37 +39,37 @@ describe(`language:analyze`, () => {
});

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

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

it(`should analyze entities in text`, () => {
const output = run(`${cmd} entitiesOfText "${text}"`, cwd);
const output = run(`${cmd} entities-text "${text}"`, cwd);
assert.equal(output.includes(`Entities:`), true);
assert.equal(output.includes(`people:`), true);
assert.equal(output.includes(`places:`), true);
});

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

it(`should analyze syntax in text`, () => {
const output = run(`${cmd} syntaxOfText "${text}"`, cwd);
const output = run(`${cmd} syntax-text "${text}"`, cwd);
assert.equal(output.includes(`Tags:`), true);
assert.equal(output.includes(`NOUN`), true);
assert.equal(output.includes(`VERB`), true);
assert.equal(output.includes(`PUNCT`), true);
});

it('should analyze syntax in a file', () => {
const output = run(`${cmd} syntaxInFile ${bucketName} ${fileName}`, cwd);
const output = run(`${cmd} syntax-file ${bucketName} ${fileName}`, cwd);
assert.equal(output.includes(`Tags:`), true);
assert.equal(output.includes(`NOUN`), true);
assert.equal(output.includes(`VERB`), true);
Expand Down
14 changes: 5 additions & 9 deletions cloud-language/snippets/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ const proxyquire = require(`proxyquire`).noPreserveCache();
const language = proxyquire(`@google-cloud/language`, {})();

describe(`language:quickstart`, () => {
let languageMock, LanguageMock;

it(`should detect sentiment`, (done) => {
const expectedText = `Hello, world!`;

languageMock = {
const languageMock = {
detectSentiment: (_text) => {
assert.equal(_text, expectedText);

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

setTimeout(() => {
assert.equal(console.log.calledTwice, true);
assert.deepEqual(console.log.firstCall.args, [`Text: ${expectedText}`]);
assert.deepEqual(console.log.secondCall.args, [`Sentiment: ${sentiment}`]);
assert.equal(console.log.callCount, 2);
assert.deepEqual(console.log.getCall(0).args, [`Text: ${expectedText}`]);
assert.deepEqual(console.log.getCall(1).args, [`Sentiment: ${sentiment}`]);
done();
}, 200);

return results;
});
}
};
LanguageMock = sinon.stub().returns(languageMock);

proxyquire(`../quickstart`, {
'@google-cloud/language': LanguageMock
'@google-cloud/language': sinon.stub().returns(languageMock)
});
});
});

0 comments on commit 4547c40

Please sign in to comment.