Natural Language Understanding 入门
此简短教程通过示例请求和指向其他资源的链接介绍了 Natural Language Understanding API。
观看以下视频以获取 Natural Language Understanding 服务入门的直观摘要。
开始之前
- 创建服务的实例:
- 转至 IBM Cloud 目录中的 Natural Language Understanding 页面。
- 注册免费的 IBM Cloud 帐户或登录。
- 单击创建。
- 复制凭证以向服务实例进行认证:
- 在管理页面上,单击显示凭证。
- 复制
API Key
和URL
值。
- 确保您具有
curl
命令。-
测试是否安装了
curl
。 在命令行上运行以下命令。 如果输出列出具有 SSL 支持的curl
版本,那么将针对教程进行设置。curl -V
-
如果需要,请从 curl.haxx.se安装启用了 SSL 的版本。 如果想要从任何命令行位置运行
curl
,请将文件位置添加到 PATH 环境变量。
-
本教程说明了如何通过命令行界面使用 Natural Language Understanding API。 要下载各种编程语言的客户机库,请查阅 Watson SDK。
第 1 步:分析 Web 页面
运行以下命令来分析 Web 页面以获取观点、概念、类别、实体和关键字。
curl -X POST -u "apikey:{apikey}" \
--header "Content-Type: application/json" \
--data '{
"url": "http://newsroom.ibm.com/Guerbet-and-IBM-Watson-Health-Announce-Strategic-Partnership-for-Artificial-Intelligence-in-Medical-Imaging-Liver",
"features": {
"sentiment": {},
"categories": {},
"concepts": {},
"entities": {},
"keywords": {}
}
}' \
"{url}/v1/analyze?version=2019-07-12"
Windows 用户: 此命令可能未在 Windows 上运行。 请改为运行以下命令:
curl -X POST -u "apikey:{apikey}" --header "Content-Type: application/json" --data "{\"url\":\"http://newsroom.ibm.com/Guerbet-and-IBM-Watson-Health-Announce-Strategic-Partnership-for-Artificial-Intelligence-in-Medical-Imaging-Liver\",\"features\":{\"sentiment\":{},\"categories\":{},\"concepts\":{},\"entities\":{},\"keywords\":{}}}" "{url}/v1/analyze?version=2019-07-12"
下一步演示如何指定用于为每个特征定制分析的选项。
第 2 步:分析目标短语和关键字
Natural Language Understanding 可以在周围文本的上下文中分析目标短语,以获得焦点观点和情绪结果。 以下示例中观点的 targets 选项指示服务搜索目标“apples”、“oranges”和“brocoli”。 由于“apples”和“oranges”位于文本中,因此将为这些目标返回观点分数。
您还可以获取文本中检测到的实体和关键字的观点和情绪结果。 在该示例中,关键字的 emotion 选项指示服务分析情绪结果中每个检测到的关键字。
curl -X POST -u "apikey:{apikey}" \
--header "Content-Type: application/json" \
--data '{
"text": "I love apples! I do not like oranges.",
"features": {
"sentiment": {
"targets": [
"apples",
"oranges",
"broccoli"
]
},
"keywords": {
"emotion": true
}
}
}' \
"{url}/v1/analyze?version=2019-07-12"
适用于 Windows 用户的 Runnable 命令:
curl -X POST -u "apikey:{apikey}" --header "Content-Type: application/json" --data "{\"text\":\"I love apples! I do not like oranges.\",\"features\":{\"sentiment\":{\"targets\":[\"apples\",\"oranges\",\"broccoli\"]},\"keywords\":{\"emotion\":true}}}" "{url}/v1/analyze?version=2019-07-12"