A Análise de entidade inspeciona o texto fornecido das entidades conhecidas, ou seja, nomes próprios como personalidades públicas, pontos de referência etc., e retorna informações sobre elas. A análise de entidade é realizada com o método analyzeEntities
. Para mais informações sobre os tipos de entidades que o Natural Language identifica, consulte a documentação da Entidade. Para mais informações sobre quais idiomas são compatíveis com a API Natural Language, consulte Compatibilidade de idiomas.
Nesta seção, você verá algumas maneiras de detectar entidades em um documento.
Como analisar entidades em uma string
Veja um exemplo de análise de entidade em uma string de texto enviada diretamente para a Natural Language API:
Protocolo
Para analisar as entidades em um documento, crie uma solicitação POST
para o método REST documents:analyzeEntities
e forneça o corpo da solicitação apropriada, como mostrado no exemplo a seguir.
No exemplo, o comando gcloud auth application-default print-access-token
é usado para gerar um token de acesso para uma conta de serviço configurada para o projeto usando o SDK do Cloud do Google Cloud Platform.
Para ver instruções sobre como instalar o SDK do Cloud, configurar um projeto com uma conta de serviço, consulte o Guia de início 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"
Se você não especificar document.language
, o idioma será detectado automaticamente. Para mais informações sobre quais idiomas são compatíveis com a API Natural Language, consulte Compatibilidade de idiomas. Consulte a documentação de referência Document
para mais informações sobre como configurar o corpo da solicitação.
Quando a solicitação é bem-sucedida, o servidor retorna um código de status HTTP 200 OK
e a resposta no 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" }
A matriz entities
contém objetos Entity
que representam as entidades detectadas, com informações como o nome e o tipo da entidade.
gcloud
Consulte o comando analyze-entities
para ver todos os detalhes.
Para realizar uma análise de entidade, use a ferramenta de linha de comando gcloud
e use a sinalização --content
para identificar o conteúdo a ser analisado:
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."
Se a solicitação for bem-sucedida, o servidor retornará uma resposta no 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" }
A matriz entities
contém objetos Entity
que representam as entidades detectadas, com informações como o nome e o tipo da entidade.
C#
Go
Java
Node.js
PHP
Python
Ruby
Analisar entidades do Google Cloud Storage
Para sua comodidade, a Natural Language API faz a análise da entidade diretamente em um arquivo localizado no Google Cloud Storage, sem a necessidade de enviar o conteúdo do arquivo no corpo da solicitação.
Veja um exemplo de análise de entidade em um arquivo localizado no Google Cloud Storage.
Protocolo
Para analisar as entidades de um documento armazenado no Google Cloud Storage, crie uma solicitação POST
para o método REST documents:analyzeEntities
e forneça o caminho para o documento ao corpo da solicitação apropriada, como mostrado no exemplo a seguir.
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"
Se você não especificar document.language
, o idioma será detectado automaticamente. Para ver mais informações sobre quais idiomas são compatíveis com a API Natural Language, consulte Compatibilidade de idiomas. Consulte a documentação de referência Document
para mais informações sobre como configurar o corpo da solicitação.
Quando a solicitação é bem-sucedida, o servidor retorna um código de status HTTP 200 OK
e a resposta no 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" }
A matriz entities
contém objetos Entity
que representam as entidades detectadas, com informações como o nome e o tipo da entidade.
gcloud
Consulte o comando analyze-entities
para ver todos os detalhes.
Para realizar uma análise de entidade em um arquivo no Google Cloud Storage, use a ferramenta de linha de comando gcloud
e use a sinalização --content-file
para identificar o caminho do arquivo que contém o conteúdo a ser analisado:
gcloud ml language analyze-entities --content-file=gs://YOUR_BUCKET_NAME/YOUR_FILE_NAME
Se a solicitação for bem-sucedida, o servidor retornará uma resposta no 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" }
A matriz entities
contém objetos Entity
que representam as entidades detectadas, com informações como o nome e o tipo da entidade.
C#
Go
Java
Node.js
PHP
Python
Ruby