[英]Python IBM Watson NLU Sentiment Analysis - TypeError: cannot convert dic
使用以下代碼,我收到錯誤消息
TypeError:無法將字典更新序列元素#0轉換為序列
他使用的代碼如下:
import watson_developer_cloud as WDC
import watson_developer_cloud.natural_language_understanding.features.v1 as nluFeatures
#from wdc_config import nlu_config
nlu = WDC.NaturalLanguageUnderstandingV1('2017-02-27',username='myusernamehere',password='mypasswordhere')
data = ([1, "I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser Gate. All those moments will be lost in time, like tears in rain. Time to die."])
def nlu_analyze():
response = nlu.analyze(text=data,features=[nluFeatures.Keywords(),nluFeatures.Entities(),nluFeatures.Categories(),nluFeatures.Emotion(),nluFeatures.Sentiment()])
return response
response = nlu_analyze()
print(response["keywords"])
print(response["entities"])
print(response["categories"])
print(response["emotion"])
print(response["sentiment"])
為什么會出現此錯誤?
多虧了楚格茨,這個問題才得以解決。
import watson_developer_cloud as WDC
from watson_developer_cloud.natural_language_understanding_v1 import Features, KeywordsOptions, EntitiesOptions, CategoriesOptions, EmotionOptions, SentimentOptions
nlu = WDC.NaturalLanguageUnderstandingV1('2017-02-27',username='yourusername',password='yourpassword')
data = ("I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser Gate. All those moments will be lost in time, like tears in rain. Time to die.")
def nlu_analyze():
response = nlu.analyze(text=data, features=Features(keywords=KeywordsOptions(), entities=EntitiesOptions(), categories=CategoriesOptions(), emotion=EmotionOptions(), sentiment=SentimentOptions()))
return response
response = nlu_analyze()
print(response["keywords"])
print(response["entities"])
print(response["categories"])
print(response["emotion"])
print(response["sentiment"])
我認為問題出在您導入功能上
import watson_developer_cloud.natural_language_understanding.features.v1 as nluFeatures
根據API文檔-https://www.ibm.com/watson/developercloud/natural-language-understanding/api/v1/? python#post- analyze應該是哪個
from watson_developer_cloud.natural_language_understanding_v1 \
import Features, KeywordsOptions, EntitiesOptions, CategoriesOptions, EmotionOptions, SentimentOptions
和
response = nlu.analyze(text=data,features=[nluFeatures.Keywords(),nluFeatures.Entities(),nluFeatures.Categories(),nluFeatures.Emotion(),nluFeatures.Sentiment()])
會成為
response = nlu.analyze(text=data,features=Features(keywords=KeywordsOptions(),entities=EntitiesOptions(),categories=CategoriesOptions(),emotion=EmotionOptions(),sentiment=SentimentOptions()))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.