我试图解析一个网站,将其标记化并在数组中存储不同的句子,这样就变成了一个字符串数组。我需要访问从json返回的类。例如,我必须上课:nhate和hate。如果这个班级很讨厌,那个班级的信心是> 0.50,然后做点什么。但是我无法访问这些课程。
words = text.split(".")
c=0
for i in words:
if not words[c]:
words[c] = "this was empty before."
classes = natural_language_classifier.classify('90e7b4x199-nlc-36073',words[c])
result = json.dumps(classes, indent=2)
if (classes.class_name == 'hate' and classes.confidence > 0.50):
print(json.dumps(classes, indent=2))
c=c+1
我得到的错误是:
Traceback (most recent call last):
File "parse.py", line 45, in <module>
if (classes.class_name == 'hate' and classes.confidence > 0.50)
AttributeError: 'dict' object has no attribute 'class_name'
已编辑:我获得的json是这样的:
{
"classifier_id": "10D41B-nlc-1",
"url": "https://gateway.watsonplatform.net/natural-language-classifier /api/v1/classifiers/10D41B-nlc-1/classify?text=How%20hot%20wil/10D41B-nlc-1",
"text": "How hot will it be today?",
"top_class": "nhate",
"classes": [
{
"class_name": "nhate",
"confidence": 0.9998201258549781
},
{
"class_name": "hate",
"confidence": 0.00017987414502176904
}
]
}
被修改
print(classes)给了我:
{u'url': u'https://gateway.watsonplatform.net/natural-language-classifier/api/v1
/classifiers/90e7b4x199-nlc-36073',
u'text': u' A Partnership With Abu Shaklak Printing House',
u'classes':
[{u'class_name': u'nhate.', u'confidence': 0.9398546
187612434}, {u'class_name': u'hate.', u'confidence': 0.0449277873541271}, {u'cla
ss_name': u'Feels good man', u'confidence': 0.015217593884629425}], u'classifier
_id': u'90e7b4x199-nlc-36073', u'top_class': u'nhate.'}