首页 > 解决方案 > 如何使用 curl 命令为 Watson NLU 指定输入文件?

问题描述

我有一个文本文件,我想通过 curl 命令将其输入到 Watson 的自然语言理解服务中,并且想知道如何在传递给服务的数据上表明这一点。我已经让它使用 URL 工作,但想使用不同的来源。在下面的示例中,我想替换“我喜欢苹果!我不喜欢橘子”。带有文件名。谢谢。

curl -X POST -u "apikey:{apikey}" \
"{url}/v1/analyze?version=2019-07-12" \
--request POST \
--header "Content-Type: application/json" \
--data '{
  "text": "I love apples! I do not like oranges.",
  "features": {
    "sentiment": {
      "targets": [
        "apples",
        "oranges",
        "broccoli"
      ]
    },
    "keywords": {
      "emotion": true
    }
  }
}'

标签: curlnlpibm-watson

解决方案


您可以将 --data 参数作为 .json 文件提供,以包含文本,但也需要包含其他查询参数。

来自 API 文档 - https://cloud.ibm.com/apidocs/natural-language-understanding#analyze-text

 curl -X POST -u "apikey":"{apikey}" -H "Content-Type: application/json" -d @parameters.json "https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze?version=2019-07-12"

其中parameters.json看起来像:

 {
   "text": "IBM is an American multinational technology company headquartered in Armonk, New York, United States, with operations in over 170 countries.",
   "features": {
     "entities": {
       "emotion": true,
       "sentiment": true,
       "limit": 2
     },
     "keywords": {
       "emotion": true,
       "sentiment": true,
       "limit": 2
     }
   }
 }

推荐阅读