我尝试使用 IBM Watson 通过 Python 进行情感分析,但收到“无模块”错误。
我按照文档操作,我的代码如下所示:
import json
import constants
from ibm_watson import NaturalLanguageUnderstandingV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
from ibm_watson.natural_language_understanding_v1 import Features, SentimentOptions
class SentimentAnalysis:
def __init__(self):
authenticator = IAMAuthenticator(constants.IBM_WATSON_KEY)
this.natural_language_understanding = NaturalLanguageUnderstandingV1(
version='2020-08-01',
authenticator=authenticator
)
this.natural_language_understanding.set_service_url(constants.IBM_WATSON_URL)
def analyse_sentiments(self, data):
response = this.natural_language_understanding.analyze(
url='www.wsj.com/news/markets',
features=Features(sentiment=SentimentOptions(targets=['stocks']))).get_result()
assert isinstance(response, object)
return response
if __name__ == "__main__":
sentiment_analysis_object = SentimentAnalysis()
data = ("I've seen you")
response = sentiment_analysis_object.analyse_sentiments(data)
print json.dumps(response, indent=2)
我使用的 Python 版本是 Python 3.6.5 :: Anaconda, Inc.
莫回无
喵喔喔
森栏
相关分类