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: Object of type 'Entities' is not JSON serializable
请您参考如下方法:
我通过转储 response.result 而不仅仅是 response 解决了这个问题。
API guide错误地说要使用:print(json.dumps(response, indent=2))
在查看 docstring in the source code 时我发现 DetailedResponse 类型包含“结果、 header 和 HTTP 状态代码”。
我认为 API 文档中的示例需要更新,以免误导人们。
