Skip to content

Commit 00957c3

Browse files
stephenpluspluscallmehiphop
authored andcommitted
Introduce Natural Language API (#1440)
* language: implement API
1 parent b6433a1 commit 00957c3

File tree

15 files changed

+3028
-9
lines changed

15 files changed

+3028
-9
lines changed

README.md

+92-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This client supports the following Google Cloud Platform services:
2121
* [Google Prediction API](#google-prediction-api)
2222
* [Google Translate API](#google-translate-api)
2323
* [Google Cloud Logging](#google-cloud-logging-beta) (Beta)
24+
* [Google Cloud Natural Language](#google-cloud-natural-language-beta) (Beta)
2425
* [Google Cloud Resource Manager](#google-cloud-resource-manager-beta) (Beta)
2526
* [Google Cloud Vision](#google-cloud-vision-beta) (Beta)
2627

@@ -772,6 +773,90 @@ loggingClient.getEntries(function(err, entries) {
772773
```
773774

774775

776+
## Google Cloud Natural Language (Beta)
777+
778+
> **This is a Beta release of Google Cloud Natural Language.** This feature is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
779+
780+
- [API Documentation][gcloud-language-docs]
781+
- [Official Documentation][cloud-language-docs]
782+
783+
#### Using the all-in-one module
784+
785+
```
786+
$ npm install --save google-cloud
787+
```
788+
789+
```js
790+
var gcloud = require('google-cloud');
791+
var language = gcloud.language;
792+
```
793+
794+
#### Using the Natural Language API module
795+
796+
```
797+
$ npm install --save @google-cloud/language
798+
```
799+
800+
```js
801+
var language = require('@google-cloud/language');
802+
```
803+
804+
#### Preview
805+
806+
```js
807+
// Authenticating on a per-API-basis. You don't need to do this if you auth on a
808+
// global basis (see Authorization section above).
809+
810+
var languageClient = language({
811+
projectId: 'grape-spaceship-123',
812+
keyFilename: '/path/to/keyfile.json'
813+
});
814+
815+
// Get the entities from a sentence.
816+
languageClient.detectEntities('Stephen of Michigan!', function(err, entities) {
817+
// entities = {
818+
// people: ['Stephen'],
819+
// places: ['Michigan']
820+
// }
821+
});
822+
823+
// Create a document if you plan to run multiple detections.
824+
var document = languageClient.document('Contributions welcome!');
825+
826+
// Analyze the sentiment of the document.
827+
document.detectSentiment(function(err, sentiment) {
828+
// sentiment = 100 // Large numbers represent more positive sentiments.
829+
});
830+
831+
// Parse the syntax of the document.
832+
document.annotate(function(err, annotations) {
833+
// annotations = {
834+
// language: 'en',
835+
// sentiment: 100,
836+
// entities: {},
837+
// sentences: ['Contributions welcome!'],
838+
// tokens: [
839+
// {
840+
// text: 'Contributions',
841+
// partOfSpeech: 'Noun (common and proper)',
842+
// partOfSpeechTag: 'NOUN'
843+
// },
844+
// {
845+
// text: 'welcome',
846+
// partOfSpeech: 'Verb (all tenses and modes)',
847+
// partOfSpeechTag: 'VERB'
848+
// },
849+
// {
850+
// text: '!',
851+
// partOfSpeech: 'Punctuation',
852+
// partOfSpeechTag: 'PUNCT'
853+
// }
854+
// ]
855+
// }
856+
});
857+
```
858+
859+
775860
## Google Cloud Resource Manager (Beta)
776861

777862
> **This is a Beta release of Google Cloud Resource Manager.** This feature is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes.
@@ -803,8 +888,8 @@ var resource = require('@google-cloud/resource');
803888
#### Preview
804889

805890
```js
806-
// Authorizing on a per-API-basis. You don't need to do this if you auth on a
807-
// global basis (see Authorization section above).
891+
// Authenticating on a per-API-basis. You don't need to do this if you auth on a
892+
// global basis (see Authentication section above).
808893

809894
var resourceClient = resource({
810895
projectId: 'grape-spaceship-123',
@@ -858,8 +943,8 @@ var vision = require('@google-cloud/vision');
858943
#### Preview
859944

860945
```js
861-
// Authorizing on a per-API-basis. You don't need to do this if you auth on a
862-
// global basis (see Authorization section above).
946+
// Authenticating on a per-API-basis. You don't need to do this if you auth on a
947+
// global basis (see Authentication section above).
863948

864949
var visionClient = vision({
865950
projectId: 'grape-spaceship-123',
@@ -975,6 +1060,7 @@ Apache 2.0 - See [COPYING](COPYING) for more information.
9751060
[gcloud-compute-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/compute
9761061
[gcloud-datastore-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/datastore
9771062
[gcloud-dns-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/dns
1063+
[gcloud-language-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/language
9781064
[gcloud-logging-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/logging
9791065
[gcloud-prediction-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/prediction
9801066
[gcloud-pubsub-docs]: https://googlecloudplatform.github.io/gcloud-node/#/docs/pubsub
@@ -1011,6 +1097,8 @@ Apache 2.0 - See [COPYING](COPYING) for more information.
10111097

10121098
[cloud-dns-docs]: https://cloud.google.com/dns/docs
10131099

1100+
[cloud-language-docs]: https://cloud.google.com/natural-language/docs
1101+
10141102
[cloud-logging-docs]: https://cloud.google.com/logging/docs
10151103

10161104
[cloud-prediction-docs]: https://cloud.google.com/prediction/docs

docs/json/master/language/.gitkeep

Whitespace-only changes.

docs/toc.json

+8
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@
137137
"title": "Transaction",
138138
"type": "datastore/transaction"
139139
}]
140+
}, {
141+
}, {
142+
"title": "Language",
143+
"type": "language",
144+
"nav": [{
145+
"title": "Document",
146+
"type": "language/document"
147+
}]
140148
}, {
141149
"title": "Logging",
142150
"type": "logging",

packages/common/src/service-object.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var util = require('./util.js');
5050
* @param {object} config - Configuration object.
5151
* @param {string} config.baseUrl - The base URL to make API requests to.
5252
* @param {string} config.createMethod - The method which creates this object.
53-
* @param {string} config.id - The identifier of the object. For example, the
53+
* @param {string=} config.id - The identifier of the object. For example, the
5454
* name of a Storage bucket or Pub/Sub topic.
5555
* @param {object=} config.methods - A map of each method name that should be
5656
* inherited.
@@ -308,7 +308,7 @@ ServiceObject.prototype.request = function(reqOpts, callback) {
308308

309309
var uriComponents = [
310310
this.baseUrl,
311-
this.id,
311+
this.id || '',
312312
reqOpts.uri
313313
];
314314

packages/common/test/service-object.js

+16
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,22 @@ describe('ServiceObject', function() {
606606
serviceObject.request(reqOpts, done);
607607
});
608608

609+
it('should not require a service object ID', function(done) {
610+
var expectedUri = [
611+
serviceObject.baseUrl,
612+
reqOpts.uri
613+
].join('/');
614+
615+
serviceObject.parent.request = function(reqOpts) {
616+
assert.strictEqual(reqOpts.uri, expectedUri);
617+
done();
618+
};
619+
620+
delete serviceObject.id;
621+
622+
serviceObject.request(reqOpts, assert.ifError);
623+
});
624+
609625
it('should support absolute uris', function(done) {
610626
var expectedUri = 'http://www.google.com';
611627

packages/google-cloud/package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,20 @@
9393
"vision"
9494
],
9595
"dependencies": {
96+
"extend": "^3.0.0",
9697
"@google-cloud/bigquery": "0.1.0",
9798
"@google-cloud/bigtable": "0.1.0",
9899
"@google-cloud/compute": "0.1.0",
99100
"@google-cloud/datastore": "0.1.0",
100101
"@google-cloud/dns": "0.1.0",
101-
"@google-cloud/prediction": "0.1.0",
102+
"@google-cloud/language": "0.1.0",
102103
"@google-cloud/logging": "0.1.0",
104+
"@google-cloud/prediction": "0.1.0",
103105
"@google-cloud/pubsub": "0.1.0",
104106
"@google-cloud/resource": "0.1.0",
105107
"@google-cloud/storage": "0.1.0",
106108
"@google-cloud/translate": "0.1.0",
107-
"@google-cloud/vision": "0.1.0",
108-
"extend": "^3.0.0"
109+
"@google-cloud/vision": "0.1.0"
109110
},
110111
"devDependencies": {
111112
"proxyquire": "^1.7.10"

packages/google-cloud/src/index.js

+24
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,30 @@ var apis = {
127127
*/
128128
dns: require('@google-cloud/dns'),
129129

130+
/**
131+
* The [Google Cloud Natural Language](https://cloud.google.com/natural-language/docs)
132+
* API provides natural language understanding technologies to developers,
133+
* including sentiment analysis, entity recognition, and syntax analysis.
134+
*
135+
* <p class="notice">
136+
* **This is a Beta release of Google Cloud Natural Language.** This API is
137+
* not covered by any SLA or deprecation policy and may be subject to
138+
* backward-incompatible changes.
139+
* </p>
140+
*
141+
* @type {module:language}
142+
*
143+
* @return {module:language}
144+
*
145+
* @example
146+
* var gcloud = require('google-cloud');
147+
* var language = gcloud.language({
148+
* projectId: 'grape-spaceship-123',
149+
* keyFilename: '/path/to/keyfile.json'
150+
* });
151+
*/
152+
language: require('@google-cloud/language'),
153+
130154
/**
131155
* [Google Cloud Logging](https://cloud.google.com/logging/docs) collects and
132156
* stores logs from applications and services on the Google Cloud Platform:

packages/google-cloud/test/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var FakeBigtable = createFakeApi();
3737
var FakeCompute = createFakeApi();
3838
var FakeDatastore = createFakeApi();
3939
var FakeDNS = createFakeApi();
40+
var FakeLanguage = createFakeApi();
4041
var FakeLogging = createFakeApi();
4142
var FakePrediction = createFakeApi();
4243
var FakePubSub = createFakeApi();
@@ -55,6 +56,7 @@ describe('gcloud', function() {
5556
'@google-cloud/compute': FakeCompute,
5657
'@google-cloud/datastore': FakeDatastore,
5758
'@google-cloud/dns': FakeDNS,
59+
'@google-cloud/language': FakeLanguage,
5860
'@google-cloud/logging': FakeLogging,
5961
'@google-cloud/prediction': FakePrediction,
6062
'@google-cloud/pubsub': FakePubSub,
@@ -89,6 +91,10 @@ describe('gcloud', function() {
8991
assert.strictEqual(gcloud.dns, FakeDNS);
9092
});
9193

94+
it('should export static language', function() {
95+
assert.strictEqual(gcloud.language, FakeLanguage);
96+
});
97+
9298
it('should export static logging', function() {
9399
assert.strictEqual(gcloud.logging, FakeLogging);
94100
});
@@ -193,6 +199,15 @@ describe('gcloud', function() {
193199
});
194200
});
195201

202+
describe('language', function() {
203+
it('should create a new Language', function() {
204+
var language = localGcloud.language(options);
205+
206+
assert(language instanceof FakeLanguage);
207+
assert.strictEqual(language.calledWith_[0], options);
208+
});
209+
});
210+
196211
describe('logging', function() {
197212
it('should create a new Logging', function() {
198213
var logging = localGcloud.logging(options);

packages/language/package.json

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"name": "@google-cloud/language",
3+
"version": "0.1.0",
4+
"author": "Google Inc.",
5+
"description": "Google Cloud Natural Language Client Library for Node.js",
6+
"contributors": [
7+
{
8+
"name": "Burcu Dogan",
9+
"email": "[email protected]"
10+
},
11+
{
12+
"name": "Johan Euphrosine",
13+
"email": "[email protected]"
14+
},
15+
{
16+
"name": "Patrick Costello",
17+
"email": "[email protected]"
18+
},
19+
{
20+
"name": "Ryan Seys",
21+
"email": "[email protected]"
22+
},
23+
{
24+
"name": "Silvano Luciani",
25+
"email": "[email protected]"
26+
},
27+
{
28+
"name": "Stephen Sawchuk",
29+
"email": "[email protected]"
30+
}
31+
],
32+
"main": "./src/index.js",
33+
"files": [
34+
"./src/*",
35+
"AUTHORS",
36+
"CONTRIBUTORS",
37+
"COPYING"
38+
],
39+
"repository": "googlecloudplatform/gcloud-node",
40+
"keywords": [
41+
"google apis client",
42+
"google api client",
43+
"google apis",
44+
"google api",
45+
"google",
46+
"google cloud platform",
47+
"google cloud",
48+
"cloud",
49+
"google cloud natural language",
50+
"google cloud language",
51+
"natural language",
52+
"language"
53+
],
54+
"dependencies": {
55+
"@google-cloud/common": "^0.1.0",
56+
"@google-cloud/storage": "^0.1.0",
57+
"arrify": "^1.0.1",
58+
"extend": "^3.0.0",
59+
"google-proto-files": "^0.4.0",
60+
"is": "^3.0.1",
61+
"propprop": "^0.3.1",
62+
"string-format-obj": "^1.0.0"
63+
},
64+
"devDependencies": {
65+
"mocha": "^2.1.0",
66+
"proxyquire": "^1.7.10"
67+
},
68+
"scripts": {
69+
"publish": "../../scripts/publish.sh",
70+
"test": "mocha test/*.js",
71+
"system-test": "mocha system-test/*.js --no-timeouts --bail"
72+
},
73+
"license": "Apache-2.0",
74+
"engines": {
75+
"node": ">=0.12.0"
76+
}
77+
}

0 commit comments

Comments
 (0)