使用 gcloud 工具执行情感分析
本页面介绍如何利用 Google Cloud SDK 开始使用 Cloud Natural Language API。
如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示:
准备工作
-
登录您的 Google 账号。
如果您还没有 Google 账号,请注册新账号。
- 安装 Google Cloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init
-
-
创建 Google Cloud 项目:
gcloud projects create PROJECT_ID
将
PROJECT_ID
替换为您要创建的 Google Cloud 项目的名称。 -
选择您创建的 Google Cloud 项目:
gcloud config set project PROJECT_ID
将
PROJECT_ID
替换为您的 Google Cloud 项目 名称。
-
-
Enable the Cloud Natural Language API:
gcloud services enable language.googleapis.com
- 安装 Google Cloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init
-
-
创建 Google Cloud 项目:
gcloud projects create PROJECT_ID
将
PROJECT_ID
替换为您要创建的 Google Cloud 项目的名称。 -
选择您创建的 Google Cloud 项目:
gcloud config set project PROJECT_ID
将
PROJECT_ID
替换为您的 Google Cloud 项目 名称。
-
-
Enable the Cloud Natural Language API:
gcloud services enable language.googleapis.com
发出实体分析请求
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" }
清理
- 在 Google Cloud 控制台中,进入管理资源页面。
- 在项目列表中,选择要删除的项目,然后点击删除。
- 在对话框中输入项目 ID,然后点击关闭以删除项目。
后续步骤
通过使用 Cloud Natural Language 客户端库,开始使用您所选语言的 Natural Language API。
查阅 Natural Language 基础知识,了解有关形成 Natural Language API 请求和处理响应的概念信息。