我试图解析一个网站,标记它,并存储在一个数组中不同的句子,所以这成为一个字符串数组。我需要访问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
}
]
}
编辑
打印(类)给我:
{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.'}
你可以这样做:
在这里试试:https://repl.it/Hl9b/1
我不确定这个API契约是什么样子的,所以我不想假设“hate”永远是数组中的第二项。你知道吗
还要注意的是,我将
classes
变量名改为data
,因为classes['classes']
很混乱。你知道吗更改为
相关问题 更多 >
编程相关推荐