I'm trying to use IBM watson for sentiment analysis. but it is crashing on the import: from ibm_watson import NaturalLanguageUnderstandingV1
The whole code snippet is
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(
text=data,
features=Features(sentiment=SentimentOptions())).get_result()
assert isinstance(response, object)
return response
My python version is Python 2.7.16 Installed IBM watson using pip install --upgrade "ibm-watson>=4.6.0"
The error I'm seeing is
/usr/bin/python /Users/rabbal1892/Desktop/DeepInsight/nextcontent-etl/scrapers/sentiment_analysis.py Traceback (most recent call last): File "/Users/rabbal1892/Desktop/DeepInsight/nextcontent-etl/scrapers/sentiment_analysis.py", line 3, in <module>
from ibm_watson import NaturalLanguageUnderstandingV1 File "/Users/rabbal1892/Library/Python/2.7/lib/python/site-packages/ibm_watson/__init__.py", line 16, in <module>
from ibm_cloud_sdk_core import IAMTokenManager, DetailedResponse, BaseService, ApiException File "/Users/rabbal1892/Library/Python/2.7/lib/python/site-packages/ibm_cloud_sdk_core/__init__.py", line 34, in <module>
from .base_service import BaseService File "/Users/rabbal1892/Library/Python/2.7/lib/python/site-packages/ibm_cloud_sdk_core/base_service.py", line 68
service_url: str = None,
^ SyntaxError: invalid syntax
I'll appreciate any help. Thanks.
The ibm-watson project description page mentions it's only tested on Python V3.x versions.
There is a lot of Python v3 syntax that is not compatible with Python v2.
Since you mention you have Python v2, you should Python v3 instead if you want to using the ibm-watson library as is, you will have to use Python v3 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.