El análisis de entidades inspecciona el texto dado en busca de entidades conocidas (nombres propios como figuras públicas, puntos de referencia, etc.) y muestra información sobre ellas. Se ejecuta con el método analyzeEntities
. Para obtener información sobre los tipos de entidades que identifica Natural Language, consulta la documentación de Entidad. Para obtener información sobre los idiomas compatibles con la API de Natural Language, consulta Idiomas compatibles.
Esta sección muestra algunas maneras de detectar entidades en un documento.
Analizar entidades en una string
Este es un ejemplo de la realización de un análisis de entidades en una string de texto enviado directamente a la API de Natural Language:
Protocolo
Para analizar entidades en un documento, realiza una solicitud POST
al método documents:analyzeEntities
de REST y proporciona el cuerpo de solicitud adecuado, como se muestra en el siguiente ejemplo.
En este ejemplo, se usa el comando gcloud auth application-default print-access-token
a fin de obtener un token de acceso correspondiente a una cuenta de servicio configurada para el proyecto con el SDK de Cloud de Google Cloud Platform.
Para obtener instrucciones sobre cómo instalar el SDK de Cloud y configurar un proyecto con una cuenta de servicio, consulta la Guía de inicio rápido.
curl -X POST \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ -H "Content-Type: application/json; charset=utf-8" \ --data "{ 'encodingType': 'UTF8', 'document': { 'type': 'PLAIN_TEXT', 'content': 'President Trump will speak from the White House, located at 1600 Pennsylvania Ave NW, Washington, DC, on October 7.' } }" "https://language.googleapis.com/v1/documents:analyzeEntities"
Si no especificas document.language
, el idioma se detectará automáticamente. Para obtener información sobre los idiomas compatibles con la API de Natural Language, consulta Idiomas compatibles. Consulta la documentación de referencia de Document
para obtener más información sobre la configuración del cuerpo de la solicitud.
Si la solicitud se completa correctamente, el servidor muestra un código de estado HTTP 200 OK
y la respuesta en formato JSON:
{ "entities": [ { "name": "Trump", "type": "PERSON", "metadata": { "mid": "/m/0cqt90", "wikipedia_url": "https://en.wikipedia.org/wiki/Donald_Trump" }, "salience": 0.7936003, "mentions": [ { "text": { "content": "Trump", "beginOffset": 10 }, "type": "PROPER" }, { "text": { "content": "President", "beginOffset": 0 }, "type": "COMMON" } ] }, { "name": "White House", "type": "LOCATION", "metadata": { "mid": "/m/081sq", "wikipedia_url": "https://en.wikipedia.org/wiki/White_House" }, "salience": 0.09172433, "mentions": [ { "text": { "content": "White House", "beginOffset": 36 }, "type": "PROPER" } ] }, { "name": "Pennsylvania Ave NW", "type": "LOCATION", "metadata": { "mid": "/g/1tgb87cq" }, "salience": 0.085507184, "mentions": [ { "text": { "content": "Pennsylvania Ave NW", "beginOffset": 65 }, "type": "PROPER" } ] }, { "name": "Washington, DC", "type": "LOCATION", "metadata": { "mid": "/m/0rh6k", "wikipedia_url": "https://en.wikipedia.org/wiki/Washington,_D.C." }, "salience": 0.029168168, "mentions": [ { "text": { "content": "Washington, DC", "beginOffset": 86 }, "type": "PROPER" } ] } { "name": "1600 Pennsylvania Ave NW, Washington, DC", "type": "ADDRESS", "metadata": { "country": "US", "sublocality": "Fort Lesley J. McNair", "locality": "Washington", "street_name": "Pennsylvania Avenue Northwest", "broad_region": "District of Columbia", "narrow_region": "District of Columbia", "street_number": "1600" }, "salience": 0, "mentions": [ { "text": { "content": "1600 Pennsylvania Ave NW, Washington, DC", "beginOffset": 60 }, "type": "TYPE_UNKNOWN" } ] } } { "name": "1600", "type": "NUMBER", "metadata": { "value": "1600" }, "salience": 0, "mentions": [ { "text": { "content": "1600", "beginOffset": 60 }, "type": "TYPE_UNKNOWN" } ] }, { "name": "October 7", "type": "DATE", "metadata": { "day": "7", "month": "10" }, "salience": 0, "mentions": [ { "text": { "content": "October 7", "beginOffset": 105 }, "type": "TYPE_UNKNOWN" } ] } { "name": "7", "type": "NUMBER", "metadata": { "value": "7" }, "salience": 0, "mentions": [ { "text": { "content": "7", "beginOffset": 113 }, "type": "TYPE_UNKNOWN" } ] } ], "language": "en" }
El arreglo entities
contiene objetos Entity
que representan las entidades detectadas, incluida información sobre el nombre y el tipo de entidad.
gcloud
Consulta el comando analyze-entities
para obtener todos los detalles.
Para realizar un análisis de entidades, usa la herramienta de línea de comandos de gcloud
y también utiliza la marca --content
para identificar el contenido que se analizará:
gcloud ml language analyze-entities --content="President Trump will speak from the White House, located at 1600 Pennsylvania Ave NW, Washington, DC, on October 7."
Si la solicitud es exitosa, el servidor muestra una respuesta en formato JSON:
{ "entities": [ { "name": "Trump", "type": "PERSON", "metadata": { "mid": "/m/0cqt90", "wikipedia_url": "https://en.wikipedia.org/wiki/Donald_Trump" }, "salience": 0.7936003, "mentions": [ { "text": { "content": "Trump", "beginOffset": 10 }, "type": "PROPER" }, { "text": { "content": "President", "beginOffset": 0 }, "type": "COMMON" } ] }, { "name": "White House", "type": "LOCATION", "metadata": { "mid": "/m/081sq", "wikipedia_url": "https://en.wikipedia.org/wiki/White_House" }, "salience": 0.09172433, "mentions": [ { "text": { "content": "White House", "beginOffset": 36 }, "type": "PROPER" } ] }, { "name": "Pennsylvania Ave NW", "type": "LOCATION", "metadata": { "mid": "/g/1tgb87cq" }, "salience": 0.085507184, "mentions": [ { "text": { "content": "Pennsylvania Ave NW", "beginOffset": 65 }, "type": "PROPER" } ] }, { "name": "Washington, DC", "type": "LOCATION", "metadata": { "mid": "/m/0rh6k", "wikipedia_url": "https://en.wikipedia.org/wiki/Washington,_D.C." }, "salience": 0.029168168, "mentions": [ { "text": { "content": "Washington, DC", "beginOffset": 86 }, "type": "PROPER" } ] } { "name": "1600 Pennsylvania Ave NW, Washington, DC", "type": "ADDRESS", "metadata": { "country": "US", "sublocality": "Fort Lesley J. McNair", "locality": "Washington", "street_name": "Pennsylvania Avenue Northwest", "broad_region": "District of Columbia", "narrow_region": "District of Columbia", "street_number": "1600" }, "salience": 0, "mentions": [ { "text": { "content": "1600 Pennsylvania Ave NW, Washington, DC", "beginOffset": 60 }, "type": "TYPE_UNKNOWN" } ] } } { "name": "1600", "type": "NUMBER", "metadata": { "value": "1600" }, "salience": 0, "mentions": [ { "text": { "content": "1600", "beginOffset": 60 }, "type": "TYPE_UNKNOWN" } ] }, { "name": "October 7", "type": "DATE", "metadata": { "day": "7", "month": "10" }, "salience": 0, "mentions": [ { "text": { "content": "October 7", "beginOffset": 105 }, "type": "TYPE_UNKNOWN" } ] } { "name": "7", "type": "NUMBER", "metadata": { "value": "7" }, "salience": 0, "mentions": [ { "text": { "content": "7", "beginOffset": 113 }, "type": "TYPE_UNKNOWN" } ] } ], "language": "en" }
El arreglo entities
contiene objetos Entity
que representan las entidades detectadas, incluida información sobre el nombre y el tipo de entidad.
C#
Comienza a usarlo
Java
Node.js
PHP
Python
Ruby
Analiza entidades en Google Cloud Storage
Para tu comodidad, la API de Natural Language puede realizar un análisis de entidades directamente en un archivo ubicado en Google Cloud Storage, sin la necesidad de enviar el contenido del archivo en el cuerpo de tu solicitud.
El siguiente es un ejemplo de cómo realizar un análisis de entidades en un archivo ubicado en Cloud Storage.
Protocolo
Para analizar entidades en un documento almacenado en Google Cloud Storage, realiza una solicitud POST
al método documents:analyzeEntities
de REST y proporciona el cuerpo de la solicitud adecuado con la ruta del documento, como se muestra en el siguiente ejemplo.
curl -X POST \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ -H "Content-Type: application/json; charset=utf-8" \ --data "{ 'document':{ 'type':'PLAIN_TEXT', 'gcsContentUri':'gs://<bucket-name>/<object-name>' } }" "https://language.googleapis.com/v1/documents:analyzeEntities"
Si no especificas document.language
, el idioma se detectará automáticamente. Para obtener información sobre los idiomas compatibles con la API de Natural Language, consulta Idiomas compatibles. Consulta la documentación de referencia de Document
para obtener más información sobre la configuración del cuerpo de la solicitud.
Si la solicitud se completa correctamente, el servidor muestra un código de estado HTTP 200 OK
y la respuesta en formato JSON:
{ "entities": [ { "name": "Trump", "type": "PERSON", "metadata": { "mid": "/m/0cqt90", "wikipedia_url": "https://en.wikipedia.org/wiki/Donald_Trump" }, "salience": 0.7936003, "mentions": [ { "text": { "content": "Trump", "beginOffset": 10 }, "type": "PROPER" }, { "text": { "content": "President", "beginOffset": 0 }, "type": "COMMON" } ] }, { "name": "White House", "type": "LOCATION", "metadata": { "mid": "/m/081sq", "wikipedia_url": "https://en.wikipedia.org/wiki/White_House" }, "salience": 0.09172433, "mentions": [ { "text": { "content": "White House", "beginOffset": 36 }, "type": "PROPER" } ] }, { "name": "Pennsylvania Ave NW", "type": "LOCATION", "metadata": { "mid": "/g/1tgb87cq" }, "salience": 0.085507184, "mentions": [ { "text": { "content": "Pennsylvania Ave NW", "beginOffset": 65 }, "type": "PROPER" } ] }, { "name": "Washington, DC", "type": "LOCATION", "metadata": { "mid": "/m/0rh6k", "wikipedia_url": "https://en.wikipedia.org/wiki/Washington,_D.C." }, "salience": 0.029168168, "mentions": [ { "text": { "content": "Washington, DC", "beginOffset": 86 }, "type": "PROPER" } ] } { "name": "1600 Pennsylvania Ave NW, Washington, DC", "type": "ADDRESS", "metadata": { "country": "US", "sublocality": "Fort Lesley J. McNair", "locality": "Washington", "street_name": "Pennsylvania Avenue Northwest", "broad_region": "District of Columbia", "narrow_region": "District of Columbia", "street_number": "1600" }, "salience": 0, "mentions": [ { "text": { "content": "1600 Pennsylvania Ave NW, Washington, DC", "beginOffset": 60 }, "type": "TYPE_UNKNOWN" } ] } } { "name": "1600", "type": "NUMBER", "metadata": { "value": "1600" }, "salience": 0, "mentions": [ { "text": { "content": "1600", "beginOffset": 60 }, "type": "TYPE_UNKNOWN" } ] }, { "name": "October 7", "type": "DATE", "metadata": { "day": "7", "month": "10" }, "salience": 0, "mentions": [ { "text": { "content": "October 7", "beginOffset": 105 }, "type": "TYPE_UNKNOWN" } ] } { "name": "7", "type": "NUMBER", "metadata": { "value": "7" }, "salience": 0, "mentions": [ { "text": { "content": "7", "beginOffset": 113 }, "type": "TYPE_UNKNOWN" } ] } ], "language": "en" }
El arreglo entities
contiene objetos Entity
que representan las entidades detectadas, incluida información sobre el nombre y el tipo de entidad.
gcloud
Consulta el comando analyze-entities
para obtener todos los detalles.
Para realizar un análisis de entidades en un archivo de Google Cloud Storage, usa la herramienta de línea de comandos gcloud
y también utiliza la marca --content-file
para identificar la ruta del archivo que tiene el contenido que analizarás:
gcloud ml language analyze-entities --content-file=gs://YOUR_BUCKET_NAME/YOUR_FILE_NAME
Si la solicitud es exitosa, el servidor muestra una respuesta en formato JSON:
{ "entities": [ { "name": "Trump", "type": "PERSON", "metadata": { "mid": "/m/0cqt90", "wikipedia_url": "https://en.wikipedia.org/wiki/Donald_Trump" }, "salience": 0.7936003, "mentions": [ { "text": { "content": "Trump", "beginOffset": 10 }, "type": "PROPER" }, { "text": { "content": "President", "beginOffset": 0 }, "type": "COMMON" } ] }, { "name": "White House", "type": "LOCATION", "metadata": { "mid": "/m/081sq", "wikipedia_url": "https://en.wikipedia.org/wiki/White_House" }, "salience": 0.09172433, "mentions": [ { "text": { "content": "White House", "beginOffset": 36 }, "type": "PROPER" } ] }, { "name": "Pennsylvania Ave NW", "type": "LOCATION", "metadata": { "mid": "/g/1tgb87cq" }, "salience": 0.085507184, "mentions": [ { "text": { "content": "Pennsylvania Ave NW", "beginOffset": 65 }, "type": "PROPER" } ] }, { "name": "Washington, DC", "type": "LOCATION", "metadata": { "mid": "/m/0rh6k", "wikipedia_url": "https://en.wikipedia.org/wiki/Washington,_D.C." }, "salience": 0.029168168, "mentions": [ { "text": { "content": "Washington, DC", "beginOffset": 86 }, "type": "PROPER" } ] } { "name": "1600 Pennsylvania Ave NW, Washington, DC", "type": "ADDRESS", "metadata": { "country": "US", "sublocality": "Fort Lesley J. McNair", "locality": "Washington", "street_name": "Pennsylvania Avenue Northwest", "broad_region": "District of Columbia", "narrow_region": "District of Columbia", "street_number": "1600" }, "salience": 0, "mentions": [ { "text": { "content": "1600 Pennsylvania Ave NW, Washington, DC", "beginOffset": 60 }, "type": "TYPE_UNKNOWN" } ] } } { "name": "1600", "type": "NUMBER", "metadata": { "value": "1600" }, "salience": 0, "mentions": [ { "text": { "content": "1600", "beginOffset": 60 }, "type": "TYPE_UNKNOWN" } ] }, { "name": "October 7", "type": "DATE", "metadata": { "day": "7", "month": "10" }, "salience": 0, "mentions": [ { "text": { "content": "October 7", "beginOffset": 105 }, "type": "TYPE_UNKNOWN" } ] } { "name": "7", "type": "NUMBER", "metadata": { "value": "7" }, "salience": 0, "mentions": [ { "text": { "content": "7", "beginOffset": 113 }, "type": "TYPE_UNKNOWN" } ] } ], "language": "en" }
El arreglo entities
contiene objetos Entity
que representan las entidades detectadas, incluida información sobre el nombre y el tipo de entidad.
C#
Comienza a usarlo
Java
Node.js
PHP
Python
Ruby