|
| 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)); |
0 commit comments