Young Dual
Young Dual

Reputation: 3

sentiment analysis call fails with cognitive service, returning "HttpOperationError"

text_analytics = TextAnalyticsClient(endpoint=endpoint, credentials=credentials)
documents = [
    {
        "id": "1",
        "language": "en",
        "text": "I had the best day of my life."
    }
]
response = text_analytics.sentiment(documents=documents)
for document in response.documents:
    print("Document Id: ", document.id, ", Sentiment Score: ",
          "{:.2f}".format(document.score))

Hi, with the sample code from API manual https://learn.microsoft.com/en-us/azure/cognitive-services/text-analytics/quickstarts/python-sdk#sentiment-analysis I got the following error while trying to call the sentiment classifier


HttpOperationError                        Traceback (most recent call last)
<ipython-input-18-f0fb322c9e8c> in <module>
      8     }
      9 ]
---> 10 response = text_analytics.sentiment(documents=documents)
     11 for document in response.documents:
     12     print("Document Id: ", document.id, ", Sentiment Score: ",

~/anaconda3/envs/lib/python3.6/site-packages/azure/cognitiveservices/language/textanalytics/text_analytics_client.py in sentiment(self, show_stats, documents, custom_headers, raw, **operation_config)
    361 
    362         if response.status_code not in [200, 500]:
--> 363             raise HttpOperationError(self._deserialize, response)
    364 
    365         deserialized = None

HttpOperationError: Operation returned an invalid status code 'Resource Not Found'

Upvotes: 0

Views: 76

Answers (1)

Stanley Gong
Stanley Gong

Reputation: 12153

It works for me on my side , pls follow or check the steps below to get started with python sentiment analysis SDK :

  1. Create a text analysis service on Azure portal : enter image description here

  2. Once created , note its endpoint and either one of two keys. enter image description here

  3. Try the code below :

from azure.cognitiveservices.language.textanalytics import TextAnalyticsClient
from msrest.authentication import CognitiveServicesCredentials

subscriptionKey = "<your Azure servcice key >"
endpoint = "<your Azure servcice endpoint>"


credentials = CognitiveServicesCredentials(subscriptionKey)

text_analytics = TextAnalyticsClient(endpoint=endpoint, credentials=credentials)

documents = [
    {
        "id": "1",
        "language": "en",
        "text": "I had the best day of my life."
    }
]
response = text_analytics.sentiment(documents=documents)
for document in response.documents:
    print("Document Id: ", document.id, ", Sentiment Score: ",
          "{:.2f}".format(document.score))

Result :

enter image description here

Hope it helps .

Upvotes: 1

Related Questions