Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Translate samples, with some minor tweaks to Language samples. #255

Merged
merged 1 commit into from
Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions language/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,30 @@ Learning API.

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

__Usage:__ `node analyze --help`
__Usage:__ `node analyze.js --help`

```
Commands:
sentimentOfText <text> Detect the sentiment of a block of text.
sentimentInFile <bucket> <filename> Detect the sentiment of text in a GCS file.
entitiesOfText <text> Detect the entities of a block of text.
entitiesInFile <bucket> <filename> Detect the entities of text in a GCS file.
syntaxOfText <text> Detect the syntax of a block of text.
syntaxInFile <bucket> <filename> Detect the syntax of text in a GCS file.
sentiment-text <text> Detects sentiment of a string.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The program doesn't throw an error if GOOGLE_APPLICATION_CREDENTIALS is not set. It silently swallows it and return nothing. We should fix this behavior.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the fault of the program, it's the fault of google-cloud-node. There's nothing I can fix about that in this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. I realized that now. But can we file an issue for those guys.

sentiment-file <bucket> <filename> Detects sentiment in a file in Google Cloud Storage.
entities-text <text> Detects entities in a string.
entities-file <bucket> <filename> Detects entities in a file in Google Cloud Storage.
syntax-text <text> Detects syntax of a string.
syntax-file <bucket> <filename> Detects syntax in a file in Google Cloud Storage.

Options:
--help Show help [boolean]

Examples:
node analyze sentimentOfText "President Obama is speaking at
node analyze.js sentiment-text "President Obama is speaking
at the White House."
node analyze.js sentiment-file my-bucket file.txt Detects sentiment in gs://my-bucket/file.txt
node analyze.js entities-text "President Obama is speaking
at the White House."
node analyze.js entities-file my-bucket file.txt Detects entities in gs://my-bucket/file.txt
node analyze.js syntax-text "President Obama is speaking at
the White House."
node analyze sentimentInFile my-bucket file.txt
node analyze entitiesOfText "President Obama is speaking at
the White House."
node analyze entitiesInFile my-bucket file.txt
node analyze syntaxOfText "President Obama is speaking at
the White House."
node analyze syntaxInFile my-bucket file.txt
node analyze.js syntax-file my-bucket file.txt Detects syntax in gs://my-bucket/file.txt

For more information, see https://cloud.google.com/natural-language/docs
```
Expand Down
105 changes: 59 additions & 46 deletions language/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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same is for entities. No entities printed on console.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be call it gcs_bucket here and everywhere to make it clear.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not seeing any output from $ node analyze sentiment-text "President Obama is speaking" for example.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)
.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 language/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 language/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)
});
});
});
33 changes: 15 additions & 18 deletions translate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,29 @@ text between thousands of language pairs.

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

__Usage:__ `node translate --help`
__Usage:__ `node translate.js --help`

```
Commands:
detect <input..> Detect the language of the provided text or texts
list [target] List available translation languages. To return language names in a language other than
English, specify a target language.
translate <toLang> <input..> Translate the provided text or texts to the target language, optionally specifying the
source language.
detect <input..> Detects the language of one or more strings.
list [target] Lists available translation languages. To return
language names in a language other thanEnglish,
specify a target language.
translate <toLang> <input..> Translates one or more strings into the target
language.

Options:
--apiKey, -k Your Translate API key. Defaults to the value of the TRANSLATE_API_KEY environment variable. [string]
--help Show help [boolean]
--help Show help [boolean]

Examples:
node translate detect "Hello world!" Detect the language of "Hello world!".
node translate detect -k your-api-key "Hello world!" Detect the language of "Hello world!" and "Goodbye",
"Goodbye" supplying the API key inline..
node translate list -k your-api-key List available translation languages with names in
English, supplying the API key inline..
node translate list es List available translation languages with names in
node translate.js detect "Hello world!" Detects the language of a string.
node translate.js detect "Hello world!" "Goodbye" Detects the languages of multiple strings.
node translate.js list Lists available translation languages with names in
English.
node translate.js list es Lists available translation languages with names in
Spanish.
node translate translate ru "Good morning!" Translate "Good morning!" to Russian, auto-detecting the
source language.
node translate translate ru "Good morning!" -f en -k Translate "Good morning!" to Russian from English,
your-api-key supplying the API key inline.
node translate.js translate ru "Good morning!" Translates a string into Russian.
node translate.js translate ru "Good morning!" "Good night!" Translates multiple strings into Russian.

For more information, see https://cloud.google.com/translate/docs
```
Expand Down
8 changes: 2 additions & 6 deletions translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
"license": "Apache Version 2.0",
"author": "Google Inc.",
"scripts": {
"test": "mocha -R spec --require intelli-espower-loader ../test/_setup.js test/*.test.js",
"system-test": "mocha -R spec --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
"test": "cd ..; npm run st -- translate/system-test/*.test.js"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'm not a fan of cding in tests, especially if this affects the user's current directory.

Copy link
Member Author

@jmdobry jmdobry Nov 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not affect the user's current directory, it just assigns a "current working directory" to the npm script that's being executed.

},
"dependencies": {
"@google-cloud/translate": "^0.4.0",
"@google-cloud/translate": "^0.5.0",
"yargs": "^6.4.0"
},
"devDependencies": {
"mocha": "^3.1.2"
},
"engines": {
"node": ">=4.3.2"
}
Expand Down
Loading