|
| 1 | +// Copyright 2016, Google, Inc. |
| 2 | +// |
| 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 | + |
| 15 | +/** |
| 16 | + * @fileoverview Command-line program to demonstrate how to call different |
| 17 | + * methods in Cloud Natural Language API. |
| 18 | + * |
| 19 | + * To run this example, install npm: |
| 20 | + * npm install |
| 21 | + * |
| 22 | + * You must also set up to authenticate with the Cloud APIs using your |
| 23 | + * project's service account credentials. See the README for details. |
| 24 | + * |
| 25 | + * To run: |
| 26 | + * node analyze.js <sentiment|entities|syntax> <text> |
| 27 | + * |
| 28 | + * Here is an example: |
| 29 | + * node analyze.js entities "President Obama is speaking at the White House." |
| 30 | + */ |
| 31 | +'use strict'; |
| 32 | + |
| 33 | +var google = require('googleapis'); |
| 34 | + |
| 35 | +var languageScopes = ['https://www.googleapis.com/auth/cloud-platform']; |
| 36 | + |
| 37 | +/** |
| 38 | + * Gets a client that is connected to the Google Cloud Natural Language API. |
| 39 | + */ |
| 40 | +function getLanguageService (callback) { |
| 41 | + google.auth.getApplicationDefault(function (err, authClient) { |
| 42 | + if (err) { |
| 43 | + return callback(err); |
| 44 | + } |
| 45 | + |
| 46 | + // Depending on the environment that provides the default credentials |
| 47 | + // (e.g. Compute Engine, App Engine), the credentials retrieved may |
| 48 | + // require you to specify the scopes you need explicitly. |
| 49 | + if (authClient.createScopedRequired && authClient.createScopedRequired()) { |
| 50 | + authClient = authClient.createScoped(languageScopes); |
| 51 | + } |
| 52 | + |
| 53 | + // Load the discovery document for the natural language api service, using |
| 54 | + // the acquired credentials. |
| 55 | + console.log('Loading language service...'); |
| 56 | + google.discoverAPI({ |
| 57 | + url: 'https://language.googleapis.com/$discovery/rest', |
| 58 | + version: 'v1beta1', |
| 59 | + auth: authClient |
| 60 | + }, function (err, languageService) { |
| 61 | + if (err) { |
| 62 | + return callback(err); |
| 63 | + } |
| 64 | + callback(null, languageService, authClient); |
| 65 | + }); |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +function analyzeSentiment (inputText, languageService, authClient, callback) { |
| 70 | + languageService.documents.analyzeSentiment( |
| 71 | + { |
| 72 | + auth: authClient, |
| 73 | + resource: { // Resource is used as the body for the API call. |
| 74 | + document: { |
| 75 | + content: inputText, |
| 76 | + type: 'PLAIN_TEXT' |
| 77 | + } |
| 78 | + } |
| 79 | + }, |
| 80 | + function (err, result) { |
| 81 | + if (err) { |
| 82 | + return callback(err); |
| 83 | + } |
| 84 | + callback(null, result); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +function analyzeEntities (inputText, languageService, authClient, callback) { |
| 89 | + languageService.documents.analyzeEntities( |
| 90 | + { |
| 91 | + auth: authClient, |
| 92 | + resource: { // Resource is used as the body for the API call. |
| 93 | + document: { |
| 94 | + content: inputText, |
| 95 | + type: 'PLAIN_TEXT' |
| 96 | + }, |
| 97 | + encoding_type: 'UTF16' |
| 98 | + } |
| 99 | + }, |
| 100 | + function (err, result) { |
| 101 | + if (err) { |
| 102 | + return callback(err); |
| 103 | + } |
| 104 | + callback(null, result); |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +function analyzeSyntax (inputText, languageService, authClient, callback) { |
| 109 | + languageService.documents.annotateText( |
| 110 | + { |
| 111 | + auth: authClient, |
| 112 | + resource: { // Resource is used as the body for the API call. |
| 113 | + document: { |
| 114 | + content: inputText, |
| 115 | + type: 'PLAIN_TEXT' |
| 116 | + }, |
| 117 | + features: { |
| 118 | + extract_syntax: 'TRUE' |
| 119 | + }, |
| 120 | + encoding_type: 'UTF16' |
| 121 | + } |
| 122 | + }, |
| 123 | + function (err, result) { |
| 124 | + if (err) { |
| 125 | + return callback(err); |
| 126 | + } |
| 127 | + callback(null, result); |
| 128 | + }); |
| 129 | +} |
| 130 | + |
| 131 | +// Run the examples. |
| 132 | +exports.main = function (command, inputText, callback) { |
| 133 | + getLanguageService(function (err, languageService, authClient) { |
| 134 | + if (err) { |
| 135 | + return callback(err); |
| 136 | + } |
| 137 | + |
| 138 | + var resultCallback = function (err, result) { |
| 139 | + if (err) { |
| 140 | + return callback(err); |
| 141 | + } |
| 142 | + callback(null, result); |
| 143 | + }; |
| 144 | + if (command === 'sentiment') { |
| 145 | + analyzeSentiment(inputText, languageService, authClient, resultCallback); |
| 146 | + } else if (command === 'entities') { |
| 147 | + analyzeEntities(inputText, languageService, authClient, resultCallback); |
| 148 | + } else if (command === 'syntax') { |
| 149 | + analyzeSyntax(inputText, languageService, authClient, resultCallback); |
| 150 | + } else { |
| 151 | + return callback(err); |
| 152 | + } |
| 153 | + }); |
| 154 | +}; |
| 155 | + |
| 156 | +if (require.main === module) { |
| 157 | + var args = process.argv.slice(2); |
| 158 | + if (args.length !== 2) { |
| 159 | + console.log('Incorrect number of arguments. ' + |
| 160 | + 'Usage: node analyze.js <sentiment|entities|syntax> <text>'); |
| 161 | + process.exit(1); |
| 162 | + } |
| 163 | + if (['sentiment', 'entities', 'syntax'].indexOf(args[0]) === -1) { |
| 164 | + console.log('Incorrect command. ' + |
| 165 | + 'Usage: node analyze.js <sentiment|entities|syntax> <text>'); |
| 166 | + process.exit(1); |
| 167 | + } |
| 168 | + exports.main(args[0], args[1], function (result) { |
| 169 | + console.log(JSON.stringify(result, null, ' ')); |
| 170 | + }); |
| 171 | +} |
0 commit comments