From fa24e776a31978a29e96ef61510c50507ece018d Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 15 Nov 2016 11:17:26 -0800 Subject: [PATCH] Update Language samples. (#254) * Update Language samples. * Address comments. --- .../google-cloud-language/samples/README.md | 31 ++++++------- .../samples/package.json | 13 ++---- .../samples/quickstart.js | 14 +++--- .../samples/test/quickstart.test.js | 44 ------------------- 4 files changed, 26 insertions(+), 76 deletions(-) delete mode 100644 packages/google-cloud-language/samples/test/quickstart.test.js diff --git a/packages/google-cloud-language/samples/README.md b/packages/google-cloud-language/samples/README.md index d7a0fddadd8..fb7ec573037 100644 --- a/packages/google-cloud-language/samples/README.md +++ b/packages/google-cloud-language/samples/README.md @@ -35,25 +35,26 @@ __Usage:__ `node analyze --help` ``` Commands: - sentimentFromString Detect the sentiment of a block of text. - sentimentFromFile Detect the sentiment of text in a GCS file. - entitiesFromString Detect the entities of a block of text. - entitiesFromFile Detect the entities of text in a GCS file. - syntaxFromString Detect the syntax of a block of text. - syntaxFromFile Detect the syntax of text in a GCS file. + sentimentOfText Detect the sentiment of a block of text. + sentimentInFile Detect the sentiment of text in a GCS file. + entitiesOfText Detect the entities of a block of text. + entitiesInFile Detect the entities of text in a GCS file. + syntaxOfText Detect the syntax of a block of text. + syntaxInFile Detect the syntax of text in a GCS file. Options: - --language, -l The language of the text. [string] - --type, -t Type of text [string] [choices: "text", "html"] [default: "text"] - --help Show help [boolean] + --help Show help [boolean] Examples: - node analyze sentimentFromString "President Obama is speaking at the White House." - node analyze sentimentFromFile my-bucket file.txt - node analyze entitiesFromString "

President Obama is speaking at the White House.

" -t html - node analyze entitiesFromFile my-bucket file.txt - node analyze syntaxFromString "President Obama is speaking at the White House." - node analyze syntaxFromFile my-bucket es_file.txt -l es + node analyze sentimentOfText "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 For more information, see https://cloud.google.com/natural-language/docs ``` diff --git a/packages/google-cloud-language/samples/package.json b/packages/google-cloud-language/samples/package.json index c5ac342fd00..f95e29b9fd0 100644 --- a/packages/google-cloud-language/samples/package.json +++ b/packages/google-cloud-language/samples/package.json @@ -5,17 +5,12 @@ "license": "Apache Version 2.0", "author": "Google Inc.", "scripts": { - "test": "mocha -R spec -t 120000 --require intelli-espower-loader ../test/_setup.js test/*.test.js", - "system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js" + "test": "cd ..; npm run st -- language/system-test/*" }, "dependencies": { - "@google-cloud/language": "^0.1.2", - "@google-cloud/storage": "^0.1.1", - "yargs": "^5.0.0" - }, - "devDependencies": { - "mocha": "^3.0.2", - "node-uuid": "^1.4.7" + "@google-cloud/language": "^0.6.0", + "@google-cloud/storage": "^0.4.0", + "yargs": "^6.4.0" }, "engines": { "node": ">=4.3.2" diff --git a/packages/google-cloud-language/samples/quickstart.js b/packages/google-cloud-language/samples/quickstart.js index ac595aa563b..fd40ed8a2b8 100644 --- a/packages/google-cloud-language/samples/quickstart.js +++ b/packages/google-cloud-language/samples/quickstart.js @@ -31,13 +31,11 @@ const languageClient = Language({ const text = 'Hello, world!'; // Detects the sentiment of the text -languageClient.detectSentiment(text, { verbose: true }, (err, sentiment) => { - if (err) { - console.error(err); - return; - } +languageClient.detectSentiment(text) + .then((results) => { + const sentiment = results[0]; - console.log('Text: %s', text); - console.log('Sentiment: %j', sentiment); -}); + console.log(`Text: ${text}`); + console.log(`Sentiment: ${sentiment}`); + }); // [END language_quickstart] diff --git a/packages/google-cloud-language/samples/test/quickstart.test.js b/packages/google-cloud-language/samples/test/quickstart.test.js deleted file mode 100644 index df0519c73c0..00000000000 --- a/packages/google-cloud-language/samples/test/quickstart.test.js +++ /dev/null @@ -1,44 +0,0 @@ -/** - * 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'; - -const proxyquire = require(`proxyquire`).noCallThru(); - -describe(`language:quickstart`, () => { - let languageMock, LanguageMock; - const error = new Error(`error`); - const text = 'Hello, world!'; - - before(() => { - languageMock = { - detectSentiment: sinon.stub().yields(error) - }; - LanguageMock = sinon.stub().returns(languageMock); - }); - - it(`should handle error`, () => { - proxyquire(`../quickstart`, { - '@google-cloud/language': LanguageMock - }); - - assert.equal(LanguageMock.calledOnce, true); - assert.deepEqual(LanguageMock.firstCall.args, [{ projectId: 'YOUR_PROJECT_ID' }]); - assert.equal(languageMock.detectSentiment.calledOnce, true); - assert.deepEqual(languageMock.detectSentiment.firstCall.args.slice(0, -1), [text, { verbose: true }]); - assert.equal(console.error.calledOnce, true); - assert.deepEqual(console.error.firstCall.args, [error]); - }); -});