Will
Will

Reputation: 3

Microsoft Text Analytics API (v2.1) sample code does not work

I want to use the Sentiment analysis API of Microsoft Azure. I use their sample code (https://learn.microsoft.com/en-us/azure/cognitive-services/text-analytics/quickstarts/python) as follows.

# -*- coding: utf-8 -*
import requests
# pprint is used to format the JSON response
from pprint import pprint

import os

subscription_key = "*********"
endpoint = "https://westcentralus.api.cognitive.microsoft.com/text/analytics"


sentiment_url = endpoint + "/text/analytics/v2.1/sentiment"


documents = {"documents": [
    {"id": "1", "language": "en",
        "text": "I had a wonderful experience! The rooms were wonderful and the staff was helpful."},
    {"id": "2", "language": "en",
        "text": "I had a terrible time at the hotel. The staff was rude and the food was awful."},
    {"id": "3", "language": "es",
        "text": "Los caminos que llevan hasta Monte Rainier son espectaculares y hermosos."},
    {"id": "4", "language": "es",
     "text": "La carretera estaba atascada. Había mucho tráfico el día de ayer."}
]}


headers = {"Ocp-Apim-Subscription-Key": subscription_key}
response = requests.post(sentiment_url, headers=headers, json=documents)
sentiments = response.json()
pprint(sentiments)

But it only returns {'error': {'code': '404', 'message': 'Resource not found'}} What is the problem?

Upvotes: 0

Views: 431

Answers (1)

Hilton Giesenow
Hilton Giesenow

Reputation: 10844

I haven't played much with this side of the analytics, so I might be totally off base, but it looks to me like you've got "text/analytics" doubled up.

endpoint = "https://westcentralus.api.cognitive.microsoft.com/text/analytics"
sentiment_url = endpoint + "/text/analytics/v2.1/sentiment"

would give a final result of https://westcentralus.api.cognitive.microsoft.com/text/analytics/text/analytics/v2.1/sentiment

and it's probably supposed to just be:

https://westcentralus.api.cognitive.microsoft.com/text/analytics/v2.1/sentiment

Upvotes: 0

Related Questions