使用 gcloud 工具執行情緒分析

本頁說明如何透過 Google Cloud SDK 開始使用 Cloud Natural Language API。


如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導)

逐步引導


事前準備

  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. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  4. 如要初始化 gcloud CLI,請執行下列指令:

    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. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

  10. 如要初始化 gcloud CLI,請執行下列指令:

    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. 建立實體分析要求

    gcloud

    使用 gcloud 指令列工具呼叫 analyze-entities 指令,並使用 --content 旗標指定要分析的文字。
    gcloud ml language analyze-entities --content="Michelangelo Caravaggio, Italian painter, is known for 'The Calling of Saint Matthew'."
    

    指令列

    使用 curldocuments:analyzeEntities 方法發出 POST 要求並提供適當的要求主體,如同下列範例所示。

    下列範例針對您在設定專案時建立的服務帳戶,使用 gcloud auth application-default print-access-token 指令取得該帳戶的存取憑證。

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

    畫面會顯示類似以下的回應:

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

    清除所用資源

      Delete a Google Cloud project:

      gcloud projects delete PROJECT_ID

    後續步驟