-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ccb300
commit 9fc73d8
Showing
16 changed files
with
3,924 additions
and
3,056 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,181 +1,49 @@ | ||
# @google-cloud/language ([Beta][versioning]) | ||
> Cloud Natural Language Client Library for Node.js | ||
# Node.js Client for Google Cloud Natural Language API ([Beta](https://github.com/GoogleCloudPlatform/google-cloud-node#versioning)) | ||
|
||
*Looking for more Google APIs than just Natural Language? You might want to check out [`google-cloud`][google-cloud].* | ||
[Google Cloud Natural Language API][Product Documentation]: Google Cloud Natural Language API provides natural language understanding technologies to developers. Examples include sentiment analysis, entity recognition, and text annotations. | ||
- [Client Library Documentation][] | ||
- [Product Documentation][] | ||
|
||
- [API Documentation][gcloud-language-docs] | ||
- [Official Documentation][cloud-language-docs] | ||
## Quick Start | ||
In order to use this library, you first need to go through the following steps: | ||
|
||
1. [Select or create a Cloud Platform project.](https://console.cloud.google.com/project) | ||
2. [Enable the Google Cloud Natural Language API.](https://console.cloud.google.com/apis/api/language) | ||
3. [Setup Authentication.](https://googlecloudplatform.github.io/google-cloud-node/#/docs/google-cloud/master/guides/authentication) | ||
|
||
```sh | ||
$ npm install --save @google-cloud/language | ||
``` | ||
```js | ||
var language = require('@google-cloud/language')({ | ||
projectId: 'grape-spaceship-123', | ||
keyFilename: '/path/to/keyfile.json' | ||
}); | ||
|
||
// Get the entities from a sentence. | ||
language.detectEntities('Stephen of Michigan!', function(err, entities) { | ||
// entities = [ | ||
// { | ||
// name: 'Stephen', | ||
// type: 'PERSON', | ||
// metadata: { | ||
// mid: '/m/05d8y4q' | ||
// }, | ||
// salience: 0.7309288382530212, | ||
// mentions: [ | ||
// { | ||
// text: { | ||
// content: 'Stephen', | ||
// beginOffset: -1 | ||
// }, | ||
// type: 'PROPER' | ||
// } | ||
// ] | ||
// }, | ||
// // ... | ||
// ] | ||
}); | ||
|
||
// Create a document if you plan to run multiple detections. | ||
var document = language.document('Contributions welcome!'); | ||
|
||
// Analyze the sentiment of the document. | ||
document.detectSentiment(function(err, sentiment) { | ||
// sentiment = { | ||
// magnitude: 0.30000001192092896, | ||
// score: 0.30000001192092896 | ||
// } | ||
}); | ||
|
||
// Parse the syntax of the document. | ||
document.annotate(function(err, annotation) { | ||
// annotation = { | ||
// language: 'en', | ||
// sentiment: { | ||
// magnitude: 0.30000001192092896, | ||
// score: 0.30000001192092896 | ||
// }, | ||
// entities: [ | ||
// { | ||
// name: 'Contributions', | ||
// type: 'OTHER', | ||
// metadata: {}, | ||
// salience: 1, | ||
// mentions: [ | ||
// { | ||
// text: { | ||
// content: 'Contributions', | ||
// beginOffset: -1 | ||
// }, | ||
// type: 'COMMON' | ||
// } | ||
// ] | ||
// } | ||
// ], | ||
// sentences: [ | ||
// { | ||
// text: { | ||
// content: 'Contributions welcome!', | ||
// beginOffset: -1 | ||
// }, | ||
// sentiment: { | ||
// magnitude: 0.30000001192092896, | ||
// score: 0.30000001192092896 | ||
// } | ||
// } | ||
// ], | ||
// tokens: [ | ||
// { | ||
// text: { | ||
// content: 'Contributions', | ||
// beginOffset: -1 | ||
// }, | ||
// partOfSpeech: { | ||
// tag: 'NOUN', | ||
// aspect: 'ASPECT_UNKNOWN', | ||
// case: 'CASE_UNKNOWN', | ||
// form: 'FORM_UNKNOWN', | ||
// gender: 'GENDER_UNKNOWN', | ||
// mood: 'MOOD_UNKNOWN', | ||
// number: 'PLURAL', | ||
// person: 'PERSON_UNKNOWN', | ||
// proper: 'PROPER_UNKNOWN', | ||
// reciprocity: 'RECIPROCITY_UNKNOWN', | ||
// tense: 'TENSE_UNKNOWN', | ||
// voice: 'VOICE_UNKNOWN' | ||
// }, | ||
// dependencyEdge: { | ||
// headTokenIndex: 1, | ||
// label: 'NSUBJ' | ||
// }, | ||
// lemma: 'contribution' | ||
// }, | ||
// // ... | ||
// ] | ||
// } | ||
}); | ||
|
||
// Promises are also supported by omitting callbacks. | ||
document.annotate().then(function(data) { | ||
var annotations = data[0]; | ||
}); | ||
|
||
// It's also possible to integrate with third-party Promise libraries. | ||
var language = require('@google-cloud/language')({ | ||
promise: require('bluebird') | ||
}); | ||
### Installation | ||
``` | ||
|
||
|
||
## Authentication | ||
|
||
It's incredibly easy to get authenticated and start using Google's APIs. You can set your credentials on a global basis as well as on a per-API basis. See each individual API section below to see how you can auth on a per-API-basis. This is useful if you want to use different accounts for different Cloud services. | ||
|
||
### On Google Cloud Platform | ||
|
||
If you are running this client on Google Cloud Platform, we handle authentication for you with no configuration. You just need to make sure that when you [set up the GCE instance][gce-how-to], you add the correct scopes for the APIs you want to access. | ||
|
||
``` js | ||
var language = require('@google-cloud/language')(); | ||
// ...you're good to go! | ||
$ npm install --save @google-cloud/language | ||
``` | ||
|
||
### Elsewhere | ||
|
||
If you are not running this client on Google Cloud Platform, you need a Google Developers service account. To create a service account: | ||
|
||
1. Visit the [Google Developers Console][dev-console]. | ||
2. Create a new project or click on an existing project. | ||
3. Navigate to **APIs & auth** > **APIs section** and turn on the following APIs (you may need to enable billing in order to use these services): | ||
* Google Cloud Natural Language API | ||
4. Navigate to **APIs & auth** > **Credentials** and then: | ||
* If you want to use a new service account key, click on **Create credentials** and select **Service account key**. After the account key is created, you will be prompted to download the JSON key file that the library uses to authenticate your requests. | ||
* If you want to generate a new service account key for an existing service account, click on **Generate new JSON key** and download the JSON key file. | ||
|
||
``` js | ||
var projectId = process.env.GCLOUD_PROJECT; // E.g. 'grape-spaceship-123' | ||
|
||
var language = require('@google-cloud/language')({ | ||
projectId: projectId, | ||
|
||
// The path to your key file: | ||
keyFilename: '/path/to/keyfile.json' | ||
|
||
// Or the contents of the key file: | ||
credentials: require('./path/to/keyfile.json') | ||
}); | ||
|
||
// ...you're good to go! | ||
### Preview | ||
#### LanguageServiceClient | ||
```js | ||
var language = require('@google-cloud/language'); | ||
|
||
var client = language({ | ||
// optional auth parameters. | ||
}); | ||
|
||
var content = 'Hello, world!'; | ||
var type = language.v1.types.Document.Type.PLAIN_TEXT; | ||
var document = { | ||
content : content, | ||
type : type | ||
}; | ||
client.analyzeSentiment({document: document}).then(function(responses) { | ||
var response = responses[0]; | ||
// doThingsWith(response) | ||
}) | ||
.catch(function(err) { | ||
console.error(err); | ||
}); | ||
``` | ||
|
||
### Next Steps | ||
- Read the [Client Library Documentation][] for Google Cloud Natural Language API to see other available methods on the client. | ||
- Read the [Google Cloud Natural Language API Product documentation][Product Documentation] to learn more about the product and see How-to Guides. | ||
- View this [repository's main README](https://github.com/GoogleCloudPlatform/google-cloud-node/blob/master/README.md) to see the full list of Cloud APIs that we cover. | ||
|
||
[versioning]: https://github.com/GoogleCloudPlatform/google-cloud-node#versioning | ||
[google-cloud]: https://github.com/GoogleCloudPlatform/google-cloud-node/ | ||
[gce-how-to]: https://cloud.google.com/compute/docs/authentication#using | ||
[dev-console]: https://console.developers.google.com/project | ||
[gcloud-language-docs]: https://googlecloudplatform.github.io/google-cloud-node/#/docs/language | ||
[cloud-language-docs]: https://cloud.google.com/natural-language/docs | ||
[Client Library Documentation]: https://googlecloudplatform.github.io/google-cloud-node/#/docs/language | ||
[Product Documentation]: https://cloud.google.com/language |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/google-cloud-language/smoke-test/language_service_smoke_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2017, Google Inc. All rights reserved. | ||
* | ||
* 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'; | ||
|
||
describe('LanguageServiceSmokeTest', function() { | ||
|
||
it('successfully makes a call to the service', function(done) { | ||
var language = require('../src'); | ||
|
||
var client = language.v1({ | ||
// optional auth parameters. | ||
}); | ||
|
||
var content = 'Hello, world!'; | ||
var type = language.v1.types.Document.Type.PLAIN_TEXT; | ||
var document = { | ||
content : content, | ||
type : type | ||
}; | ||
client.analyzeSentiment({document: document}).then(function(responses) { | ||
var response = responses[0]; | ||
console.log(response); | ||
}) | ||
.then(done) | ||
.catch(done); | ||
}); | ||
}); |
Oops, something went wrong.