Skip to content

Commit 08970a3

Browse files
nnegreyAce Nassri
authored and
Ace Nassri
committed
docs: add natural language entity extraction ga samples (#295)
* docs: add natural language entity extraction ga samples * update package.json * lint fix
1 parent 6dcea3b commit 08970a3

7 files changed

+368
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright 2019 Google LLC
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+
'use strict';
17+
18+
function main(
19+
projectId = 'YOUR_PROJECT_ID',
20+
location = 'us-central1',
21+
displayName = 'YOUR_DISPLAY_NAME'
22+
) {
23+
// [START automl_language_entity_extraction_create_dataset]
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const projectId = 'YOUR_PROJECT_ID';
28+
// const location = 'us-central1';
29+
// const displayName = 'YOUR_DISPLAY_NAME';
30+
31+
// Imports the Google Cloud AutoML library
32+
const {AutoMlClient} = require(`@google-cloud/automl`).v1;
33+
34+
// Instantiates a client
35+
const client = new AutoMlClient();
36+
37+
async function createDataset() {
38+
// Construct request
39+
const request = {
40+
parent: client.locationPath(projectId, location),
41+
dataset: {
42+
displayName: displayName,
43+
textExtractionDatasetMetadata: {},
44+
},
45+
};
46+
47+
// Create dataset
48+
const [operation] = await client.createDataset(request);
49+
50+
// Wait for operation to complete.
51+
const [response] = await operation.promise();
52+
53+
console.log(`Dataset name: ${response.name}`);
54+
console.log(`
55+
Dataset id: ${
56+
response.name
57+
.split('/')
58+
[response.name.split('/').length - 1].split('\n')[0]
59+
}`);
60+
}
61+
62+
createDataset();
63+
// [END automl_language_entity_extraction_create_dataset]
64+
}
65+
66+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright 2019 Google LLC
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+
'use strict';
17+
18+
function main(
19+
projectId = 'YOUR_PROJECT_ID',
20+
location = 'us-central1',
21+
datasetId = 'YOUR_DATASET_ID',
22+
displayName = 'YOUR_DISPLAY_NAME'
23+
) {
24+
// [START automl_language_entity_extraction_create_model]
25+
/**
26+
* TODO(developer): Uncomment these variables before running the sample.
27+
*/
28+
// const projectId = 'YOUR_PROJECT_ID';
29+
// const location = 'us-central1';
30+
// const dataset_id = 'YOUR_DATASET_ID';
31+
// const displayName = 'YOUR_DISPLAY_NAME';
32+
33+
// Imports the Google Cloud AutoML library
34+
const {AutoMlClient} = require(`@google-cloud/automl`).v1;
35+
36+
// Instantiates a client
37+
const client = new AutoMlClient();
38+
39+
async function createModel() {
40+
// Construct request
41+
const request = {
42+
parent: client.locationPath(projectId, location),
43+
model: {
44+
displayName: displayName,
45+
datasetId: datasetId,
46+
textExtractionModelMetadata: {}, // Leave unset, to use the default base model
47+
},
48+
};
49+
50+
// Don't wait for the LRO
51+
const [operation] = await client.createModel(request);
52+
console.log(`Training started... ${operation}`);
53+
console.log(`Training operation name: ${operation.name}`);
54+
}
55+
56+
createModel();
57+
// [END automl_language_entity_extraction_create_model]
58+
}
59+
60+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright 2019 Google LLC
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+
'use strict';
17+
18+
function main(
19+
projectId = 'YOUR_PROJECT_ID',
20+
location = 'us-central1',
21+
modelId = 'YOUR_MODEL_ID',
22+
content = 'text to predict'
23+
) {
24+
// [START automl_language_entity_extraction_predict]
25+
/**
26+
* TODO(developer): Uncomment these variables before running the sample.
27+
*/
28+
// const projectId = 'YOUR_PROJECT_ID';
29+
// const location = 'us-central1';
30+
// const modelId = 'YOUR_MODEL_ID';
31+
// const content = 'text to predict'
32+
33+
// Imports the Google Cloud AutoML library
34+
const {PredictionServiceClient} = require(`@google-cloud/automl`).v1;
35+
36+
// Instantiates a client
37+
const client = new PredictionServiceClient();
38+
39+
async function predict() {
40+
// Construct request
41+
const request = {
42+
name: client.modelPath(projectId, location, modelId),
43+
payload: {
44+
textSnippet: {
45+
content: content,
46+
mimeType: 'text/plain', // Types: 'test/plain', 'text/html'
47+
},
48+
},
49+
};
50+
51+
const [response] = await client.predict(request);
52+
53+
for (const annotationPayload of response.payload) {
54+
console.log(
55+
`Text Extract Entity Types: ${annotationPayload.displayName}`
56+
);
57+
console.log(`Text Score: ${annotationPayload.textExtraction.score}`);
58+
const textSegment = annotationPayload.textExtraction.textSegment;
59+
console.log(`Text Extract Entity Content: ${textSegment.content}`);
60+
console.log(`Text Start Offset: ${textSegment.startOffset}`);
61+
console.log(`Text End Offset: ${textSegment.endOffset}`);
62+
}
63+
}
64+
65+
predict();
66+
// [END automl_language_entity_extraction_predict]
67+
}
68+
69+
main(...process.argv.slice(2));

automl/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"repository": "googleapis/nodejs-automl",
1010
"private": true,
1111
"scripts": {
12-
"test": "mocha --timeout 600000"
12+
"test": "mocha --timeout 900000"
1313
},
1414
"files": [
1515
"**/*.js",
@@ -22,7 +22,9 @@
2222
"yargs": "^15.0.0"
2323
},
2424
"devDependencies": {
25+
"@google-cloud/storage": "^3.5.0",
2526
"chai": "^4.2.0",
26-
"mocha": "^6.2.2"
27+
"mocha": "^6.2.2",
28+
"uuid": "^3.3.3"
2729
}
2830
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright 2019 Google LLC
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+
'use strict';
17+
18+
const {assert} = require('chai');
19+
const {AutoMlClient} = require('@google-cloud/automl').v1;
20+
21+
const cp = require('child_process');
22+
const uuid = require('uuid');
23+
24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
26+
const CREATE_DATASET_REGION_TAG = 'language_entity_extraction_create_dataset';
27+
const LOCATION = 'us-central1';
28+
29+
describe('Automl Natural Language Entity Extraction Create Dataset Test', () => {
30+
const client = new AutoMlClient();
31+
let datasetId;
32+
33+
it('should create a dataset', async () => {
34+
const projectId = await client.getProjectId();
35+
const displayName = `test_${uuid
36+
.v4()
37+
.replace(/-/g, '_')
38+
.substring(0, 26)}`;
39+
40+
// create
41+
const create_output = execSync(
42+
`node ${CREATE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${displayName}`
43+
);
44+
assert.match(create_output, /Dataset id:/);
45+
46+
datasetId = create_output.split('Dataset id: ')[1].split('\n')[0];
47+
});
48+
49+
after('delete created dataset', async () => {
50+
const projectId = await client.getProjectId();
51+
const request = {
52+
name: client.datasetPath(projectId, LOCATION, datasetId),
53+
};
54+
const [operation] = await client.deleteDataset(request);
55+
await operation.promise();
56+
});
57+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2019 Google LLC
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+
'use strict';
17+
18+
const {assert} = require('chai');
19+
const {AutoMlClient} = require('@google-cloud/automl').v1;
20+
21+
const cp = require('child_process');
22+
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
25+
const CREATE_MODEL_REGION_TAG = 'language_entity_extraction_create_model';
26+
const LOCATION = 'us-central1';
27+
const DATASET_ID = 'TEN8374527069979148288';
28+
29+
describe('Automl Natural Language Entity Extraction Create Model Test', () => {
30+
const client = new AutoMlClient();
31+
// let operationId;
32+
33+
// Natural language entity extraction models are non cancellable operations
34+
it.skip('should create a model', async () => {
35+
const projectId = await client.getProjectId();
36+
37+
const create_output = execSync(
38+
`node ${CREATE_MODEL_REGION_TAG}.js ${projectId} ${LOCATION} ${DATASET_ID} extraction_test_create_model`
39+
);
40+
41+
assert.match(create_output, /Training started/);
42+
43+
// operationId = create_output
44+
// .split('Training operation name: ')[1]
45+
// .split('\n')[0];
46+
});
47+
48+
// after('cancel model training', async () => {
49+
// await client.operationsClient.cancelOperation({name: operationId});
50+
// });
51+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright 2019 Google LLC
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+
'use strict';
17+
18+
const {assert} = require('chai');
19+
const {AutoMlClient} = require('@google-cloud/automl').v1;
20+
21+
const cp = require('child_process');
22+
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
25+
const PREDICT_REGION_TAG = 'language_entity_extraction_predict';
26+
const LOCATION = 'us-central1';
27+
const MODEL_ID = 'TEN2238627664384491520';
28+
29+
describe('Automl Natural Language Entity Extraction Predict Test', () => {
30+
const client = new AutoMlClient();
31+
32+
before('should verify the model is deployed', async () => {
33+
const projectId = await client.getProjectId();
34+
const request = {
35+
name: client.modelPath(projectId, LOCATION, MODEL_ID),
36+
};
37+
38+
const [response] = await client.getModel(request);
39+
if (response.deploymentState === 'UNDEPLOYED') {
40+
const request = {
41+
name: client.modelPath(projectId, LOCATION, MODEL_ID),
42+
};
43+
44+
const [operation] = await client.deployModel(request);
45+
46+
// Wait for operation to complete.
47+
await operation.promise();
48+
}
49+
});
50+
51+
it('should predict', async () => {
52+
const projectId = await client.getProjectId();
53+
const content =
54+
"'Constitutional mutations in the WT1 gene in patients with Denys-Drash syndrome.'";
55+
56+
const predictOutput = execSync(
57+
`node ${PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} ${content}`
58+
);
59+
assert.match(predictOutput, /Text Extract Entity Types/);
60+
});
61+
});

0 commit comments

Comments
 (0)