Skip to content

Commit

Permalink
docs: add natural language entity extraction ga samples (#295)
Browse files Browse the repository at this point in the history
* docs: add natural language entity extraction ga samples

* update package.json

* lint fix
  • Loading branch information
nnegrey authored and Ace Nassri committed Nov 15, 2022
1 parent 2ab27c0 commit 4497d2e
Show file tree
Hide file tree
Showing 7 changed files with 368 additions and 2 deletions.
66 changes: 66 additions & 0 deletions automl/language_entity_extraction_create_dataset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright 2019 Google LLC
* 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';

function main(
projectId = 'YOUR_PROJECT_ID',
location = 'us-central1',
displayName = 'YOUR_DISPLAY_NAME'
) {
// [START automl_language_entity_extraction_create_dataset]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const displayName = 'YOUR_DISPLAY_NAME';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new AutoMlClient();

async function createDataset() {
// Construct request
const request = {
parent: client.locationPath(projectId, location),
dataset: {
displayName: displayName,
textExtractionDatasetMetadata: {},
},
};

// Create dataset
const [operation] = await client.createDataset(request);

// Wait for operation to complete.
const [response] = await operation.promise();

console.log(`Dataset name: ${response.name}`);
console.log(`
Dataset id: ${
response.name
.split('/')
[response.name.split('/').length - 1].split('\n')[0]
}`);
}

createDataset();
// [END automl_language_entity_extraction_create_dataset]
}

main(...process.argv.slice(2));
60 changes: 60 additions & 0 deletions automl/language_entity_extraction_create_model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2019 Google LLC
* 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';

function main(
projectId = 'YOUR_PROJECT_ID',
location = 'us-central1',
datasetId = 'YOUR_DATASET_ID',
displayName = 'YOUR_DISPLAY_NAME'
) {
// [START automl_language_entity_extraction_create_model]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const dataset_id = 'YOUR_DATASET_ID';
// const displayName = 'YOUR_DISPLAY_NAME';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new AutoMlClient();

async function createModel() {
// Construct request
const request = {
parent: client.locationPath(projectId, location),
model: {
displayName: displayName,
datasetId: datasetId,
textExtractionModelMetadata: {}, // Leave unset, to use the default base model
},
};

// Don't wait for the LRO
const [operation] = await client.createModel(request);
console.log(`Training started... ${operation}`);
console.log(`Training operation name: ${operation.name}`);
}

createModel();
// [END automl_language_entity_extraction_create_model]
}

main(...process.argv.slice(2));
69 changes: 69 additions & 0 deletions automl/language_entity_extraction_predict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2019 Google LLC
* 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';

function main(
projectId = 'YOUR_PROJECT_ID',
location = 'us-central1',
modelId = 'YOUR_MODEL_ID',
content = 'text to predict'
) {
// [START automl_language_entity_extraction_predict]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const modelId = 'YOUR_MODEL_ID';
// const content = 'text to predict'

// Imports the Google Cloud AutoML library
const {PredictionServiceClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new PredictionServiceClient();

async function predict() {
// Construct request
const request = {
name: client.modelPath(projectId, location, modelId),
payload: {
textSnippet: {
content: content,
mimeType: 'text/plain', // Types: 'test/plain', 'text/html'
},
},
};

const [response] = await client.predict(request);

for (const annotationPayload of response.payload) {
console.log(
`Text Extract Entity Types: ${annotationPayload.displayName}`
);
console.log(`Text Score: ${annotationPayload.textExtraction.score}`);
const textSegment = annotationPayload.textExtraction.textSegment;
console.log(`Text Extract Entity Content: ${textSegment.content}`);
console.log(`Text Start Offset: ${textSegment.startOffset}`);
console.log(`Text End Offset: ${textSegment.endOffset}`);
}
}

predict();
// [END automl_language_entity_extraction_predict]
}

main(...process.argv.slice(2));
6 changes: 4 additions & 2 deletions automl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"repository": "googleapis/nodejs-automl",
"private": true,
"scripts": {
"test": "mocha --timeout 600000"
"test": "mocha --timeout 900000"
},
"files": [
"**/*.js",
Expand All @@ -22,7 +22,9 @@
"yargs": "^15.0.0"
},
"devDependencies": {
"@google-cloud/storage": "^3.5.0",
"chai": "^4.2.0",
"mocha": "^6.2.2"
"mocha": "^6.2.2",
"uuid": "^3.3.3"
}
}
57 changes: 57 additions & 0 deletions automl/test/language_entity_extraction_create_dataset.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright 2019 Google LLC
* 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 {assert} = require('chai');
const {AutoMlClient} = require('@google-cloud/automl').v1;

const cp = require('child_process');
const uuid = require('uuid');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const CREATE_DATASET_REGION_TAG = 'language_entity_extraction_create_dataset';
const LOCATION = 'us-central1';

describe('Automl Natural Language Entity Extraction Create Dataset Test', () => {
const client = new AutoMlClient();
let datasetId;

it('should create a dataset', async () => {
const projectId = await client.getProjectId();
const displayName = `test_${uuid
.v4()
.replace(/-/g, '_')
.substring(0, 26)}`;

// create
const create_output = execSync(
`node ${CREATE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${displayName}`
);
assert.match(create_output, /Dataset id:/);

datasetId = create_output.split('Dataset id: ')[1].split('\n')[0];
});

after('delete created dataset', async () => {
const projectId = await client.getProjectId();
const request = {
name: client.datasetPath(projectId, LOCATION, datasetId),
};
const [operation] = await client.deleteDataset(request);
await operation.promise();
});
});
51 changes: 51 additions & 0 deletions automl/test/language_entity_extraction_create_model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright 2019 Google LLC
* 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 {assert} = require('chai');
const {AutoMlClient} = require('@google-cloud/automl').v1;

const cp = require('child_process');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const CREATE_MODEL_REGION_TAG = 'language_entity_extraction_create_model';
const LOCATION = 'us-central1';
const DATASET_ID = 'TEN8374527069979148288';

describe('Automl Natural Language Entity Extraction Create Model Test', () => {
const client = new AutoMlClient();
// let operationId;

// Natural language entity extraction models are non cancellable operations
it.skip('should create a model', async () => {
const projectId = await client.getProjectId();

const create_output = execSync(
`node ${CREATE_MODEL_REGION_TAG}.js ${projectId} ${LOCATION} ${DATASET_ID} extraction_test_create_model`
);

assert.match(create_output, /Training started/);

// operationId = create_output
// .split('Training operation name: ')[1]
// .split('\n')[0];
});

// after('cancel model training', async () => {
// await client.operationsClient.cancelOperation({name: operationId});
// });
});
61 changes: 61 additions & 0 deletions automl/test/language_entity_extraction_predict.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright 2019 Google LLC
* 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 {assert} = require('chai');
const {AutoMlClient} = require('@google-cloud/automl').v1;

const cp = require('child_process');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const PREDICT_REGION_TAG = 'language_entity_extraction_predict';
const LOCATION = 'us-central1';
const MODEL_ID = 'TEN2238627664384491520';

describe('Automl Natural Language Entity Extraction Predict Test', () => {
const client = new AutoMlClient();

before('should verify the model is deployed', async () => {
const projectId = await client.getProjectId();
const request = {
name: client.modelPath(projectId, LOCATION, MODEL_ID),
};

const [response] = await client.getModel(request);
if (response.deploymentState === 'UNDEPLOYED') {
const request = {
name: client.modelPath(projectId, LOCATION, MODEL_ID),
};

const [operation] = await client.deployModel(request);

// Wait for operation to complete.
await operation.promise();
}
});

it('should predict', async () => {
const projectId = await client.getProjectId();
const content =
"'Constitutional mutations in the WT1 gene in patients with Denys-Drash syndrome.'";

const predictOutput = execSync(
`node ${PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} ${content}`
);
assert.match(predictOutput, /Text Extract Entity Types/);
});
});

0 comments on commit 4497d2e

Please sign in to comment.