Executar a análise de sentimento usando a ferramenta gcloud
Esta página mostra como dar os primeiros passos com a API Cloud Natural Language usando o SDK do Google Cloud.
Para seguir as instruções detalhadas desta tarefa diretamente no console do Google Cloud, clique em Orientação:
Antes de começar
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Natural Language API:
gcloud services enable language.googleapis.com
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Natural Language API:
gcloud services enable language.googleapis.com
Enviar uma solicitação de análise de entidade
gcloud
Use a ferramenta de linha de comandogcloud
para chamar o comando analyze-entities
e use a flag --content
para
especificar o texto a ser analisado.
gcloud ml language analyze-entities --content="Michelangelo Caravaggio, Italian painter, is known for 'The Calling of Saint Matthew'."
Linha de comando
Usecurl
para fazer uma solicitação POST
ao método documents:analyzeEntities
e forneça o corpo da solicitação apropriado, conforme mostrado no exemplo
a seguir.
O exemplo usa o comando gcloud auth application-default print-access-token
para gerar um token de acesso para a conta de serviço que você criou quando configurou o projeto.
curl -X POST \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ -H "Content-Type: application/json; charset=utf-8" \ "https://language.googleapis.com/v1/documents:analyzeEntities" \ --data "{ 'document':{ 'type':'PLAIN_TEXT', 'content':'Michelangelo Caravaggio, Italian painter, is known for \'The Calling of Saint Matthew\'.' }, 'encodingType':'UTF8' }"
Uma resposta semelhante a esta vai aparecer:
{ "entities": [ { "name": "Michelangelo Caravaggio", "type": "PERSON", "metadata": { "wikipedia_url": "http://en.wikipedia.org/wiki/Caravaggio", "mid": "/m/020bg" }, "salience": 0.83047235, "mentions": [ { "text": { "content": "Michelangelo Caravaggio", "beginOffset": 0 }, "type": "PROPER" }, { "text": { "content": "painter", "beginOffset": 33 }, "type": "COMMON" } ] }, { "name": "Italian", "type": "LOCATION", "metadata": { "mid": "/m/03rjj", "wikipedia_url": "http://en.wikipedia.org/wiki/Italy" }, "salience": 0.13870546, "mentions": [ { "text": { "content": "Italian", "beginOffset": 25 }, "type": "PROPER" } ] }, { "name": "The Calling of Saint Matthew", "type": "EVENT", "metadata": { "mid": "/m/085_p7", "wikipedia_url": "http://en.wikipedia.org/wiki/The_Calling_of_St_Matthew_(Caravaggio)" }, "salience": 0.030822212, "mentions": [ { "text": { "content": "The Calling of Saint Matthew", "beginOffset": 69 }, "type": "PROPER" } ] } ], "language": "en" }
Limpar
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
A seguir
Comece a usar a API Natural Language no seu idioma com uma Biblioteca de cliente do Cloud Natural Language.
Consulte os Princípios básicos do Natural Language para ver informações sobre como fazer solicitações da API Natural Language e processar respostas.
Teste o Tutorial de análise de sentimento e procure os aplicativos de amostra.