1
import json
from watson_developer_cloud import NaturalLanguageUnderstandingV1
import watson_developer_cloud.natural_language_understanding.features.v1 \
as Features

natural_language_understanding = NaturalLanguageUnderstandingV1(
username="username",
password="password",
version="2017-02-27")

response = natural_language_understanding.analyze(
text="IBM is an American multinational technology company headquartered \
in Armonk, New York, United States, with operations in over 170 \
countries.",
features=[
Features.Entities(
  emotion=True,
  sentiment=True,
  limit=2
),
Features.Keywords(
  emotion=True,
  sentiment=True,
  limit=2
)
 ]
 )

print(json.dumps(response, indent=2))

我是 IBM watson API 的新手.....我正在尝试他们提供的这个示例代码,我收到了这个错误

TypeError:“实体”类型的对象不是 JSON 可序列化的

4

3 回答 3

3

我通过倾倒response.result而不是仅仅解决了这个问题response

API 指南错误地说要使用:print(json.dumps(response, indent=2))

在查看源代码中的文档字符串时,我发现该 DetailedResponse类型包含“结果、标题和 HTTP 状态代码”。

我认为 API 文档中的示例需要更新,以免误导人们。

于 2018-09-20T20:11:40.503 回答
1

一切都取决于您在文本参数中插入的内容。你使用相同的文本吗?

我使用 API 参考中的示例和相同的短语来回答这个问题......但是,JSON 只知道如何处理 Unicode 字符串,而不知道如何处理字节序列。要么转换成 Unicode (json.dumps(response.decode("utf-8"), indent=2)),要么是一个整数数组(json.dumps(list(response)))。你也可以试试print(json.dumps(list(response.values())))

因此,这是在 Python 中使用 NLU 服务的一步一步。

IBM CloudIBM Bluemix的新名称)

  • 创建一个帐户(现在,您无需信用卡即可创建并使用 Watson 和其他服务的 LITE 计划!)
  • 目录 -> Watson -> 自然语言理解服务 -> 创建 -> 服务凭证

在您的 PC 中,安装 Python 后,尝试在 CMD/终端中运行命令:

pip install --upgrade watson-developer-cloud

使用API 参考中提供的相同代码:

import json
from watson_developer_cloud import NaturalLanguageUnderstandingV1
import watson_developer_cloud.natural_language_understanding.features.v1 \
  as Features

natural_language_understanding = NaturalLanguageUnderstandingV1(
  username="username from the NLU -> Service Credentials",
  password="passoword from the NLU -> Service Credentials",
  version="2017-02-27")

response = natural_language_understanding.analyze(
  text="IBM is an American multinational technology company headquartered \
    in Armonk, New York, United States, with operations in over 170 \
    countries.",
  features=[
    Features.Entities(
      emotion=True,
      sentiment=True,
      limit=2
    ),
    Features.Keywords(
      emotion=True,
      sentiment=True,
      limit=2
    )
  ]
)

print(json.dumps(response, indent=2))

当我python NLUAnalyze.py在 CMD 中运行命令时的返回是:

{
  "usage": {
    "text_units": 1,
    "text_characters": 148,
    "features": 2
  },
  "language": "en",
  "keywords": [
    {
      "text": "American multinational technology",
      "sentiment": {
        "score": 0.0,
        "label": "neutral"
      },
      "relevance": 0.993518,
      "emotion": {
        "sadness": 0.085259,
        "joy": 0.026169,
        "fear": 0.02454,
        "disgust": 0.088711,
        "anger": 0.033078
      }
    },
    {
      "text": "New York",
      "sentiment": {
        "score": 0.0,
        "label": "neutral"
      },
      "relevance": 0.613816,
      "emotion": {
        "sadness": 0.166741,
        "joy": 0.228903,
        "fear": 0.057987,
        "disgust": 0.050965,
        "anger": 0.054653
      }
    }
  ],
  "entities": [
    {
      "type": "Company",
      "text": "IBM",
      "sentiment": {
        "score": 0.0,
        "label": "neutral"
      },
      "relevance": 0.33,
      "emotion": {
        "sadness": 0.085259,
        "joy": 0.026169,
        "fear": 0.02454,
        "disgust": 0.088711,
        "anger": 0.033078
      },
      "disambiguation": {
        "subtype": [
          "SoftwareLicense",
          "OperatingSystemDeveloper",
          "ProcessorManufacturer",
          "SoftwareDeveloper",
          "CompanyFounder",
          "ProgrammingLanguageDesigner",
          "ProgrammingLanguageDeveloper"
        ],
        "name": "IBM",
        "dbpedia_resource": "http://dbpedia.org/resource/IBM"
      },
      "count": 1
    }
  ]
}
于 2017-11-17T12:29:14.710 回答
1

从 IBM 开发人员那里得到了解决方案, 这里是链接

只需更换

features=[
   Features.Entities(
          emotion=True,
          sentiment=True,
           limit=2
    ),
   Features.Keywords(
           emotion=True,
           sentiment=True,
           limit=2
    )
]

和 :

features=Features(entities=EntitiesOptions(
                      emotion=True, sentiment=True,limit=2), 
               keywords=KeywordsOptions(
                      emotion=True, sentiment=True,limit=2
                                ))

这是由于在 v 1 python sdk 中所做的更改 这里是显示在 v 1 python sdk 中所做更改的链接

于 2017-11-23T03:34:16.310 回答