gpt4 book ai didi

Python IBM Watson NLU 情感分析 - TypeError : cannot convert dic

转载 作者:行者123 更新时间:2023-11-30 22:15:09 25 4
gpt4 key购买 nike

使用以下代码,我收到错误消息

TypeError: cannot convert dictionary update sequence element #0 to a sequence

使用的代码如下:

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"])

为什么我会收到此错误?

<小时/>

已解决

感谢 chughts,这个问题已经解决了。

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()))

关于Python IBM Watson NLU 情感分析 - TypeError : cannot convert dic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50366496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com