使用 gcloud 工具執行情緒分析
本頁面說明如何透過 Google Cloud SDK 開始使用 Cloud Natural Language API。
如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導):
事前準備
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
建立實體分析要求
gcloud
使用gcloud
指令列工具呼叫 analyze-entities
指令,並使用 --content
旗標指定要分析的文字。gcloud ml language analyze-entities --content="Michelangelo Caravaggio, Italian painter, is known for 'The Calling of Saint Matthew'."
指令列
使用curl
向 documents: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" }
清除所用資源
- 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.
後續步驟
透過 Cloud Natural Language 用戶端程式庫,以您選擇的語言開始使用 Natural Language API。
有關建構 Natural Language API 要求及處理回應的概念性資訊,請參閱 Natural Language 基礎知識。