Realizar análisis de sentimiento con la herramienta gcloud

En esta página se muestra cómo empezar a usar la API Natural Language de Cloud con el SDK de Google Cloud.


Para seguir las instrucciones paso a paso de esta tarea directamente en la Google Cloud consola, haz clic en Ayúdame:

Guíame


Antes de empezar

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Install the Google Cloud CLI.

  3. Si utilizas un proveedor de identidades (IdP) externo, primero debes iniciar sesión en la CLI de gcloud con tu identidad federada.

  4. Para inicializar gcloud CLI, ejecuta el siguiente comando:

    gcloud init
  5. 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.

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Natural Language API:

    gcloud services enable language.googleapis.com
  8. Install the Google Cloud CLI.

  9. Si utilizas un proveedor de identidades (IdP) externo, primero debes iniciar sesión en la CLI de gcloud con tu identidad federada.

  10. Para inicializar gcloud CLI, ejecuta el siguiente comando:

    gcloud init
  11. 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.

  12. Verify that billing is enabled for your Google Cloud project.

  13. Enable the Cloud Natural Language API:

    gcloud services enable language.googleapis.com
  14. Hacer una solicitud de análisis de entidades

    gcloud

    Usa la herramienta de línea de comandos gcloud para llamar al comando analyze-entities y usa la marca --content para especificar el texto que quieres analizar.
    gcloud ml language analyze-entities --content="Michelangelo Caravaggio, Italian painter, is known for 'The Calling of Saint Matthew'."
    

    Línea de comandos

    Usa curl para hacer una solicitud POST al método documents:analyzeEntities y proporciona el cuerpo de la solicitud adecuado, tal como se muestra en el siguiente ejemplo.

    En el ejemplo se usa el comando gcloud auth application-default print-access-token para obtener un token de acceso de la cuenta de servicio que creaste al configurar el proyecto.

    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'
    }"

    Deberías ver una respuesta similar a la siguiente:

    {
      "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"
    }

    Limpieza

      Delete a Google Cloud project:

      gcloud projects delete PROJECT_ID

    Siguientes pasos