[英]Django REST: TypeError: Object of type 'Language' is not JSON serializable
[英]TypeError: Object of type 'Entities' is not JSON serializable IBM Cloud natural language understanding
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序列化
一切取决于您在text参数中插入的内容。 您是否使用相同的文字?
我使用了API参考中的示例,并使用了相同的短语来表示此答案...但是,JSON只知道如何处理Unicode字符串,而不是字节序列。 转换为Unicode (json.dumps(response.decode("utf-8"), indent=2))
,或者转换为一个整数数组(json.dumps(list(response)))
。 您也可以尝试print(json.dumps(list(response.values())))
。
因此,这是将NLU服务与Python结合使用的分步操作。
IBM Cloud ( IBM Bluemix的新名称)
在您的PC中,安装Python之后,尝试在CMD / Terminal中运行命令:
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))
当我在CMD中运行命令python NLUAnalyze.py
时,返回的内容是:
{
"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
}
]
}
只需更换
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中所做的更改的链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.